1# mmap(2) blacklisting.  Some platforms provide the mmap library routine
2# but don't support all of the features we need from it.
3AC_DEFUN([AC_FUNC_MMAP_BLACKLIST],
4[
5AC_CHECK_HEADER([sys/mman.h],
6		[libffi_header_sys_mman_h=yes], [libffi_header_sys_mman_h=no])
7AC_CHECK_FUNC([mmap], [libffi_func_mmap=yes], [libffi_func_mmap=no])
8if test "$libffi_header_sys_mman_h" != yes \
9 || test "$libffi_func_mmap" != yes; then
10   ac_cv_func_mmap_file=no
11   ac_cv_func_mmap_dev_zero=no
12   ac_cv_func_mmap_anon=no
13else
14   AC_CACHE_CHECK([whether read-only mmap of a plain file works],
15  ac_cv_func_mmap_file,
16  [# Add a system to this blacklist if
17   # mmap(0, stat_size, PROT_READ, MAP_PRIVATE, fd, 0) doesn't return a
18   # memory area containing the same data that you'd get if you applied
19   # read() to the same fd.  The only system known to have a problem here
20   # is VMS, where text files have record structure.
21   case "$host_os" in
22     vms* | ultrix*)
23	ac_cv_func_mmap_file=no ;;
24     *)
25	ac_cv_func_mmap_file=yes;;
26   esac])
27   AC_CACHE_CHECK([whether mmap from /dev/zero works],
28  ac_cv_func_mmap_dev_zero,
29  [# Add a system to this blacklist if it has mmap() but /dev/zero
30   # does not exist, or if mmapping /dev/zero does not give anonymous
31   # zeroed pages with both the following properties:
32   # 1. If you map N consecutive pages in with one call, and then
33   #    unmap any subset of those pages, the pages that were not
34   #    explicitly unmapped remain accessible.
35   # 2. If you map two adjacent blocks of memory and then unmap them
36   #    both at once, they must both go away.
37   # Systems known to be in this category are Windows (all variants),
38   # VMS, and Darwin.
39   case "$host_os" in
40     vms* | cygwin* | pe | mingw* | darwin* | ultrix* | hpux10* | hpux11.00)
41	ac_cv_func_mmap_dev_zero=no ;;
42     *)
43	ac_cv_func_mmap_dev_zero=yes;;
44   esac])
45
46   # Unlike /dev/zero, the MAP_ANON(YMOUS) defines can be probed for.
47   AC_CACHE_CHECK([for MAP_ANON(YMOUS)], ac_cv_decl_map_anon,
48     [AC_COMPILE_IFELSE(
49       [AC_LANG_PROGRAM([[
50#include <sys/types.h>
51#include <sys/mman.h>
52#include <unistd.h>
53#ifndef MAP_ANONYMOUS
54#define MAP_ANONYMOUS MAP_ANON
55#endif
56]],
57[[
58int n = MAP_ANONYMOUS;
59]])],
60     ac_cv_decl_map_anon=yes,
61     ac_cv_decl_map_anon=no)])
62   if test $ac_cv_decl_map_anon = no; then
63     ac_cv_func_mmap_anon=no
64   else
65     AC_CACHE_CHECK([whether mmap with MAP_ANON(YMOUS) works],
66     ac_cv_func_mmap_anon,
67  [# Add a system to this blacklist if it has mmap() and MAP_ANON or
68   # MAP_ANONYMOUS, but using mmap(..., MAP_PRIVATE|MAP_ANONYMOUS, -1, 0)
69   # doesn't give anonymous zeroed pages with the same properties listed
70   # above for use of /dev/zero.
71   # Systems known to be in this category are Windows, VMS, and SCO Unix.
72   case "$host_os" in
73     vms* | cygwin* | pe | mingw* | sco* | udk* )
74	ac_cv_func_mmap_anon=no ;;
75     *)
76	ac_cv_func_mmap_anon=yes;;
77   esac])
78   fi
79fi
80
81if test $ac_cv_func_mmap_file = yes; then
82  AC_DEFINE(HAVE_MMAP_FILE, 1,
83	    [Define if read-only mmap of a plain file works.])
84fi
85if test $ac_cv_func_mmap_dev_zero = yes; then
86  AC_DEFINE(HAVE_MMAP_DEV_ZERO, 1,
87	    [Define if mmap of /dev/zero works.])
88fi
89if test $ac_cv_func_mmap_anon = yes; then
90  AC_DEFINE(HAVE_MMAP_ANON, 1,
91	    [Define if mmap with MAP_ANON(YMOUS) works.])
92fi
93])
94
95dnl ----------------------------------------------------------------------
96dnl This whole bit snagged from libstdc++-v3, via libatomic.
97
98dnl
99dnl LIBFFI_ENABLE
100dnl    (FEATURE, DEFAULT, HELP-ARG, HELP-STRING)
101dnl    (FEATURE, DEFAULT, HELP-ARG, HELP-STRING, permit a|b|c)
102dnl    (FEATURE, DEFAULT, HELP-ARG, HELP-STRING, SHELL-CODE-HANDLER)
103dnl
104dnl See docs/html/17_intro/configury.html#enable for documentation.
105dnl
106m4_define([LIBFFI_ENABLE],[dnl
107m4_define([_g_switch],[--enable-$1])dnl
108m4_define([_g_help],[AS_HELP_STRING(_g_switch$3,[$4 @<:@default=$2@:>@])])dnl
109 AC_ARG_ENABLE($1,_g_help,
110  m4_bmatch([$5],
111   [^permit ],
112     [[
113      case "$enableval" in
114       m4_bpatsubst([$5],[permit ])) ;;
115       *) AC_MSG_ERROR(Unknown argument to enable/disable $1) ;;
116          dnl Idea for future:  generate a URL pointing to
117          dnl "onlinedocs/configopts.html#whatever"
118      esac
119     ]],
120   [^$],
121     [[
122      case "$enableval" in
123       yes|no) ;;
124       *) AC_MSG_ERROR(Argument to enable/disable $1 must be yes or no) ;;
125      esac
126     ]],
127   [[$5]]),
128  [enable_]m4_bpatsubst([$1],-,_)[=][$2])
129m4_undefine([_g_switch])dnl
130m4_undefine([_g_help])dnl
131])
132
133dnl
134dnl If GNU ld is in use, check to see if tricky linker opts can be used.  If
135dnl the native linker is in use, all variables will be defined to something
136dnl safe (like an empty string).
137dnl
138dnl Defines:
139dnl  SECTION_LDFLAGS='-Wl,--gc-sections' if possible
140dnl  OPT_LDFLAGS='-Wl,-O1' if possible
141dnl  LD (as a side effect of testing)
142dnl Sets:
143dnl  with_gnu_ld
144dnl  libat_ld_is_gold (possibly)
145dnl  libat_gnu_ld_version (possibly)
146dnl
147dnl The last will be a single integer, e.g., version 1.23.45.0.67.89 will
148dnl set libat_gnu_ld_version to 12345.  Zeros cause problems.
149dnl
150AC_DEFUN([LIBFFI_CHECK_LINKER_FEATURES], [
151  # If we're not using GNU ld, then there's no point in even trying these
152  # tests.  Check for that first.  We should have already tested for gld
153  # by now (in libtool), but require it now just to be safe...
154  test -z "$SECTION_LDFLAGS" && SECTION_LDFLAGS=''
155  test -z "$OPT_LDFLAGS" && OPT_LDFLAGS=''
156  AC_REQUIRE([LT_PATH_LD])
157  AC_REQUIRE([AC_PROG_AWK])
158
159  # The name set by libtool depends on the version of libtool.  Shame on us
160  # for depending on an impl detail, but c'est la vie.  Older versions used
161  # ac_cv_prog_gnu_ld, but now it's lt_cv_prog_gnu_ld, and is copied back on
162  # top of with_gnu_ld (which is also set by --with-gnu-ld, so that actually
163  # makes sense).  We'll test with_gnu_ld everywhere else, so if that isn't
164  # set (hence we're using an older libtool), then set it.
165  if test x${with_gnu_ld+set} != xset; then
166    if test x${ac_cv_prog_gnu_ld+set} != xset; then
167      # We got through "ac_require(ac_prog_ld)" and still not set?  Huh?
168      with_gnu_ld=no
169    else
170      with_gnu_ld=$ac_cv_prog_gnu_ld
171    fi
172  fi
173
174  # Start by getting the version number.  I think the libtool test already
175  # does some of this, but throws away the result.
176  libat_ld_is_gold=no
177  if $LD --version 2>/dev/null | grep 'GNU gold'> /dev/null 2>&1; then
178    libat_ld_is_gold=yes
179  fi
180  changequote(,)
181  ldver=`$LD --version 2>/dev/null |
182         sed -e 's/GNU gold /GNU ld /;s/GNU ld version /GNU ld /;s/GNU ld ([^)]*) /GNU ld /;s/GNU ld \([0-9.][0-9.]*\).*/\1/; q'`
183  changequote([,])
184  libat_gnu_ld_version=`echo $ldver | \
185         $AWK -F. '{ if (NF<3) [$]3=0; print ([$]1*100+[$]2)*100+[$]3 }'`
186
187  # Set --gc-sections.
188  if test "$with_gnu_ld" = "notbroken"; then
189    # GNU ld it is!  Joy and bunny rabbits!
190
191    # All these tests are for C++; save the language and the compiler flags.
192    # Need to do this so that g++ won't try to link in libstdc++
193    ac_test_CFLAGS="${CFLAGS+set}"
194    ac_save_CFLAGS="$CFLAGS"
195    CFLAGS='-x c++  -Wl,--gc-sections'
196
197    # Check for -Wl,--gc-sections
198    # XXX This test is broken at the moment, as symbols required for linking
199    # are now in libsupc++ (not built yet).  In addition, this test has
200    # cored on solaris in the past.  In addition, --gc-sections doesn't
201    # really work at the moment (keeps on discarding used sections, first
202    # .eh_frame and now some of the glibc sections for iconv).
203    # Bzzzzt.  Thanks for playing, maybe next time.
204    AC_MSG_CHECKING([for ld that supports -Wl,--gc-sections])
205    AC_RUN_IFELSE(
206      [AC_LANG_SOURCE(
207        [[
208           int main(void)
209           {
210              try { throw 1; }
211              catch (...) { };
212              return 0;
213           }
214        ]])],
215    [ac_sectionLDflags=yes],
216    [ac_sectionLDflags=no],
217    [ac_sectionLDflags=yes])
218    if test "$ac_test_CFLAGS" = set; then
219      CFLAGS="$ac_save_CFLAGS"
220    else
221      # this is the suspicious part
222      CFLAGS=''
223    fi
224    if test "$ac_sectionLDflags" = "yes"; then
225      SECTION_LDFLAGS="-Wl,--gc-sections $SECTION_LDFLAGS"
226    fi
227    AC_MSG_RESULT($ac_sectionLDflags)
228  fi
229
230  # Set linker optimization flags.
231  if test x"$with_gnu_ld" = x"yes"; then
232    OPT_LDFLAGS="-Wl,-O1 $OPT_LDFLAGS"
233  fi
234
235  AC_SUBST(SECTION_LDFLAGS)
236  AC_SUBST(OPT_LDFLAGS)
237])
238
239
240dnl
241dnl If GNU ld is in use, check to see if tricky linker opts can be used.  If
242dnl the native linker is in use, all variables will be defined to something
243dnl safe (like an empty string).
244dnl
245dnl Defines:
246dnl  SECTION_LDFLAGS='-Wl,--gc-sections' if possible
247dnl  OPT_LDFLAGS='-Wl,-O1' if possible
248dnl  LD (as a side effect of testing)
249dnl Sets:
250dnl  with_gnu_ld
251dnl  libat_ld_is_gold (possibly)
252dnl  libat_gnu_ld_version (possibly)
253dnl
254dnl The last will be a single integer, e.g., version 1.23.45.0.67.89 will
255dnl set libat_gnu_ld_version to 12345.  Zeros cause problems.
256dnl
257AC_DEFUN([LIBFFI_CHECK_LINKER_FEATURES], [
258  # If we're not using GNU ld, then there's no point in even trying these
259  # tests.  Check for that first.  We should have already tested for gld
260  # by now (in libtool), but require it now just to be safe...
261  test -z "$SECTION_LDFLAGS" && SECTION_LDFLAGS=''
262  test -z "$OPT_LDFLAGS" && OPT_LDFLAGS=''
263  AC_REQUIRE([LT_PATH_LD])
264  AC_REQUIRE([AC_PROG_AWK])
265
266  # The name set by libtool depends on the version of libtool.  Shame on us
267  # for depending on an impl detail, but c'est la vie.  Older versions used
268  # ac_cv_prog_gnu_ld, but now it's lt_cv_prog_gnu_ld, and is copied back on
269  # top of with_gnu_ld (which is also set by --with-gnu-ld, so that actually
270  # makes sense).  We'll test with_gnu_ld everywhere else, so if that isn't
271  # set (hence we're using an older libtool), then set it.
272  if test x${with_gnu_ld+set} != xset; then
273    if test x${ac_cv_prog_gnu_ld+set} != xset; then
274      # We got through "ac_require(ac_prog_ld)" and still not set?  Huh?
275      with_gnu_ld=no
276    else
277      with_gnu_ld=$ac_cv_prog_gnu_ld
278    fi
279  fi
280
281  # Start by getting the version number.  I think the libtool test already
282  # does some of this, but throws away the result.
283  libat_ld_is_gold=no
284  if $LD --version 2>/dev/null | grep 'GNU gold'> /dev/null 2>&1; then
285    libat_ld_is_gold=yes
286  fi
287  libat_ld_is_lld=no
288  if $LD --version 2>/dev/null | grep 'LLD '> /dev/null 2>&1; then
289    libat_ld_is_lld=yes
290  fi
291  changequote(,)
292  ldver=`$LD --version 2>/dev/null |
293         sed -e 's/GNU gold /GNU ld /;s/GNU ld version /GNU ld /;s/GNU ld ([^)]*) /GNU ld /;s/GNU ld \([0-9.][0-9.]*\).*/\1/; q'`
294  changequote([,])
295  libat_gnu_ld_version=`echo $ldver | \
296         $AWK -F. '{ if (NF<3) [$]3=0; print ([$]1*100+[$]2)*100+[$]3 }'`
297
298  # Set --gc-sections.
299  if test "$with_gnu_ld" = "notbroken"; then
300    # GNU ld it is!  Joy and bunny rabbits!
301
302    # All these tests are for C++; save the language and the compiler flags.
303    # Need to do this so that g++ won't try to link in libstdc++
304    ac_test_CFLAGS="${CFLAGS+set}"
305    ac_save_CFLAGS="$CFLAGS"
306    CFLAGS='-x c++  -Wl,--gc-sections'
307
308    # Check for -Wl,--gc-sections
309    # XXX This test is broken at the moment, as symbols required for linking
310    # are now in libsupc++ (not built yet).  In addition, this test has
311    # cored on solaris in the past.  In addition, --gc-sections doesn't
312    # really work at the moment (keeps on discarding used sections, first
313    # .eh_frame and now some of the glibc sections for iconv).
314    # Bzzzzt.  Thanks for playing, maybe next time.
315    AC_MSG_CHECKING([for ld that supports -Wl,--gc-sections])
316    AC_RUN_IFELSE([
317      AC_LANG_SOURCE(
318      [[
319         int main(void)
320         {
321            try { throw 1; }
322            catch (...) { };
323            return 0;
324         }
325      ]])],
326    [ac_sectionLDflags=yes],
327    [ac_sectionLDflags=no],
328    [ac_sectionLDflags=yes])
329    if test "$ac_test_CFLAGS" = set; then
330      CFLAGS="$ac_save_CFLAGS"
331    else
332      # this is the suspicious part
333      CFLAGS=''
334    fi
335    if test "$ac_sectionLDflags" = "yes"; then
336      SECTION_LDFLAGS="-Wl,--gc-sections $SECTION_LDFLAGS"
337    fi
338    AC_MSG_RESULT($ac_sectionLDflags)
339  fi
340
341  # Set linker optimization flags.
342  if test x"$with_gnu_ld" = x"yes"; then
343    OPT_LDFLAGS="-Wl,-O1 $OPT_LDFLAGS"
344  fi
345
346  AC_SUBST(SECTION_LDFLAGS)
347  AC_SUBST(OPT_LDFLAGS)
348])
349
350
351dnl
352dnl Add version tags to symbols in shared library (or not), additionally
353dnl marking other symbols as private/local (or not).
354dnl
355dnl --enable-symvers=style adds a version script to the linker call when
356dnl       creating the shared library.  The choice of version script is
357dnl       controlled by 'style'.
358dnl --disable-symvers does not.
359dnl  +  Usage:  LIBFFI_ENABLE_SYMVERS[(DEFAULT)]
360dnl       Where DEFAULT is either 'yes' or 'no'.  Passing `yes' tries to
361dnl       choose a default style based on linker characteristics.  Passing
362dnl       'no' disables versioning.
363dnl
364AC_DEFUN([LIBFFI_ENABLE_SYMVERS], [
365
366LIBFFI_ENABLE(symvers,yes,[=STYLE],
367  [enables symbol versioning of the shared library],
368  [permit yes|no|gnu*|sun])
369
370# If we never went through the LIBFFI_CHECK_LINKER_FEATURES macro, then we
371# don't know enough about $LD to do tricks...
372AC_REQUIRE([LIBFFI_CHECK_LINKER_FEATURES])
373
374# Turn a 'yes' into a suitable default.
375if test x$enable_symvers = xyes ; then
376  # FIXME  The following test is too strict, in theory.
377  if test $enable_shared = no || test "x$LD" = x; then
378    enable_symvers=no
379  else
380    if test $with_gnu_ld = yes ; then
381      enable_symvers=gnu
382    else
383      case ${target_os} in
384        # Sun symbol versioning exists since Solaris 2.5.
385        solaris2.[[5-9]]* | solaris2.1[[0-9]]*)
386          enable_symvers=sun ;;
387        *)
388          enable_symvers=no ;;
389      esac
390    fi
391  fi
392fi
393
394# Check if 'sun' was requested on non-Solaris 2 platforms.
395if test x$enable_symvers = xsun ; then
396  case ${target_os} in
397    solaris2*)
398      # All fine.
399      ;;
400    *)
401      # Unlikely to work.
402      AC_MSG_WARN([=== You have requested Sun symbol versioning, but])
403      AC_MSG_WARN([=== you are not targetting Solaris 2.])
404      AC_MSG_WARN([=== Symbol versioning will be disabled.])
405      enable_symvers=no
406      ;;
407  esac
408fi
409
410# Check to see if libgcc_s exists, indicating that shared libgcc is possible.
411if test $enable_symvers != no; then
412  AC_MSG_CHECKING([for shared libgcc])
413  ac_save_CFLAGS="$CFLAGS"
414  CFLAGS=' -lgcc_s'
415  AC_LINK_IFELSE(
416     [AC_LANG_SOURCE([[]], [[return 0;]])], libat_shared_libgcc=yes, libat_shared_libgcc=no)
417  CFLAGS="$ac_save_CFLAGS"
418  if test $libat_shared_libgcc = no; then
419    cat > conftest.c <<EOF
420int main (void) { return 0; }
421EOF
422changequote(,)dnl
423    libat_libgcc_s_suffix=`${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS \
424			     -shared -shared-libgcc -o conftest.so \
425			     conftest.c -v 2>&1 >/dev/null \
426			     | sed -n 's/^.* -lgcc_s\([^ ]*\) .*$/\1/p'`
427changequote([,])dnl
428    rm -f conftest.c conftest.so
429    if test x${libat_libgcc_s_suffix+set} = xset; then
430      CFLAGS=" -lgcc_s$libat_libgcc_s_suffix"
431      AC_LINK_IFELSE(
432         [AC_LANG_SOURCE([[]], [[return 0;]])], libat_shared_libgcc=yes)
433      CFLAGS="$ac_save_CFLAGS"
434    fi
435  fi
436  AC_MSG_RESULT($libat_shared_libgcc)
437fi
438
439# For GNU ld, we need at least this version.  The format is described in
440# LIBFFI_CHECK_LINKER_FEATURES above.
441libat_min_gnu_ld_version=21400
442# XXXXXXXXXXX libat_gnu_ld_version=21390
443
444# Check to see if unspecified "yes" value can win, given results above.
445# Change "yes" into either "no" or a style name.
446if test $enable_symvers != no && test $libat_shared_libgcc = yes; then
447  if test $with_gnu_ld = yes; then
448    if test $libat_gnu_ld_version -ge $libat_min_gnu_ld_version ; then
449      enable_symvers=gnu
450    elif test $libat_ld_is_gold = yes ; then
451      enable_symvers=gnu
452    elif test $libat_ld_is_lld = yes ; then
453      enable_symvers=gnu
454    else
455      # The right tools, the right setup, but too old.  Fallbacks?
456      AC_MSG_WARN(=== Linker version $libat_gnu_ld_version is too old for)
457      AC_MSG_WARN(=== full symbol versioning support in this release of GCC.)
458      AC_MSG_WARN(=== You would need to upgrade your binutils to version)
459      AC_MSG_WARN(=== $libat_min_gnu_ld_version or later and rebuild GCC.)
460      if test $libat_gnu_ld_version -ge 21200 ; then
461        # Globbing fix is present, proper block support is not.
462        dnl AC_MSG_WARN([=== Dude, you are soooo close.  Maybe we can fake it.])
463        dnl enable_symvers=???
464        AC_MSG_WARN([=== Symbol versioning will be disabled.])
465        enable_symvers=no
466      else
467        # 2.11 or older.
468        AC_MSG_WARN([=== Symbol versioning will be disabled.])
469        enable_symvers=no
470      fi
471    fi
472  elif test $enable_symvers = sun; then
473    : All interesting versions of Sun ld support sun style symbol versioning.
474  else
475    # just fail for now
476    AC_MSG_WARN([=== You have requested some kind of symbol versioning, but])
477    AC_MSG_WARN([=== either you are not using a supported linker, or you are])
478    AC_MSG_WARN([=== not building a shared libgcc_s (which is required).])
479    AC_MSG_WARN([=== Symbol versioning will be disabled.])
480    enable_symvers=no
481  fi
482fi
483if test $enable_symvers = gnu; then
484  AC_DEFINE(LIBFFI_GNU_SYMBOL_VERSIONING, 1,
485	    [Define to 1 if GNU symbol versioning is used for libatomic.])
486fi
487
488AM_CONDITIONAL(LIBFFI_BUILD_VERSIONED_SHLIB, test $enable_symvers != no)
489AM_CONDITIONAL(LIBFFI_BUILD_VERSIONED_SHLIB_GNU, test $enable_symvers = gnu)
490AM_CONDITIONAL(LIBFFI_BUILD_VERSIONED_SHLIB_SUN, test $enable_symvers = sun)
491AC_MSG_NOTICE(versioning on shared library symbols is $enable_symvers)
492])
493