xref: /netbsd-src/external/gpl3/gdb/dist/libiberty/acinclude.m4 (revision 5173eb0a33e5d83890ba976253e703be4c92557c)
1*5173eb0aSchristosdnl Copyright (C) 2000-2024 Free Software Foundation, Inc.
24b169a6bSchristosdnl
34b169a6bSchristosdnl GCC is free software; you can redistribute it and/or modify
44b169a6bSchristosdnl it under the terms of the GNU General Public License as published by
54b169a6bSchristosdnl the Free Software Foundation; either version 3, or (at your option)
64b169a6bSchristosdnl any later version.
74b169a6bSchristosdnl
84b169a6bSchristosdnl GCC is distributed in the hope that it will be useful,
94b169a6bSchristosdnl but WITHOUT ANY WARRANTY; without even the implied warranty of
104b169a6bSchristosdnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
114b169a6bSchristosdnl GNU General Public License for more details.
124b169a6bSchristosdnl
134b169a6bSchristosdnl You should have received a copy of the GNU General Public License
144b169a6bSchristosdnl along with GCC; see the file COPYING3.  If not see
154b169a6bSchristosdnl <http://www.gnu.org/licenses/>.
164b169a6bSchristos
174b169a6bSchristosdnl See whether strncmp reads past the end of its string parameters.
184b169a6bSchristosdnl On some versions of SunOS4 at least, strncmp reads a word at a time
194b169a6bSchristosdnl but erroneously reads past the end of strings.  This can cause
204b169a6bSchristosdnl a SEGV in some cases.
214b169a6bSchristosAC_DEFUN([libiberty_AC_FUNC_STRNCMP],
22*5173eb0aSchristos[AC_REQUIRE([GCC_AC_FUNC_MMAP])
234b169a6bSchristosAC_CACHE_CHECK([for working strncmp], ac_cv_func_strncmp_works,
244b169a6bSchristos[AC_TRY_RUN([
254b169a6bSchristos/* Test by Jim Wilson and Kaveh Ghazi.
264b169a6bSchristos   Check whether strncmp reads past the end of its string parameters. */
274b169a6bSchristos#include <stdlib.h>
284b169a6bSchristos#include <string.h>
294b169a6bSchristos#include <sys/types.h>
304b169a6bSchristos
314b169a6bSchristos#ifdef HAVE_FCNTL_H
324b169a6bSchristos#include <fcntl.h>
334b169a6bSchristos#endif
344b169a6bSchristos
354b169a6bSchristos#ifdef HAVE_SYS_MMAN_H
364b169a6bSchristos#include <sys/mman.h>
374b169a6bSchristos#endif
384b169a6bSchristos
394b169a6bSchristos#ifndef MAP_ANON
404b169a6bSchristos#ifdef MAP_ANONYMOUS
414b169a6bSchristos#define MAP_ANON MAP_ANONYMOUS
424b169a6bSchristos#else
434b169a6bSchristos#define MAP_ANON MAP_FILE
444b169a6bSchristos#endif
454b169a6bSchristos#endif
464b169a6bSchristos
474b169a6bSchristos#ifndef MAP_FILE
484b169a6bSchristos#define MAP_FILE 0
494b169a6bSchristos#endif
504b169a6bSchristos#ifndef O_RDONLY
514b169a6bSchristos#define O_RDONLY 0
524b169a6bSchristos#endif
534b169a6bSchristos
544b169a6bSchristos#define MAP_LEN 0x10000
554b169a6bSchristos
564b169a6bSchristosint
574b169a6bSchristosmain (void)
584b169a6bSchristos{
594b169a6bSchristos#if defined(HAVE_MMAP) || defined(HAVE_MMAP_ANYWHERE)
604b169a6bSchristos  char *p;
614b169a6bSchristos  int dev_zero;
624b169a6bSchristos
634b169a6bSchristos  dev_zero = open ("/dev/zero", O_RDONLY);
644b169a6bSchristos  if (dev_zero < 0)
654b169a6bSchristos    exit (1);
664b169a6bSchristos
674b169a6bSchristos  p = (char *) mmap (0, MAP_LEN, PROT_READ|PROT_WRITE,
684b169a6bSchristos		     MAP_ANON|MAP_PRIVATE, dev_zero, 0);
694b169a6bSchristos  if (p == (char *)-1)
704b169a6bSchristos    p = (char *) mmap (0, MAP_LEN, PROT_READ|PROT_WRITE,
714b169a6bSchristos		       MAP_ANON|MAP_PRIVATE, -1, 0);
724b169a6bSchristos  if (p == (char *)-1)
734b169a6bSchristos    exit (2);
744b169a6bSchristos  else
754b169a6bSchristos    {
764b169a6bSchristos      char *string = "__si_type_info";
774b169a6bSchristos      char *q = (char *) p + MAP_LEN - strlen (string) - 2;
784b169a6bSchristos      char *r = (char *) p + 0xe;
794b169a6bSchristos
804b169a6bSchristos      strcpy (q, string);
814b169a6bSchristos      strcpy (r, string);
824b169a6bSchristos      strncmp (r, q, 14);
834b169a6bSchristos    }
844b169a6bSchristos#endif /* HAVE_MMAP || HAVE_MMAP_ANYWHERE */
854b169a6bSchristos  exit (0);
864b169a6bSchristos}
874b169a6bSchristos], ac_cv_func_strncmp_works=yes, ac_cv_func_strncmp_works=no,
884b169a6bSchristos  ac_cv_func_strncmp_works=yes)
894b169a6bSchristosrm -f core core.* *.core])
904b169a6bSchristosif test $ac_cv_func_strncmp_works = no ; then
914b169a6bSchristos  AC_LIBOBJ([strncmp])
924b169a6bSchristosfi
934b169a6bSchristos])
944b169a6bSchristos
954b169a6bSchristosdnl See if errno must be declared even when <errno.h> is included.
964b169a6bSchristosAC_DEFUN([libiberty_AC_DECLARE_ERRNO],
974b169a6bSchristos[AC_CACHE_CHECK(whether errno must be declared, libiberty_cv_declare_errno,
984b169a6bSchristos[AC_TRY_COMPILE(
994b169a6bSchristos[#include <errno.h>],
1004b169a6bSchristos[int x = errno;],
1014b169a6bSchristoslibiberty_cv_declare_errno=no,
1024b169a6bSchristoslibiberty_cv_declare_errno=yes)])
1034b169a6bSchristosif test $libiberty_cv_declare_errno = yes
1044b169a6bSchristosthen AC_DEFINE(NEED_DECLARATION_ERRNO, 1,
1054b169a6bSchristos  [Define if errno must be declared even when <errno.h> is included.])
1064b169a6bSchristosfi
1074b169a6bSchristos])
1084b169a6bSchristos
1094b169a6bSchristosdnl See whether we need a declaration for a function.
1104b169a6bSchristosAC_DEFUN([libiberty_NEED_DECLARATION],
1114b169a6bSchristos[AC_MSG_CHECKING([whether $1 must be declared])
1124b169a6bSchristosAC_CACHE_VAL(libiberty_cv_decl_needed_$1,
1134b169a6bSchristos[AC_TRY_COMPILE([
1144b169a6bSchristos#include "confdefs.h"
1154b169a6bSchristos#include <stdio.h>
1164b169a6bSchristos#ifdef HAVE_STRING_H
1174b169a6bSchristos#include <string.h>
1184b169a6bSchristos#else
1194b169a6bSchristos#ifdef HAVE_STRINGS_H
1204b169a6bSchristos#include <strings.h>
1214b169a6bSchristos#endif
1224b169a6bSchristos#endif
1234b169a6bSchristos#ifdef HAVE_STDLIB_H
1244b169a6bSchristos#include <stdlib.h>
1254b169a6bSchristos#endif
1264b169a6bSchristos#ifdef HAVE_UNISTD_H
1274b169a6bSchristos#include <unistd.h>
1284b169a6bSchristos#endif],
1294b169a6bSchristos[char *(*pfn) = (char *(*)) $1],
1304b169a6bSchristoslibiberty_cv_decl_needed_$1=no, libiberty_cv_decl_needed_$1=yes)])
1314b169a6bSchristosAC_MSG_RESULT($libiberty_cv_decl_needed_$1)
1324b169a6bSchristosif test $libiberty_cv_decl_needed_$1 = yes; then
1334b169a6bSchristos  AC_DEFINE([NEED_DECLARATION_]translit($1, [a-z], [A-Z]), 1,
1344b169a6bSchristos            [Define if $1 is not declared in system header files.])
1354b169a6bSchristosfi
1364b169a6bSchristos])dnl
1374b169a6bSchristos
1384b169a6bSchristos# We always want a C version of alloca() compiled into libiberty,
1394b169a6bSchristos# because native-compiler support for the real alloca is so !@#$%
1404b169a6bSchristos# unreliable that GCC has decided to use it only when being compiled
1414b169a6bSchristos# by GCC.  This is the part of AC_FUNC_ALLOCA that calculates the
1424b169a6bSchristos# information alloca.c needs.
1434b169a6bSchristosAC_DEFUN([libiberty_AC_FUNC_C_ALLOCA],
1444b169a6bSchristos[AC_CACHE_CHECK(whether alloca needs Cray hooks, ac_cv_os_cray,
1454b169a6bSchristos[AC_EGREP_CPP(webecray,
1464b169a6bSchristos[#if defined(CRAY) && ! defined(CRAY2)
1474b169a6bSchristoswebecray
1484b169a6bSchristos#else
1494b169a6bSchristoswenotbecray
1504b169a6bSchristos#endif
1514b169a6bSchristos], ac_cv_os_cray=yes, ac_cv_os_cray=no)])
1524b169a6bSchristosif test $ac_cv_os_cray = yes; then
1534b169a6bSchristos  for ac_func in _getb67 GETB67 getb67; do
1544b169a6bSchristos    AC_CHECK_FUNC($ac_func,
1554b169a6bSchristos      [AC_DEFINE_UNQUOTED(CRAY_STACKSEG_END, $ac_func,
1564b169a6bSchristos  [Define to one of _getb67, GETB67, getb67 for Cray-2 and Cray-YMP
1574b169a6bSchristos   systems. This function is required for alloca.c support on those
1584b169a6bSchristos   systems.])  break])
1594b169a6bSchristos  done
1604b169a6bSchristosfi
1614b169a6bSchristos
1624b169a6bSchristosAC_CACHE_CHECK(stack direction for C alloca, ac_cv_c_stack_direction,
1634b169a6bSchristos[AC_TRY_RUN([#include <stdlib.h>
1644b169a6bSchristos
1654b169a6bSchristosint
1664b169a6bSchristosfind_stack_direction (void)
1674b169a6bSchristos{
1684b169a6bSchristos  static char *addr = 0;
1694b169a6bSchristos  auto char dummy;
1704b169a6bSchristos  if (addr == 0)
1714b169a6bSchristos    {
1724b169a6bSchristos      addr = &dummy;
1734b169a6bSchristos      return find_stack_direction ();
1744b169a6bSchristos    }
1754b169a6bSchristos  else
1764b169a6bSchristos    return (&dummy > addr) ? 1 : -1;
1774b169a6bSchristos}
1784b169a6bSchristos
1794b169a6bSchristosint
1804b169a6bSchristosmain (void)
1814b169a6bSchristos{
1824b169a6bSchristos  exit (find_stack_direction() < 0);
1834b169a6bSchristos}],
1844b169a6bSchristos  ac_cv_c_stack_direction=1,
1854b169a6bSchristos  ac_cv_c_stack_direction=-1,
1864b169a6bSchristos  ac_cv_c_stack_direction=0)])
1874b169a6bSchristosAC_DEFINE_UNQUOTED(STACK_DIRECTION, $ac_cv_c_stack_direction,
1884b169a6bSchristos  [Define if you know the direction of stack growth for your system;
1894b169a6bSchristos   otherwise it will be automatically deduced at run-time.
1904b169a6bSchristos        STACK_DIRECTION > 0 => grows toward higher addresses
1914b169a6bSchristos        STACK_DIRECTION < 0 => grows toward lower addresses
1924b169a6bSchristos        STACK_DIRECTION = 0 => direction of growth unknown])
1934b169a6bSchristos])
194