Mon Jul 8 02:14:25 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[glibc.git] / libc-symbols.h
blob2f72d5fa7afa589d870789edf54e30ee441a9050
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. */
47 #define _LIBC 1
49 /* Enable declarations of GNU extensions, since we are compiling them. */
50 #define _GNU_SOURCE 1
52 #include <config.h>
57 #ifndef ASSEMBLER
59 /* Define the macros `_' and `N_' for conveniently marking translatable
60 strings in the libc source code. */
62 #define N_(msgid) msgid
64 #include <libintl.h>
65 extern const char _libc_intl_domainname[];
67 #ifdef dgettext
68 /* This is defined as an optimizing macro, so use it. */
69 #define _(msgid) dgettext (_libc_intl_domainname, (msgid))
70 #else
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))
74 #endif
76 #endif
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
86 #endif
88 #ifndef __SYMBOL_PREFIX
89 #ifdef NO_UNDERSCORES
90 #define __SYMBOL_PREFIX
91 #else
92 #define __SYMBOL_PREFIX "_"
93 #endif
94 #endif
96 #ifndef C_SYMBOL_NAME
97 #ifdef NO_UNDERSCORES
98 #define C_SYMBOL_NAME(name) name
99 #else
100 #define C_SYMBOL_NAME(name) _##name
101 #endif
102 #endif
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)
110 #ifdef ASSEMBLER
111 #define strong_alias(original, alias) strong_alias_asm (original, alias)
112 #else
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);
116 #endif
117 #else
118 #define strong_alias_asm(original, alias) \
119 ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME (alias); \
120 C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original)
121 #ifdef ASSEMBLER
122 #define strong_alias(original, alias) strong_alias_asm (original, alias)
123 #else
124 #define strong_alias(original, alias) \
125 asm (__string_1 (ASM_GLOBAL_DIRECTIVE) " " __SYMBOL_PREFIX #alias "\n" \
126 __SYMBOL_PREFIX #alias " = " __SYMBOL_PREFIX #original);
127 #endif
128 #endif
130 /* Helper macros used above. */
131 #define __string_1(x) __string_0(x)
132 #define __string_0(x) #x
135 #ifdef HAVE_WEAK_SYMBOLS
137 #ifdef ASSEMBLER
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 */
182 #else
183 #define weak_alias(original, alias) strong_alias(original, alias)
184 #define weak_extern(symbol) /* Do nothing; the ref will be strong. */
185 #endif
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. */
193 #undef strong_alias
194 #define strong_alias(name, aliasname) \
195 extern __typeof (name) aliasname __attribute__ ((alias (#name)));
197 #ifdef HAVE_WEAK_SYMBOLS
198 #undef weak_alias
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))
206 #endif /* HAVE_WEAK_SYMBOLS. */
207 #endif /* Not ASSEMBLER, and GCC 2.7 or later. */
210 #ifndef weak_function
211 /* If we do not have the __attribute__ ((weak)) syntax, there is no way we
212 can define functions as weak symbols. The compiler will emit a `.globl'
213 directive for the function symbol, and a `.weak' directive in addition
214 will produce an error from the assembler. */
215 #define weak_function /* empty */
216 #endif
219 /* When a reference to SYMBOL is encountered, the linker will emit a
220 warning message MSG. */
221 #ifdef HAVE_GNU_LD
222 #ifdef HAVE_ELF
223 #define link_warning(symbol, msg) \
224 static const char __evoke_link_warning_##symbol[] \
225 __attribute__ ((section (".gnu.warning." #symbol))) = msg;
226 #else
227 #define link_warning(symbol, msg) \
228 asm(".stabs \"" msg "\",30,0,0,0\n" \
229 ".stabs \"" __SYMBOL_PREFIX #symbol "\",1,0,0,0\n");
230 #endif
231 #else
232 /* We will never be heard; they will all die horribly. */
233 #define link_warning(symbol, msg)
234 #endif
236 /* A canned warning for sysdeps/stub functions. */
237 #define stub_warning(name) \
238 link_warning (name, \
239 "warning: " #name " is not implemented and will always fail")
245 #ifdef HAVE_GNU_LD
247 /* Symbol set support macros. */
249 #ifdef HAVE_ELF
251 /* Make SYMBOL, which is in the text segment, an element of SET. */
252 #define text_set_element(set, symbol) _elf_set_element(set, symbol)
253 /* Make SYMBOL, which is in the data segment, an element of SET. */
254 #define data_set_element(set, symbol) _elf_set_element(set, symbol)
255 /* Make SYMBOL, which is in the bss segment, an element of SET. */
256 #define bss_set_element(set, symbol) _elf_set_element(set, symbol)
258 /* These are all done the same way in ELF.
259 There is a new section created for each set. */
260 #ifdef PIC
261 /* When building a shared library, make the set section writable,
262 because it will need to be relocated at run time anyway. */
263 #define _elf_set_element(set, symbol) \
264 static const void *__elf_set_##set##_element_##symbol##__ \
265 __attribute__ ((unused, section (#set))) = &(symbol)
266 #else
267 #define _elf_set_element(set, symbol) \
268 static const void *const __elf_set_##set##_element_##symbol##__ \
269 __attribute__ ((unused, section (#set))) = &(symbol)
270 #endif
272 /* Define SET as a symbol set. This may be required (it is in a.out) to
273 be able to use the set's contents. */
274 #define symbol_set_define(set) symbol_set_declare(set)
276 /* Declare SET for use in this module, if defined in another module. */
277 #define symbol_set_declare(set) \
278 extern void *const __start_##set __attribute__ ((__weak__)); \
279 extern void *const __stop_##set __attribute__ ((__weak__)); \
280 weak_extern (__start_##set) weak_extern (__stop_##set)
282 /* Return a pointer (void *const *) to the first element of SET. */
283 #define symbol_set_first_element(set) (&__start_##set)
285 /* Return true iff PTR (a void *const *) has been incremented
286 past the last element in SET. */
287 #define symbol_set_end_p(set, ptr) ((ptr) >= &__stop_##set)
289 #else /* Not ELF: a.out. */
291 #define text_set_element(set, symbol) \
292 asm(".stabs \"" __SYMBOL_PREFIX #set "\",23,0,0," __SYMBOL_PREFIX #symbol)
293 #define data_set_element(set, symbol) \
294 asm(".stabs \"" __SYMBOL_PREFIX #set "\",25,0,0," __SYMBOL_PREFIX #symbol)
295 #define bss_set_element(set, symbol) ?error Must use initialized data.
296 #define symbol_set_define(set) void *const (set)[1];
297 #define symbol_set_declare(set) extern void *const (set)[1];
299 #define symbol_set_first_element(set) &(set)[1]
300 #define symbol_set_end_p(set, ptr) (*(ptr) == 0)
302 #endif /* ELF. */
303 #endif /* Have GNU ld. */
305 #endif /* libc-symbols.h */