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 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
18 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
19 Cambridge, MA 02139, USA. */
21 #ifndef _LIBC_SYMBOLS_H
22 #define _LIBC_SYMBOLS_H
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. */
59 /* Define the macros `_' and `N_' for conveniently marking translatable
60 strings in the libc source code. */
62 #define N_(msgid) msgid
65 extern const char _libc_intl_domainname
[];
68 /* This is defined as an optimizing macro, so use it. */
69 #define _(msgid) dgettext (_libc_intl_domainname, (msgid))
71 /* Be sure to use only the __ name when `dgettext' is a plain function
72 instead of an optimizing macro. */
73 #define _(msgid) __dgettext (_libc_intl_domainname, (msgid))
81 /* The symbols in all the user (non-_) macros are C symbols.
82 HAVE_GNU_LD without HAVE_ELF implies a.out. */
84 #if defined (HAVE_ASM_WEAK_DIRECTIVE) || defined (HAVE_ASM_WEAKEXT_DIRECTIVE)
85 #define HAVE_WEAK_SYMBOLS
88 #ifndef __SYMBOL_PREFIX
90 #define __SYMBOL_PREFIX
92 #define __SYMBOL_PREFIX "_"
98 #define C_SYMBOL_NAME(name) name
100 #define C_SYMBOL_NAME(name) _##name
105 /* Define ALIAS as a strong alias for ORIGINAL. */
106 #ifdef HAVE_ASM_SET_DIRECTIVE
107 #define strong_alias_asm(original, alias) \
108 ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME (alias); \
109 .set C_SYMBOL_NAME (alias),C_SYMBOL_NAME (original)
111 #define strong_alias(original, alias) strong_alias_asm (original, alias)
113 #define strong_alias(original, alias) \
114 asm (__string_1 (ASM_GLOBAL_DIRECTIVE) " " __SYMBOL_PREFIX #alias "\n" \
115 ".set " __SYMBOL_PREFIX #alias "," __SYMBOL_PREFIX #original);
118 #define strong_alias_asm(original, alias) \
119 ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME (alias); \
120 C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original)
122 #define strong_alias(original, alias) strong_alias_asm (original, alias)
124 #define strong_alias(original, alias) \
125 asm (__string_1 (ASM_GLOBAL_DIRECTIVE) " " __SYMBOL_PREFIX #alias "\n" \
126 __SYMBOL_PREFIX #alias " = " __SYMBOL_PREFIX #original);
130 /* Helper macros used above. */
131 #define __string_1(x) __string_0(x)
132 #define __string_0(x) #x
135 #ifdef HAVE_WEAK_SYMBOLS
139 #ifdef HAVE_ASM_WEAKEXT_DIRECTIVE
141 /* Define ALIAS as a weak alias for ORIGINAL.
142 If weak aliases are not available, this defines a strong alias. */
143 #define weak_alias(original, alias) \
144 .weakext C_SYMBOL_NAME (alias), C_SYMBOL_NAME (original)
146 /* Declare SYMBOL as weak undefined symbol (resolved to 0 if not defined). */
147 #define weak_extern(symbol) \
148 .weakext C_SYMBOL_NAME (symbol)
150 #else /* ! HAVE_ASM_WEAKEXT_DIRECTIVE */
152 /* Define ALIAS as a weak alias for ORIGINAL.
153 If weak aliases are not available, this defines a strong alias. */
154 #define weak_alias(original, alias) \
155 .weak C_SYMBOL_NAME (alias); \
156 C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original)
159 /* Declare SYMBOL as weak undefined symbol (resolved to 0 if not defined). */
160 #define weak_extern(symbol) \
161 .weak C_SYMBOL_NAME (symbol)
163 #endif /* ! HAVE_ASM_WEAKEXT_DIRECTIVE */
165 #else /* ! ASSEMBLER */
167 #ifdef HAVE_ASM_WEAKEXT_DIRECTIVE
168 #define weak_extern_asm(symbol) asm (".weakext " __SYMBOL_PREFIX #symbol);
169 #define weak_alias_asm(original, alias) \
170 asm (".weakext " __SYMBOL_PREFIX #alias ", " __SYMBOL_PREFIX #original);
171 #else /* ! HAVE_ASM_WEAKEXT_DIRECTIVE */
172 #define weak_extern_asm(symbol) asm (".weak " __SYMBOL_PREFIX #symbol);
173 #define weak_alias_asm(original, alias) \
174 asm (".weak " __SYMBOL_PREFIX #alias "\n" \
175 __SYMBOL_PREFIX #alias " = " __SYMBOL_PREFIX #original);
176 #endif /* ! HAVE_ASM_WEAKEXT_DIRECTIVE */
178 #define weak_alias(o, a) weak_alias_asm (o, a)
179 #define weak_extern(symbol) weak_extern_asm (symbol)
181 #endif /* ! ASSEMBLER */
183 #define weak_alias(original, alias) strong_alias(original, alias)
184 #define weak_extern(symbol) /* Do nothing; the ref will be strong. */
188 #if (!defined (ASSEMBLER) && \
189 (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)))
190 /* GCC 2.7 and later has special syntax for weak symbols and aliases.
191 Using that is better when possible, because the compiler and assembler
192 are better clued in to what we are doing. */
194 #define strong_alias(name, aliasname) \
195 extern __typeof (name) aliasname __attribute__ ((alias (#name)));
197 #ifdef HAVE_WEAK_SYMBOLS
199 #define weak_alias(name, aliasname) \
200 extern __typeof (name) aliasname __attribute__ ((weak, alias (#name)));
202 /* This comes between the return type and function name in
203 a function definition to make that definition weak. */
204 #define weak_function __attribute__ ((weak))
205 #define weak_const_function __attribute__ ((weak, __const__))
207 #endif /* HAVE_WEAK_SYMBOLS. */
208 #endif /* Not ASSEMBLER, and GCC 2.7 or later. */
211 #ifndef weak_function
212 /* If we do not have the __attribute__ ((weak)) syntax, there is no way we
213 can define functions as weak symbols. The compiler will emit a `.globl'
214 directive for the function symbol, and a `.weak' directive in addition
215 will produce an error from the assembler. */
216 #define weak_function /* empty */
217 #define weak_const_function /* empty */
221 /* When a reference to SYMBOL is encountered, the linker will emit a
222 warning message MSG. */
225 #define link_warning(symbol, msg) \
226 static const char __evoke_link_warning_##symbol[] \
227 __attribute__ ((section (".gnu.warning." #symbol))) = msg;
229 #define link_warning(symbol, msg) \
230 asm(".stabs \"" msg "\",30,0,0,0\n" \
231 ".stabs \"" __SYMBOL_PREFIX #symbol "\",1,0,0,0\n");
234 /* We will never be heard; they will all die horribly. */
235 #define link_warning(symbol, msg)
238 /* A canned warning for sysdeps/stub functions. */
239 #define stub_warning(name) \
240 link_warning (name, \
241 "warning: " #name " is not implemented and will always fail")
249 /* Symbol set support macros. */
253 /* Make SYMBOL, which is in the text segment, an element of SET. */
254 #define text_set_element(set, symbol) _elf_set_element(set, symbol)
255 /* Make SYMBOL, which is in the data segment, an element of SET. */
256 #define data_set_element(set, symbol) _elf_set_element(set, symbol)
257 /* Make SYMBOL, which is in the bss segment, an element of SET. */
258 #define bss_set_element(set, symbol) _elf_set_element(set, symbol)
260 /* These are all done the same way in ELF.
261 There is a new section created for each set. */
263 /* When building a shared library, make the set section writable,
264 because it will need to be relocated at run time anyway. */
265 #define _elf_set_element(set, symbol) \
266 static const void *__elf_set_##set##_element_##symbol##__ \
267 __attribute__ ((unused, section (#set))) = &(symbol)
269 #define _elf_set_element(set, symbol) \
270 static const void *const __elf_set_##set##_element_##symbol##__ \
271 __attribute__ ((unused, section (#set))) = &(symbol)
274 /* Define SET as a symbol set. This may be required (it is in a.out) to
275 be able to use the set's contents. */
276 #define symbol_set_define(set) symbol_set_declare(set)
278 /* Declare SET for use in this module, if defined in another module. */
279 #define symbol_set_declare(set) \
280 extern void *const __start_##set __attribute__ ((__weak__)); \
281 extern void *const __stop_##set __attribute__ ((__weak__)); \
282 weak_extern (__start_##set) weak_extern (__stop_##set)
284 /* Return a pointer (void *const *) to the first element of SET. */
285 #define symbol_set_first_element(set) (&__start_##set)
287 /* Return true iff PTR (a void *const *) has been incremented
288 past the last element in SET. */
289 #define symbol_set_end_p(set, ptr) ((ptr) >= &__stop_##set)
291 #else /* Not ELF: a.out. */
293 #define text_set_element(set, symbol) \
294 asm(".stabs \"" __SYMBOL_PREFIX #set "\",23,0,0," __SYMBOL_PREFIX #symbol)
295 #define data_set_element(set, symbol) \
296 asm(".stabs \"" __SYMBOL_PREFIX #set "\",25,0,0," __SYMBOL_PREFIX #symbol)
297 #define bss_set_element(set, symbol) ?error Must use initialized data.
298 #define symbol_set_define(set) void *const (set)[1];
299 #define symbol_set_declare(set) extern void *const (set)[1];
301 #define symbol_set_first_element(set) &(set)[1]
302 #define symbol_set_end_p(set, ptr) (*(ptr) == 0)
305 #endif /* Have GNU ld. */
307 #endif /* libc-symbols.h */