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,1997,1998,2000,2001 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 Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the 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    Lesser General Public License for more details.
15 
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20 
21 #ifndef _LIBC_SYMBOLS_H
22 #define _LIBC_SYMBOLS_H	1
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_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'.
37 
38    */
39 
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
48 
49 /* Enable declarations of GNU extensions, since we are compiling them.  */
50 #define _GNU_SOURCE	1
51 /* And we also need the data for the reentrant functions.  */
52 #define _REENTRANT	1
53 
54 #include <config.h>
55 
56 /* The symbols in all the user (non-_) macros are C symbols.
57    HAVE_GNU_LD without HAVE_ELF implies a.out.  */
58 
59 #if defined HAVE_ASM_WEAK_DIRECTIVE || defined HAVE_ASM_WEAKEXT_DIRECTIVE
60 # define HAVE_WEAK_SYMBOLS
61 #endif
62 
63 #ifndef __SYMBOL_PREFIX
64 # ifdef NO_UNDERSCORES
65 #  define __SYMBOL_PREFIX
66 # else
67 #  define __SYMBOL_PREFIX "_"
68 # endif
69 #endif
70 
71 #ifndef C_SYMBOL_NAME
72 # ifdef NO_UNDERSCORES
73 #  define C_SYMBOL_NAME(name) name
74 # else
75 #  define C_SYMBOL_NAME(name) _##name
76 # endif
77 #endif
78 
79 #ifndef ASM_LINE_SEP
80 # define ASM_LINE_SEP ;
81 #endif
82 
83 #ifndef C_SYMBOL_DOT_NAME
84 # define C_SYMBOL_DOT_NAME(name) .##name
85 #endif
86 
87 #ifndef __ASSEMBLER__
88 /* GCC understands weak symbols and aliases; use its interface where
89    possible, instead of embedded assembly language.  */
90 
91 /* Define ALIASNAME as a strong alias for NAME.  */
92 # define strong_alias(name, aliasname) _strong_alias(name, aliasname)
93 # define _strong_alias(name, aliasname) \
94   extern __typeof (name) aliasname __attribute__ ((alias (#name)));
95 
96 /* This comes between the return type and function name in
97    a function definition to make that definition weak.  */
98 # define weak_function __attribute__ ((weak))
99 # define weak_const_function __attribute__ ((weak, __const__))
100 
101 # ifdef HAVE_WEAK_SYMBOLS
102 
103 /* Define ALIASNAME as a weak alias for NAME.
104    If weak aliases are not available, this defines a strong alias.  */
105 #  define weak_alias(name, aliasname) _weak_alias (name, aliasname)
106 #  define _weak_alias(name, aliasname) \
107   extern __typeof (name) aliasname __attribute__ ((weak, alias (#name)));
108 
109 /* Declare SYMBOL as weak undefined symbol (resolved to 0 if not defined).  */
110 #  define weak_extern(symbol) _weak_extern (symbol)
111 #  ifdef HAVE_ASM_WEAKEXT_DIRECTIVE
112 #   define _weak_extern(symbol) asm (".weakext " __SYMBOL_PREFIX #symbol);
113 #  else
114 #   define _weak_extern(symbol)    asm (".weak " __SYMBOL_PREFIX #symbol);
115 #  endif
116 
117 # else
118 
119 #  define weak_alias(name, aliasname) strong_alias(name, aliasname)
120 #  define weak_extern(symbol) /* Nothing. */
121 
122 # endif
123 
124 #else /* __ASSEMBLER__ */
125 
126 # ifdef HAVE_ASM_SET_DIRECTIVE
127 #  define strong_alias(original, alias)		\
128   ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME (alias) ASM_LINE_SEP	\
129   .set C_SYMBOL_NAME (alias),C_SYMBOL_NAME (original)
130 # else
131 #  ifdef HAVE_ASM_GLOBAL_DOT_NAME
132 #   define strong_alias(original, alias)	\
133   ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME (alias) ASM_LINE_SEP	\
134   C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original) ASM_LINE_SEP	\
135   ASM_GLOBAL_DIRECTIVE C_SYMBOL_DOT_NAME (alias) ASM_LINE_SEP	\
136   C_SYMBOL_DOT_NAME (alias) = C_SYMBOL_DOT_NAME (original)
137 #  else
138 #   define strong_alias(original, alias)	\
139   ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME (alias) ASM_LINE_SEP	\
140   C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original)
141 #  endif
142 # endif
143 
144 # ifdef HAVE_WEAK_SYMBOLS
145 #  ifdef HAVE_ASM_WEAKEXT_DIRECTIVE
146 #   define weak_alias(original, alias)	\
147   .weakext C_SYMBOL_NAME (alias), C_SYMBOL_NAME (original)
148 #   define weak_extern(symbol)	\
149   .weakext C_SYMBOL_NAME (symbol)
150 
151 #  else /* ! HAVE_ASM_WEAKEXT_DIRECTIVE */
152 
153 #   ifdef HAVE_ASM_GLOBAL_DOT_NAME
154 #    define weak_alias(original, alias)	\
155   .weak C_SYMBOL_NAME (alias) ASM_LINE_SEP			\
156   C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original) ASM_LINE_SEP	\
157   ASM_GLOBAL_DIRECTIVE C_SYMBOL_DOT_NAME (alias) ASM_LINE_SEP	\
158   C_SYMBOL_DOT_NAME (alias) = C_SYMBOL_DOT_NAME (original)
159 #   else
160 #    define weak_alias(original, alias)	\
161   .weak C_SYMBOL_NAME (alias) ASM_LINE_SEP	\
162   C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original)
163 #   endif
164 
165 #   define weak_extern(symbol)	\
166   .weak C_SYMBOL_NAME (symbol)
167 
168 #  endif /* ! HAVE_ASM_WEAKEXT_DIRECTIVE */
169 
170 # else /* ! HAVE_WEAK_SYMBOLS */
171 
172 #  define weak_alias(original, alias) strong_alias(original, alias)
173 #  define weak_extern(symbol) /* Nothing */
174 # endif /* ! HAVE_WEAK_SYMBOLS */
175 
176 #endif /* __ASSEMBLER__ */
177 
178 /* On some platforms we can make internal function calls (i.e., calls of
179    functions not exported) a bit faster by using a different calling
180    convention.  */
181 #ifndef internal_function
182 # define internal_function	/* empty */
183 #endif
184 
185 /* Prepare for the case that `__builtin_expect' is not available.  */
186 #ifndef HAVE_BUILTIN_EXPECT
187 # define __builtin_expect(expr, val) (expr)
188 #endif
189 
190 /* Determine the return address.  */
191 #define RETURN_ADDRESS(nr) \
192   __builtin_extract_return_addr (__builtin_return_address (nr))
193 
194 /* When a reference to SYMBOL is encountered, the linker will emit a
195    warning message MSG.  */
196 #ifdef HAVE_GNU_LD
197 # ifdef HAVE_ELF
198 
199 /* We want the .gnu.warning.SYMBOL section to be unallocated.  */
200 #  ifdef HAVE_ASM_PREVIOUS_DIRECTIVE
201 #   define __make_section_unallocated(section_string)	\
202   asm (".section " section_string "\n\t.previous");
203 #  elif defined HAVE_ASM_POPSECTION_DIRECTIVE
204 #   define __make_section_unallocated(section_string)	\
205   asm (".pushsection " section_string "\n\t.popsection");
206 #  else
207 #   define __make_section_unallocated(section_string)
208 #  endif
209 
210 /* Tacking on "\n\t#" to the section name makes gcc put it's bogus
211    section attributes on what looks like a comment to the assembler.  */
212 #  ifdef HAVE_SECTION_QUOTES
213 #   define link_warning(symbol, msg) \
214   __make_section_unallocated (".gnu.warning." #symbol) \
215   static const char __evoke_link_warning_##symbol[]	\
216     __attribute__ ((section (".gnu.warning." #symbol "\"\n\t#\""))) = msg;
217 #  else
218 #   define link_warning(symbol, msg) \
219   __make_section_unallocated (".gnu.warning." #symbol) \
220   static const char __evoke_link_warning_##symbol[]	\
221     __attribute__ ((section (".gnu.warning." #symbol "\n\t#"))) = msg;
222 #  endif
223 # else /* Not ELF: a.out */
224 #  ifdef HAVE_XCOFF
225 /* XCOFF does not support .stabs.
226    The native aix linker will remove the .stab and .stabstr sections
227    The gnu linker will have a fatal error if there is a relocation for
228    symbol in the .stab section.  Silently disable this macro.  */
229 #   define link_warning(symbol, msg)
230 #  else
231 #   define link_warning(symbol, msg)		\
232      asm (".stabs \"" msg "\",30,0,0,0\n\t"	\
233           ".stabs \"" __SYMBOL_PREFIX #symbol "\",1,0,0,0\n");
234 #  endif /* XCOFF */
235 # endif
236 #else
237 /* We will never be heard; they will all die horribly.  */
238 # define link_warning(symbol, msg)
239 #endif
240 
241 /* A canned warning for sysdeps/stub functions.  */
242 #define	stub_warning(name) \
243   link_warning (name, \
244 		"warning: " #name " is not implemented and will always fail")
245 
246 /*
247 
248 */
249 
250 #ifdef HAVE_GNU_LD
251 
252 /* Symbol set support macros.  */
253 
254 # ifdef HAVE_ELF
255 
256 /* Make SYMBOL, which is in the text segment, an element of SET.  */
257 #  define text_set_element(set, symbol)	_elf_set_element(set, symbol)
258 /* Make SYMBOL, which is in the data segment, an element of SET.  */
259 #  define data_set_element(set, symbol)	_elf_set_element(set, symbol)
260 /* Make SYMBOL, which is in the bss segment, an element of SET.  */
261 #  define bss_set_element(set, symbol)	_elf_set_element(set, symbol)
262 
263 /* These are all done the same way in ELF.
264    There is a new section created for each set.  */
265 #  ifdef SHARED
266 /* When building a shared library, make the set section writable,
267    because it will need to be relocated at run time anyway.  */
268 #   define _elf_set_element(set, symbol) \
269   static const void *__elf_set_##set##_element_##symbol##__ \
270     __attribute__ ((unused, section (#set))) = &(symbol)
271 #  else
272 #   define _elf_set_element(set, symbol) \
273   static const void *const __elf_set_##set##_element_##symbol##__ \
274     __attribute__ ((unused, section (#set))) = &(symbol)
275 #  endif
276 
277 /* Define SET as a symbol set.  This may be required (it is in a.out) to
278    be able to use the set's contents.  */
279 #  define symbol_set_define(set)	symbol_set_declare(set)
280 
281 /* Declare SET for use in this module, if defined in another module.  */
282 #  define symbol_set_declare(set) \
283   extern void *const __start_##set __attribute__ ((__weak__));		\
284   extern void *const __stop_##set __attribute__ ((__weak__));		\
285   weak_extern (__start_##set) weak_extern (__stop_##set)
286 
287 /* Return a pointer (void *const *) to the first element of SET.  */
288 #  define symbol_set_first_element(set)	(&__start_##set)
289 
290 /* Return true iff PTR (a void *const *) has been incremented
291    past the last element in SET.  */
292 #  define symbol_set_end_p(set, ptr)	((ptr) >= &__stop_##set)
293 
294 # else	/* Not ELF: a.out.  */
295 
296 #  ifdef HAVE_XCOFF
297 /* XCOFF does not support .stabs.
298    The native aix linker will remove the .stab and .stabstr sections
299    The gnu linker will have a fatal error if there is a relocation for
300    symbol in the .stab section.  Silently disable these macros.  */
301 #   define text_set_element(set, symbol)
302 #   define data_set_element(set, symbol)
303 #   define bss_set_element(set, symbol)
304 #  else
305 #   define text_set_element(set, symbol)	\
306     asm (".stabs \"" __SYMBOL_PREFIX #set "\",23,0,0," __SYMBOL_PREFIX #symbol)
307 #   define data_set_element(set, symbol)	\
308     asm (".stabs \"" __SYMBOL_PREFIX #set "\",25,0,0," __SYMBOL_PREFIX #symbol)
309 #   define bss_set_element(set, symbol)	?error Must use initialized data.
310 #  endif /* XCOFF */
311 #  define symbol_set_define(set)	void *const (set)[1];
312 #  define symbol_set_declare(set)	extern void *const (set)[1];
313 
314 #  define symbol_set_first_element(set)	&(set)[1]
315 #  define symbol_set_end_p(set, ptr)	(*(ptr) == 0)
316 
317 # endif	/* ELF.  */
318 #else
319 /* We cannot do anything in generial.  */
320 # define text_set_element(set, symbol) asm ("")
321 # define data_set_element(set, symbol) asm ("")
322 # define bss_set_element(set, symbol) asm ("")
323 # define symbol_set_define(set)		void *const (set)[1];
324 # define symbol_set_declare(set)	extern void *const (set)[1];
325 
326 # define symbol_set_first_element(set)	&(set)[1]
327 # define symbol_set_end_p(set, ptr)	(*(ptr) == 0)
328 #endif	/* Have GNU ld.  */
329 
330 #if DO_VERSIONING
331 # define symbol_version(real, name, version) \
332      _symbol_version(real, name, version)
333 # define default_symbol_version(real, name, version) \
334      _default_symbol_version(real, name, version)
335 # ifdef __ASSEMBLER__
336 #  define _symbol_version(real, name, version) \
337      .symver real, name##@##version
338 #  define _default_symbol_version(real, name, version) \
339      .symver real, name##@##@##version
340 # else
341 #  define _symbol_version(real, name, version) \
342      __asm__ (".symver " #real "," #name "@" #version)
343 #  define _default_symbol_version(real, name, version) \
344      __asm__ (".symver " #real "," #name "@@" #version)
345 # endif
346 #else
347 # define symbol_version(real, name, version)
348 # define default_symbol_version(real, name, version) \
349   strong_alias(real, name)
350 #endif
351 
352 #endif /* libc-symbols.h */
353