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 * 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 ASM_GLOBAL_DIRECTIVE 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__ ((unused, section (".gnu.warning." #symbol "\"\n\t#\""))) \
219 # define link_warning(symbol, msg) \
220 __make_section_unallocated (".gnu.warning." #symbol) \
221 static const char __evoke_link_warning_##symbol[] \
222 __attribute__ ((unused, section (".gnu.warning." #symbol "\n\t#"))) = msg;
224 # else /* Not ELF: a.out */
226 /* XCOFF does not support .stabs.
227 The native aix linker will remove the .stab and .stabstr sections
228 The gnu linker will have a fatal error if there is a relocation for
229 symbol in the .stab section. Silently disable this macro. */
230 # define link_warning(symbol, msg)
232 # define link_warning(symbol, msg) \
233 asm (".stabs \"" msg "\",30,0,0,0\n\t" \
234 ".stabs \"" __SYMBOL_PREFIX #symbol "\",1,0,0,0\n");
238 /* We will never be heard; they will all die horribly. */
239 # define link_warning(symbol, msg)
242 /* A canned warning for sysdeps/stub functions. */
243 #define stub_warning(name) \
244 link_warning (name, \
245 "warning: " #name " is not implemented and will always fail")
253 /* Symbol set support macros. */
257 /* Make SYMBOL, which is in the text segment, an element of SET. */
258 # define text_set_element(set, symbol) _elf_set_element(set, symbol)
259 /* Make SYMBOL, which is in the data segment, an element of SET. */
260 # define data_set_element(set, symbol) _elf_set_element(set, symbol)
261 /* Make SYMBOL, which is in the bss segment, an element of SET. */
262 # define bss_set_element(set, symbol) _elf_set_element(set, symbol)
264 /* These are all done the same way in ELF.
265 There is a new section created for each set. */
267 /* When building a shared library, make the set section writable,
268 because it will need to be relocated at run time anyway. */
269 # define _elf_set_element(set, symbol) \
270 static const void *__elf_set_##set##_element_##symbol##__ \
271 __attribute__ ((unused, section (#set))) = &(symbol)
273 # define _elf_set_element(set, symbol) \
274 static const void *const __elf_set_##set##_element_##symbol##__ \
275 __attribute__ ((unused, section (#set))) = &(symbol)
278 /* Define SET as a symbol set. This may be required (it is in a.out) to
279 be able to use the set's contents. */
280 # define symbol_set_define(set) symbol_set_declare(set)
282 /* Declare SET for use in this module, if defined in another module. */
283 # define symbol_set_declare(set) \
284 extern void *const __start_##set __attribute__ ((__weak__)); \
285 extern void *const __stop_##set __attribute__ ((__weak__)); \
286 weak_extern (__start_##set) weak_extern (__stop_##set)
288 /* Return a pointer (void *const *) to the first element of SET. */
289 # define symbol_set_first_element(set) (&__start_##set)
291 /* Return true iff PTR (a void *const *) has been incremented
292 past the last element in SET. */
293 # define symbol_set_end_p(set, ptr) ((ptr) >= &__stop_##set)
295 # else /* Not ELF: a.out. */
298 /* XCOFF does not support .stabs.
299 The native aix linker will remove the .stab and .stabstr sections
300 The gnu linker will have a fatal error if there is a relocation for
301 symbol in the .stab section. Silently disable these macros. */
302 # define text_set_element(set, symbol)
303 # define data_set_element(set, symbol)
304 # define bss_set_element(set, symbol)
306 # define text_set_element(set, symbol) \
307 asm (".stabs \"" __SYMBOL_PREFIX #set "\",23,0,0," __SYMBOL_PREFIX #symbol)
308 # define data_set_element(set, symbol) \
309 asm (".stabs \"" __SYMBOL_PREFIX #set "\",25,0,0," __SYMBOL_PREFIX #symbol)
310 # define bss_set_element(set, symbol) ?error Must use initialized data.
312 # define symbol_set_define(set) void *const (set)[1];
313 # define symbol_set_declare(set) extern void *const (set)[1];
315 # define symbol_set_first_element(set) &(set)[1]
316 # define symbol_set_end_p(set, ptr) (*(ptr) == 0)
320 /* We cannot do anything in generial. */
321 # define text_set_element(set, symbol) asm ("")
322 # define data_set_element(set, symbol) asm ("")
323 # define bss_set_element(set, symbol) asm ("")
324 # define symbol_set_define(set) void *const (set)[1];
325 # define symbol_set_declare(set) extern void *const (set)[1];
327 # define symbol_set_first_element(set) &(set)[1]
328 # define symbol_set_end_p(set, ptr) (*(ptr) == 0)
329 #endif /* Have GNU ld. */
332 # define symbol_version(real, name, version) \
333 _symbol_version(real, name, version)
334 # define default_symbol_version(real, name, version) \
335 _default_symbol_version(real, name, version)
336 # ifdef __ASSEMBLER__
337 # define _symbol_version(real, name, version) \
338 .symver real, name##@##version
339 # define _default_symbol_version(real, name, version) \
340 .symver real, name##@##@##version
342 # define _symbol_version(real, name, version) \
343 __asm__ (".symver " #real "," #name "@" #version)
344 # define _default_symbol_version(real, name, version) \
345 __asm__ (".symver " #real "," #name "@@" #version)
348 # define symbol_version(real, name, version)
349 # define default_symbol_version(real, name, version) \
350 strong_alias(real, name)
353 #if defined HAVE_VISIBILITY_ATTRIBUTE && defined SHARED
354 # define attribute_hidden __attribute__ ((visibility ("hidden")))
356 # define attribute_hidden
359 /* Handling on non-exported internal names. We have to do this only
362 # define INTUSE(name) name##_internal
363 # define INTDEF(name) strong_alias (name, name##_internal)
364 # define INTVARDEF(name) \
365 _INTVARDEF (name, name##_internal)
366 # if defined HAVE_VISIBILITY_ATTRIBUTE
367 # define _INTVARDEF(name, aliasname) \
368 extern __typeof (name) aliasname __attribute__ ((alias (#name), \
369 visibility ("hidden")));
371 # define _INTVARDEF(name, aliasname) \
372 extern __typeof (name) aliasname __attribute__ ((alias (#name)));
374 # define INTDEF2(name, newname) strong_alias (name, newname##_internal)
375 # define INTVARDEF2(name, newname) _INTVARDEF (name, newname##_internal)
377 # define INTUSE(name) name
378 # define INTDEF(name)
379 # define INTVARDEF(name)
380 # define INTDEF2(name, newname)
381 # define INTVARDEF2(name, newname)
384 #endif /* libc-symbols.h */