1 # SPDX-FileCopyrightText: 2008 Kevin L. Mitchell <klmitch@mit.edu>
3 # SPDX-License-Identifier: FSFAP
5 # ===========================================================================
6 # https://www.gnu.org/software/autoconf-archive/ax_sys_weak_alias.html
7 # ===========================================================================
15 # Determines whether weak aliases are supported on the system, and if so,
16 # what scheme is used to declare them. Also checks to see if aliases can
17 # cross object file boundaries, as some systems don't permit them to.
19 # Most systems permit something called a "weak alias" or "weak symbol."
20 # These aliases permit a library to provide a stub form of a routine
21 # defined in another library, thus allowing the first library to operate
22 # even if the other library is not linked. This macro will check for
23 # support of weak aliases, figure out what schemes are available, and
24 # determine some characteristics of the weak alias support -- primarily,
25 # whether a weak alias declared in one object file may be referenced from
26 # another object file.
28 # There are four known schemes of declaring weak symbols; each scheme is
29 # checked in turn, and the first one found is preferred. Note that only
30 # one of the mentioned preprocessor macros will be defined!
32 # 1. Function attributes
34 # This scheme was first introduced by the GNU C compiler, and attaches
35 # attributes to particular functions. It is among the easiest to use, and
36 # so is the first one checked. If this scheme is detected, the
37 # preprocessor macro HAVE_SYS_WEAK_ALIAS_ATTRIBUTE will be defined to 1.
38 # This scheme is used as in the following code fragment:
42 # /* Function definition... */
45 # void weakf(int c) __attribute__((weak, alias("__weakf")));
49 # This scheme is in use by many compilers other than the GNU C compiler.
50 # It is also particularly easy to use, and fairly portable -- well, as
51 # portable as these things get. If this scheme is detected first, the
52 # preprocessor macro HAVE_SYS_WEAK_ALIAS_PRAGMA will be defined to 1. This
53 # scheme is used as in the following code fragment:
55 # extern void weakf(int c);
56 # #pragma weak weakf = __weakf
59 # /* Function definition... */
62 # 3. #pragma _HP_SECONDARY_DEF
64 # This scheme appears to be in use by the HP compiler. As it is rather
65 # specialized, this is one of the last schemes checked. If it is the first
66 # one detected, the preprocessor macro HAVE_SYS_WEAK_ALIAS_HPSECONDARY
67 # will be defined to 1. This scheme is used as in the following code
70 # extern void weakf(int c);
71 # #pragma _HP_SECONDARY_DEF __weakf weakf
74 # /* Function definition... */
77 # 4. #pragma _CRI duplicate
79 # This scheme appears to be in use by the Cray compiler. As it is rather
80 # specialized, it too is one of the last schemes checked. If it is the
81 # first one detected, the preprocessor macro
82 # HAVE_SYS_WEAK_ALIAS_CRIDUPLICATE will be defined to 1. This scheme is
83 # used as in the following code fragment:
85 # extern void weakf(int c);
86 # #pragma _CRI duplicate weakf as __weakf
89 # /* Function definition... */
92 # In addition to the preprocessor macros listed above, if any scheme is
93 # found, the preprocessor macro HAVE_SYS_WEAK_ALIAS will also be defined
96 # Once a weak aliasing scheme has been found, a check will be performed to
97 # see if weak aliases are honored across object file boundaries. If they
98 # are, the HAVE_SYS_WEAK_ALIAS_CROSSFILE preprocessor macro is defined to
101 # This Autoconf macro also makes two substitutions. The first, WEAK_ALIAS,
102 # contains the name of the scheme found (one of "attribute", "pragma",
103 # "hpsecondary", or "criduplicate"), or "no" if no weak aliasing scheme
104 # was found. The second, WEAK_ALIAS_CROSSFILE, is set to "yes" or "no"
105 # depending on whether or not weak aliases may cross object file
110 # Copyright (c) 2008 Kevin L. Mitchell <klmitch@mit.edu>
112 # Copying and distribution of this file, with or without modification, are
113 # permitted in any medium without royalty provided the copyright notice
114 # and this notice are preserved. This file is offered as-is, without any
119 AU_ALIAS([KLM_SYS_WEAK_ALIAS], [AX_SYS_WEAK_ALIAS])
120 AC_DEFUN([AX_SYS_WEAK_ALIAS], [
121 # starting point: no aliasing scheme yet...
124 # Figure out what kind of aliasing may be supported...
125 _AX_SYS_WEAK_ALIAS_ATTRIBUTE
126 _AX_SYS_WEAK_ALIAS_PRAGMA
127 _AX_SYS_WEAK_ALIAS_HPSECONDARY
128 _AX_SYS_WEAK_ALIAS_CRIDUPLICATE
130 # Do we actually support aliasing?
131 AC_CACHE_CHECK([how to create weak aliases with $CC],
132 [ax_cv_sys_weak_alias],
133 [ax_cv_sys_weak_alias=$ax_sys_weak_alias])
136 AS_IF([test $ax_cv_sys_weak_alias != no], [
137 AC_DEFINE([HAVE_SYS_WEAK_ALIAS], 1,
138 [Define this if your system can create weak aliases])
141 # Can aliases cross object file boundaries?
142 _AX_SYS_WEAK_ALIAS_CROSSFILE
144 # OK, remember the results
145 AC_SUBST([WEAK_ALIAS], [$ax_cv_sys_weak_alias])
146 AC_SUBST([WEAK_ALIAS_CROSSFILE], [$ax_cv_sys_weak_alias_crossfile])
149 AC_DEFUN([_AX_SYS_WEAK_ALIAS_ATTRIBUTE],
150 [ # Test whether compiler accepts __attribute__ form of weak aliasing
151 AC_CACHE_CHECK([whether $CC accepts function __attribute__((weak,alias()))],
152 [ax_cv_sys_weak_alias_attribute], [
153 # We add -Werror if it's gcc to force an error exit if the weak attribute
155 AS_IF([test $GCC = yes], [
159 # Try linking with a weak alias...
162 void __weakf(int c) {}
163 void weakf(int c) __attribute__((weak, alias("__weakf")));],
165 [ax_cv_sys_weak_alias_attribute=yes],
166 [ax_cv_sys_weak_alias_attribute=no])
168 # Restore original CFLAGS
169 AS_IF([test $GCC = yes], [
170 CFLAGS=$save_CFLAGS])
173 # What was the result of the test?
174 AS_IF([test $ax_sys_weak_alias = no &&
175 test $ax_cv_sys_weak_alias_attribute = yes], [
176 ax_sys_weak_alias=attribute
177 AC_DEFINE([HAVE_SYS_WEAK_ALIAS_ATTRIBUTE], 1,
178 [Define this if weak aliases may be created with __attribute__])
182 AC_DEFUN([_AX_SYS_WEAK_ALIAS_PRAGMA],
183 [ # Test whether compiler accepts #pragma form of weak aliasing
184 AC_CACHE_CHECK([whether $CC supports @%:@pragma weak],
185 [ax_cv_sys_weak_alias_pragma], [
187 # Try linking with a weak alias...
190 extern void weakf(int c);
191 @%:@pragma weak weakf = __weakf
192 void __weakf(int c) {}],
194 [ax_cv_sys_weak_alias_pragma=yes],
195 [ax_cv_sys_weak_alias_pragma=no])
198 # What was the result of the test?
199 AS_IF([test $ax_sys_weak_alias = no &&
200 test $ax_cv_sys_weak_alias_pragma = yes], [
201 ax_sys_weak_alias=pragma
202 AC_DEFINE([HAVE_SYS_WEAK_ALIAS_PRAGMA], 1,
203 [Define this if weak aliases may be created with @%:@pragma weak])
207 AC_DEFUN([_AX_SYS_WEAK_ALIAS_HPSECONDARY],
208 [ # Test whether compiler accepts _HP_SECONDARY_DEF pragma from HP...
209 AC_CACHE_CHECK([whether $CC supports @%:@pragma _HP_SECONDARY_DEF],
210 [ax_cv_sys_weak_alias_hpsecondary], [
212 # Try linking with a weak alias...
215 extern void weakf(int c);
216 @%:@pragma _HP_SECONDARY_DEF __weakf weakf
217 void __weakf(int c) {}],
219 [ax_cv_sys_weak_alias_hpsecondary=yes],
220 [ax_cv_sys_weak_alias_hpsecondary=no])
223 # What was the result of the test?
224 AS_IF([test $ax_sys_weak_alias = no &&
225 test $ax_cv_sys_weak_alias_hpsecondary = yes], [
226 ax_sys_weak_alias=hpsecondary
227 AC_DEFINE([HAVE_SYS_WEAK_ALIAS_HPSECONDARY], 1,
228 [Define this if weak aliases may be created with @%:@pragma _HP_SECONDARY_DEF])
232 AC_DEFUN([_AX_SYS_WEAK_ALIAS_CRIDUPLICATE],
233 [ # Test whether compiler accepts "_CRI duplicate" pragma from Cray
234 AC_CACHE_CHECK([whether $CC supports @%:@pragma _CRI duplicate],
235 [ax_cv_sys_weak_alias_criduplicate], [
237 # Try linking with a weak alias...
240 extern void weakf(int c);
241 @%:@pragma _CRI duplicate weakf as __weakf
242 void __weakf(int c) {}],
244 [ax_cv_sys_weak_alias_criduplicate=yes],
245 [ax_cv_sys_weak_alias_criduplicate=no])
248 # What was the result of the test?
249 AS_IF([test $ax_sys_weak_alias = no &&
250 test $ax_cv_sys_weak_alias_criduplicate = yes], [
251 ax_sys_weak_alias=criduplicate
252 AC_DEFINE([HAVE_SYS_WEAK_ALIAS_CRIDUPLICATE], 1,
253 [Define this if weak aliases may be created with @%:@pragma _CRI duplicate])
257 dnl Note: This macro is modeled closely on AC_LINK_IFELSE, and in fact
258 dnl depends on some implementation details of that macro, particularly
259 dnl its use of _AC_MSG_LOG_CONFTEST to log the failed test program and
260 dnl its use of ac_link for running the linker.
261 AC_DEFUN([_AX_SYS_WEAK_ALIAS_CROSSFILE],
262 [ # Check to see if weak aliases can cross object file boundaries
263 AC_CACHE_CHECK([whether $CC supports weak aliases across object file boundaries],
264 [ax_cv_sys_weak_alias_crossfile], [
265 AS_IF([test $ax_cv_sys_weak_alias = no],
266 [ax_cv_sys_weak_alias_crossfile=no], [
267 dnl Must build our own test files...
268 # conftest1 contains our weak alias definition...
269 cat >conftest1.$ac_ext <<_ACEOF
272 cat confdefs.h >>conftest1.$ac_ext
273 cat >>conftest1.$ac_ext <<_ACEOF
274 /* end confdefs.h. */
276 @%:@ifndef HAVE_SYS_WEAK_ALIAS_ATTRIBUTE
277 extern void weakf(int c);
279 @%:@if defined(HAVE_SYS_WEAK_ALIAS_PRAGMA)
280 @%:@pragma weak weakf = __weakf
281 @%:@elif defined(HAVE_SYS_WEAK_ALIAS_HPSECONDARY)
282 @%:@pragma _HP_SECONDARY_DEF __weakf weakf
283 @%:@elif defined(HAVE_SYS_WEAK_ALIAS_CRIDUPLICATE)
284 @%:@pragma _CRI duplicate weakf as __weakf
286 void __weakf(int c) {}
287 @%:@ifdef HAVE_SYS_WEAK_ALIAS_ATTRIBUTE
288 void weakf(int c) __attribute((weak, alias("__weakf")));
291 # And conftest2 contains our main routine that calls it
292 cat >conftest2.$ac_ext <<_ACEOF
295 cat confdefs.h >> conftest2.$ac_ext
296 cat >>conftest2.$ac_ext <<_ACEOF
297 /* end confdefs.h. */
299 extern void weakf(int c);
307 # We must remove the object files (if any) ourselves...
308 rm -f conftest2.$ac_objext conftest$ac_exeext
310 # Change ac_link to compile *2* files together
312 ac_link=`echo "$ac_link" | \
313 sed -e 's/conftest\(\.\$ac_ext\)/conftest1\1 conftest2\1/'`
314 dnl Substitute our own routine for logging the conftest
315 m4_pushdef([_AC_MSG_LOG_CONFTEST],
316 [echo "$as_me: failed program was:" >&AS_MESSAGE_LOG_FD
317 echo ">>> conftest1.$ac_ext" >&AS_MESSAGE_LOG_FD
318 sed "s/^/| /" conftest1.$ac_ext >&AS_MESSAGE_LOG_FD
319 echo ">>> conftest2.$ac_ext" >&AS_MESSAGE_LOG_FD
320 sed "s/^/| /" conftest2.$ac_ext >&AS_MESSAGE_LOG_FD
322 # Since we created the files ourselves, don't use SOURCE argument
323 AC_LINK_IFELSE(, [ax_cv_sys_weak_alias_crossfile=yes],
324 [ax_cv_sys_weak_alias_crossfile=no])
325 dnl Restore _AC_MSG_LOG_CONFTEST
326 m4_popdef([_AC_MSG_LOG_CONFTEST])dnl
330 # We must remove the object files (if any) and C files ourselves...
331 rm -f conftest1.$ac_ext conftest2.$ac_ext \
332 conftest1.$ac_objext conftest2.$ac_objext
336 # What were the results of the test?
337 AS_IF([test $ax_cv_sys_weak_alias_crossfile = yes], [
338 AC_DEFINE([HAVE_SYS_WEAK_ALIAS_CROSSFILE], 1,
339 [Define this if weak aliases in other files are honored])