Wed Feb 14 01:08:58 1996 Roland McGrath <roland@charlie-brown.gnu.ai.mit.edu>
[platform/upstream/glibc.git] / libc-symbols.h
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.
5
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.
10
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.
15
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.  */
20
21 #ifndef _LIBC_SYMBOLS_H
22 #define _LIBC_SYMBOLS_H
23
24 /* This file's macros are included implicitly in the compilation of every
25    file in the C library by -imacros.
26
27    We include config.h which is generated by configure.
28    It should define for us the following symbols:
29
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.
35
36    * HAVE_WEAK_SYMBOLS if weak symbols are available in the assembler and
37    linker being used.  Set by --with-weak-symbols.
38
39    */
40 #include <config.h>
41
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
49 /*
50 \f
51 */
52
53 #ifndef ASSEMBLER
54
55 /* Define the macros `_' and `N_' for conveniently marking translatable
56    strings in the libc source code.  */
57
58 #define N_(msgid)       msgid
59
60 #include <libintl.h>
61 extern const char _libc_intl_domainname[];
62
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
71
72 #endif
73
74 /*
75 \f
76 */
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.  */
81
82 #ifndef HAVE_WEAK_SYMBOLS
83 #if defined (HAVE_ELF) || defined (HAVE_GNU_LD)
84 #define HAVE_WEAK_SYMBOLS
85 #endif
86 #endif
87
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
100
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
108
109
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
134
135 /* Helper macros used above.  */
136 #define __string_1(x) __string_0(x)
137 #define __string_0(x) #x
138
139
140 #ifdef HAVE_WEAK_SYMBOLS
141 #ifdef ASSEMBLER
142
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)
148
149 /* Declare SYMBOL to be weak.  */
150 #define weak_symbol(symbol)     .weak C_SYMBOL_NAME (symbol)
151
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
162
163
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)));
172
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.  */
182
183
184
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
201
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")
206
207 /*
208 \f
209 */
210
211 #ifdef HAVE_GNU_LD
212
213 /* Symbol set support macros.  */
214
215 #ifdef HAVE_ELF
216
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)
223
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
237
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)
241
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)
248
249 /* Return a pointer (void *const *) to the first element of SET.  */
250 #define symbol_set_first_element(set)   (&__start_##set)
251
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)
255
256 #else   /* Not ELF: a.out.  */
257
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];
265
266 #define symbol_set_first_element(set)   &(set)[1]
267 #define symbol_set_end_p(set, ptr)      (*(ptr) == 0)
268
269 #endif  /* ELF.  */
270 #endif  /* Have GNU ld.  */
271
272 #endif /* libc-symbols.h */