Sun Dec 17 15:56:35 1995 Miles Bader <miles@gnu.ai.mit.edu>
[glibc.git] / libc-symbols.h
blob9d6c2f8f3459c555204b096569df174e6d46b0e8
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 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_GNU_LD if using GNU ld, with support for weak symbols in a.out,
31 and for symbol set and warning messages extensions in a.out and ELF.
32 This implies HAVE_WEAK_SYMBOLS; set by --with-gnu-ld.
33 * HAVE_ELF if using ELF, which supports weak symbols.
34 This implies HAVE_WEAK_SYMBOLS; set by --with-elf.
36 * HAVE_WEAK_SYMBOLS if weak symbols are available in the assembler and
37 linker being used. Set by --with-weak-symbols.
40 #include <config.h>
42 /* This is defined for the compilation of all C library code.
43 features.h tests this to avoid inclusion of stubs.h while
44 compiling the library, before stubs.h has been generated.
45 Some library code that is shared with other packages also
46 tests this symbol to see if it is being compiled as part
47 of the C library. */
48 #define _LIBC 1
53 #ifndef ASSEMBLER
55 /* Define the macros `_' and `N_' for conveniently marking translatable
56 strings in the libc source code. */
58 #define N_(msgid) msgid
60 #include <libintl.h>
61 extern const char _libc_intl_domainname[];
63 #ifdef dgettext
64 /* This is defined as an optimizing macro, so use it. */
65 #define _(msgid) dgettext (_libc_intl_domainname, (msgid))
66 #else
67 /* Be sure to use only the __ name when `dgettext' is a plain function
68 instead of an optimizing macro. */
69 #define _(msgid) __dgettext (_libc_intl_domainname, (msgid))
70 #endif
72 #endif
77 /* The symbols in all the user (non-_) macros are C symbols. Predefined
78 should be HAVE_WEAK_SYMBOLS and/or HAVE_ELF and/or HAVE_GNU_LD.
79 HAVE_WEAK_SYMBOLS is implied by the other two. HAVE_GNU_LD without
80 HAVE_ELF implies a.out. */
82 #ifndef HAVE_WEAK_SYMBOLS
83 #if defined (HAVE_ELF) || defined (HAVE_GNU_LD)
84 #define HAVE_WEAK_SYMBOLS
85 #endif
86 #endif
88 #ifndef __SYMBOL_PREFIX
89 #ifdef HAVE_ELF
90 #define NO_UNDERSCORES
91 #else
92 #include <sysdep.h> /* Should define NO_UNDERSCORES. */
93 #endif
94 #ifdef NO_UNDERSCORES
95 #define __SYMBOL_PREFIX
96 #else
97 #define __SYMBOL_PREFIX "_"
98 #endif
99 #endif
101 #ifndef C_SYMBOL_NAME
102 #ifdef NO_UNDERSCORES
103 #define C_SYMBOL_NAME(name) name
104 #else
105 #define C_SYMBOL_NAME(name) _##name
106 #endif
107 #endif
110 /* Define ALIAS as a strong alias for ORIGINAL. */
111 #ifdef HAVE_ASM_SET_DIRECTIVE
112 #define strong_alias_asm(original, alias) \
113 ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME (alias); \
114 .set C_SYMBOL_NAME (alias),C_SYMBOL_NAME (original)
115 #ifdef ASSEMBLER
116 #define strong_alias(original, alias) strong_alias_asm (original, alias)
117 #else
118 #define strong_alias(original, alias) \
119 asm (__string_1 (ASM_GLOBAL_DIRECTIVE) " " __SYMBOL_PREFIX #alias "\n" \
120 ".set " __SYMBOL_PREFIX #alias "," __SYMBOL_PREFIX #original);
121 #endif
122 #else
123 #define strong_alias_asm(original, alias) \
124 ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME (alias); \
125 C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original)
126 #ifdef ASSEMBLER
127 #define strong_alias(original, alias) strong_alias_asm (original, alias)
128 #else
129 #define strong_alias(original, alias) \
130 asm (__string_1 (ASM_GLOBAL_DIRECTIVE) " " __SYMBOL_PREFIX #alias "\n" \
131 __SYMBOL_PREFIX #alias " = " __SYMBOL_PREFIX #original);
132 #endif
133 #endif
135 /* Helper macros used above. */
136 #define __string_1(x) __string_0(x)
137 #define __string_0(x) #x
140 #ifdef HAVE_WEAK_SYMBOLS
141 #ifdef ASSEMBLER
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 .weak C_SYMBOL_NAME (alias); \
147 C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original)
149 /* Declare SYMBOL to be weak. */
150 #define weak_symbol(symbol) .weak C_SYMBOL_NAME (symbol)
152 #else
153 #define weak_symbol(symbol) asm (".weak " __SYMBOL_PREFIX #symbol);
154 #define weak_alias(original, alias) \
155 asm (".weak " __SYMBOL_PREFIX #alias "\n" \
156 __SYMBOL_PREFIX #alias " = " __SYMBOL_PREFIX #original);
157 #endif
158 #else
159 #define weak_alias(original, alias) strong_alias(original, alias)
160 #define weak_symbol(symbol) /* Do nothing. */
161 #endif
164 #if (!defined (ASSEMBLER) && \
165 (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)))
166 /* GCC 2.8 and later has special syntax for weak symbols and aliases.
167 Using that is better when possible, because the compiler and assembler
168 are better clued in to what we are doing. */
169 #undef strong_alias
170 #define strong_alias(name, aliasname) \
171 extern __typeof (name) aliasname __attribute__ ((alias (#name)));
173 #ifdef HAVE_WEAK_SYMBOLS
174 #undef weak_symbol
175 #define weak_symbol(name) \
176 extern __typeof (name) name __attribute__ ((weak));
177 #undef weak_alias
178 #define weak_alias(name, aliasname) \
179 extern __typeof (name) aliasname __attribute__ ((weak, alias (#name)));
180 #endif /* HAVE_WEAK_SYMBOLS. */
181 #endif /* Not ASSEMBLER, and GCC 2.8 or later. */
185 /* When a reference to SYMBOL is encountered, the linker will emit a
186 warning message MSG. */
187 #ifdef HAVE_GNU_LD
188 #ifdef HAVE_ELF
189 #define link_warning(symbol, msg) \
190 static const char __evoke_link_warning_##symbol[] \
191 __attribute__ ((section (".gnu.warning." #symbol))) = msg;
192 #else
193 #define link_warning(symbol, msg) \
194 asm(".stabs \"" msg "\",30,0,0,0\n" \
195 ".stabs \"" __SYMBOL_PREFIX #symbol "\",1,0,0,0\n");
196 #endif
197 #else
198 /* We will never be heard; they will all die horribly. */
199 #define link_warning(symbol, msg)
200 #endif
202 /* A canned warning for sysdeps/stub functions. */
203 #define stub_warning(name) \
204 link_warning (name, \
205 "warning: " #name " is not implemented and will always fail")
211 #ifdef HAVE_GNU_LD
213 /* Symbol set support macros. */
215 #ifdef HAVE_ELF
217 /* Make SYMBOL, which is in the text segment, an element of SET. */
218 #define text_set_element(set, symbol) _elf_set_element(set, symbol)
219 /* Make SYMBOL, which is in the data segment, an element of SET. */
220 #define data_set_element(set, symbol) _elf_set_element(set, symbol)
221 /* Make SYMBOL, which is in the bss segment, an element of SET. */
222 #define bss_set_element(set, symbol) _elf_set_element(set, symbol)
224 /* These are all done the same way in ELF.
225 There is a new section created for each set. */
226 #ifdef PIC
227 /* When building a shared library, make the set section writable,
228 because it will need to be relocated at run time anyway. */
229 #define _elf_set_element(set, symbol) \
230 static const void *__elf_set_##set##_element_##symbol##__ \
231 __attribute__ ((unused, section (#set))) = &(symbol)
232 #else
233 #define _elf_set_element(set, symbol) \
234 static const void *const __elf_set_##set##_element_##symbol##__ \
235 __attribute__ ((unused, section (#set))) = &(symbol)
236 #endif
238 /* Define SET as a symbol set. This may be required (it is in a.out) to
239 be able to use the set's contents. */
240 #define symbol_set_define(set) symbol_set_declare(set)
242 /* Declare SET for use in this module, if defined in another module. */
243 #define symbol_set_declare(set) \
244 extern void *const __start_##set __attribute__ ((__weak__)); \
245 extern void *const __stop_##set __attribute__ ((__weak__)); \
246 /* Gratuitously repeat weak decl, in case using broken GCC (<2.8). */\
247 weak_symbol (__start_##set) weak_symbol (__stop_##set)
249 /* Return a pointer (void *const *) to the first element of SET. */
250 #define symbol_set_first_element(set) (&__start_##set)
252 /* Return true iff PTR (a void *const *) has been incremented
253 past the last element in SET. */
254 #define symbol_set_end_p(set, ptr) ((ptr) >= &__stop_##set)
256 #else /* Not ELF: a.out. */
258 #define text_set_element(set, symbol) \
259 asm(".stabs \"" __SYMBOL_PREFIX #set "\",23,0,0," __SYMBOL_PREFIX #symbol)
260 #define data_set_element(set, symbol) \
261 asm(".stabs \"" __SYMBOL_PREFIX #set "\",25,0,0," __SYMBOL_PREFIX #symbol)
262 #define bss_set_element(set, symbol) ?error Must use initialized data.
263 #define symbol_set_define(set) void *const (set)[1];
264 #define symbol_set_declare(set) extern void *const (set)[1];
266 #define symbol_set_first_element(set) &(set)[1]
267 #define symbol_set_end_p(set, ptr) (*(ptr) == 0)
269 #endif /* ELF. */
270 #endif /* Have GNU ld. */
272 #endif /* libc-symbols.h */