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. */
51 /* And we also need the data for the reentrant functions. */
61 /* Define the macros `_' and `N_' for conveniently marking translatable
62 strings in the libc source code. */
64 #define N_(msgid) msgid
67 extern const char _libc_intl_domainname
[];
70 /* This is defined as an optimizing macro, so use it. */
71 #define _(msgid) dgettext (_libc_intl_domainname, (msgid))
73 /* Be sure to use only the __ name when `dgettext' is a plain function
74 instead of an optimizing macro. */
75 #define _(msgid) __dgettext (_libc_intl_domainname, (msgid))
83 /* The symbols in all the user (non-_) macros are C symbols.
84 HAVE_GNU_LD without HAVE_ELF implies a.out. */
86 #if defined (HAVE_ASM_WEAK_DIRECTIVE) || defined (HAVE_ASM_WEAKEXT_DIRECTIVE)
87 #define HAVE_WEAK_SYMBOLS
90 #ifndef __SYMBOL_PREFIX
92 #define __SYMBOL_PREFIX
94 #define __SYMBOL_PREFIX "_"
100 #define C_SYMBOL_NAME(name) name
102 #define C_SYMBOL_NAME(name) _##name
107 /* Define ALIAS as a strong alias for ORIGINAL. */
108 #ifdef HAVE_ASM_SET_DIRECTIVE
109 #define strong_alias_asm(original, alias) \
110 ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME (alias); \
111 .set C_SYMBOL_NAME (alias),C_SYMBOL_NAME (original)
113 #define strong_alias(original, alias) strong_alias_asm (original, alias)
115 #define strong_alias(original, alias) \
116 asm (__string_1 (ASM_GLOBAL_DIRECTIVE) " " __SYMBOL_PREFIX #alias "\n" \
117 ".set " __SYMBOL_PREFIX #alias "," __SYMBOL_PREFIX #original);
120 #define strong_alias_asm(original, alias) \
121 ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME (alias); \
122 C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original)
124 #define strong_alias(original, alias) strong_alias_asm (original, alias)
126 #define strong_alias(original, alias) \
127 asm (__string_1 (ASM_GLOBAL_DIRECTIVE) " " __SYMBOL_PREFIX #alias "\n" \
128 __SYMBOL_PREFIX #alias " = " __SYMBOL_PREFIX #original);
132 /* Helper macros used above. */
133 #define __string_1(x) __string_0(x)
134 #define __string_0(x) #x
137 #ifdef HAVE_WEAK_SYMBOLS
141 #ifdef HAVE_ASM_WEAKEXT_DIRECTIVE
143 /* Define ALIAS as a weak alias for ORIGINAL.
144 If weak aliases are not available, this defines a strong alias. */
145 #define weak_alias(original, alias) \
146 .weakext C_SYMBOL_NAME (alias), C_SYMBOL_NAME (original)
148 /* Declare SYMBOL as weak undefined symbol (resolved to 0 if not defined). */
149 #define weak_extern(symbol) \
150 .weakext C_SYMBOL_NAME (symbol)
152 #else /* ! HAVE_ASM_WEAKEXT_DIRECTIVE */
154 /* Define ALIAS as a weak alias for ORIGINAL.
155 If weak aliases are not available, this defines a strong alias. */
156 #define weak_alias(original, alias) \
157 .weak C_SYMBOL_NAME (alias); \
158 C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original)
161 /* Declare SYMBOL as weak undefined symbol (resolved to 0 if not defined). */
162 #define weak_extern(symbol) \
163 .weak C_SYMBOL_NAME (symbol)
165 #endif /* ! HAVE_ASM_WEAKEXT_DIRECTIVE */
167 #else /* ! ASSEMBLER */
169 #ifdef HAVE_ASM_WEAKEXT_DIRECTIVE
170 #define weak_extern_asm(symbol) asm (".weakext " __SYMBOL_PREFIX #symbol);
171 #define weak_alias_asm(original, alias) \
172 asm (".weakext " __SYMBOL_PREFIX #alias ", " __SYMBOL_PREFIX #original);
173 #else /* ! HAVE_ASM_WEAKEXT_DIRECTIVE */
174 #define weak_extern_asm(symbol) asm (".weak " __SYMBOL_PREFIX #symbol);
175 #define weak_alias_asm(original, alias) \
176 asm (".weak " __SYMBOL_PREFIX #alias "\n" \
177 __SYMBOL_PREFIX #alias " = " __SYMBOL_PREFIX #original);
178 #endif /* ! HAVE_ASM_WEAKEXT_DIRECTIVE */
180 #define weak_alias(o, a) weak_alias_asm (o, a)
181 #define weak_extern(symbol) weak_extern_asm (symbol)
183 #endif /* ! ASSEMBLER */
185 #define weak_alias(original, alias) strong_alias(original, alias)
186 #define weak_extern(symbol) /* Do nothing; the ref will be strong. */
190 #if (!defined (ASSEMBLER) && \
191 (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)))
192 /* GCC 2.7 and later has special syntax for weak symbols and aliases.
193 Using that is better when possible, because the compiler and assembler
194 are better clued in to what we are doing. */
196 #define strong_alias(name, aliasname) \
197 extern __typeof (name) aliasname __attribute__ ((alias (#name)));
199 #ifdef HAVE_WEAK_SYMBOLS
201 #define weak_alias(name, aliasname) \
202 extern __typeof (name) aliasname __attribute__ ((weak, alias (#name)));
204 /* This comes between the return type and function name in
205 a function definition to make that definition weak. */
206 #define weak_function __attribute__ ((weak))
207 #define weak_const_function __attribute__ ((weak, __const__))
209 #endif /* HAVE_WEAK_SYMBOLS. */
210 #endif /* Not ASSEMBLER, and GCC 2.7 or later. */
213 #ifndef weak_function
214 /* If we do not have the __attribute__ ((weak)) syntax, there is no way we
215 can define functions as weak symbols. The compiler will emit a `.globl'
216 directive for the function symbol, and a `.weak' directive in addition
217 will produce an error from the assembler. */
218 #define weak_function /* empty */
219 #define weak_const_function /* empty */
223 /* When a reference to SYMBOL is encountered, the linker will emit a
224 warning message MSG. */
227 #define link_warning(symbol, msg) \
228 static const char __evoke_link_warning_##symbol[] \
229 __attribute__ ((section (".gnu.warning." #symbol))) = msg;
231 #define link_warning(symbol, msg) \
232 asm(".stabs \"" msg "\",30,0,0,0\n" \
233 ".stabs \"" __SYMBOL_PREFIX #symbol "\",1,0,0,0\n");
236 /* We will never be heard; they will all die horribly. */
237 #define link_warning(symbol, msg)
240 /* A canned warning for sysdeps/stub functions. */
241 #define stub_warning(name) \
242 link_warning (name, \
243 "warning: " #name " is not implemented and will always fail")
251 /* Symbol set support macros. */
255 /* Make SYMBOL, which is in the text segment, an element of SET. */
256 #define text_set_element(set, symbol) _elf_set_element(set, symbol)
257 /* Make SYMBOL, which is in the data segment, an element of SET. */
258 #define data_set_element(set, symbol) _elf_set_element(set, symbol)
259 /* Make SYMBOL, which is in the bss segment, an element of SET. */
260 #define bss_set_element(set, symbol) _elf_set_element(set, symbol)
262 /* These are all done the same way in ELF.
263 There is a new section created for each set. */
265 /* When building a shared library, make the set section writable,
266 because it will need to be relocated at run time anyway. */
267 #define _elf_set_element(set, symbol) \
268 static const void *__elf_set_##set##_element_##symbol##__ \
269 __attribute__ ((unused, section (#set))) = &(symbol)
271 #define _elf_set_element(set, symbol) \
272 static const void *const __elf_set_##set##_element_##symbol##__ \
273 __attribute__ ((unused, section (#set))) = &(symbol)
276 /* Define SET as a symbol set. This may be required (it is in a.out) to
277 be able to use the set's contents. */
278 #define symbol_set_define(set) symbol_set_declare(set)
280 /* Declare SET for use in this module, if defined in another module. */
281 #define symbol_set_declare(set) \
282 extern void *const __start_##set __attribute__ ((__weak__)); \
283 extern void *const __stop_##set __attribute__ ((__weak__)); \
284 weak_extern (__start_##set) weak_extern (__stop_##set)
286 /* Return a pointer (void *const *) to the first element of SET. */
287 #define symbol_set_first_element(set) (&__start_##set)
289 /* Return true iff PTR (a void *const *) has been incremented
290 past the last element in SET. */
291 #define symbol_set_end_p(set, ptr) ((ptr) >= &__stop_##set)
293 #else /* Not ELF: a.out. */
295 #define text_set_element(set, symbol) \
296 asm(".stabs \"" __SYMBOL_PREFIX #set "\",23,0,0," __SYMBOL_PREFIX #symbol)
297 #define data_set_element(set, symbol) \
298 asm(".stabs \"" __SYMBOL_PREFIX #set "\",25,0,0," __SYMBOL_PREFIX #symbol)
299 #define bss_set_element(set, symbol) ?error Must use initialized data.
300 #define symbol_set_define(set) void *const (set)[1];
301 #define symbol_set_declare(set) extern void *const (set)[1];
303 #define symbol_set_first_element(set) &(set)[1]
304 #define symbol_set_end_p(set, ptr) (*(ptr) == 0)
307 #endif /* Have GNU ld. */
309 #endif /* libc-symbols.h */