[m-rev.] for review: Check for gcc generating PIC by default.

Peter Wang novalazy at gmail.com
Fri Feb 25 13:43:32 AEDT 2022


In configure, we try detect the non-working combination of gcc 5+,
targeting x86, PIC, and non-local gotos. We assumed that PIC is only
generated when creating shared libraries, but gcc may have been
configured to generate PIC by default. This is very common on more
recent Linux distributions.

configure.ac:
    Extend the test mentioned above: if shared libraries are disabled,
    check whether gcc generates PIC anyway.

    Clean up the code a little.

m4/mercury.m4:
    Add helper macros MERCURY_CC_TARGETS_X86 and
    MERCURY_CC_GENERATES_PIC.

README.x86:
    Update this document.

diff --git a/README.x86 b/README.x86
index d5d361fc7..41009d722 100644
--- a/README.x86
+++ b/README.x86
@@ -2,7 +2,7 @@ Due to improvements in GCC, the following combination is not supported:
 
   - GCC version 5 and above
   - x86
-  - PIC (position-independent code), i.e. dynamic linking
+  - PIC (position-independent code)
   - low-level C grades using non-local gotos, i.e. asm_fast.*
 
 The configure script will not select asm_fast grades when the above combination
@@ -13,8 +13,8 @@ We recommend using high-level C grades (hlc.*) on x86. If you require a
 low-level C grade, e.g. for debugging, then you can use a reg.* grade.
 
 If you must use an asm_fast grade, you will need to tell the C compiler not to
-generate position-independent code, and disable building of shared libraries.
-Alternatively, you could downgrade to gcc 4.9.
+generate position-independent code (which may be the default), *and* disable
+building of shared libraries. Alternatively, you could downgrade to gcc 4.9.
 
 Note that Windows is unaffected by this issue as Windows does not use PIC,
 and we do not yet support shared libraries (DLLs) on Windows anyway.
diff --git a/configure.ac b/configure.ac
index f489ff8e1..fab0809cb 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2,7 +2,7 @@
 # vim: ts=4 sw=4 expandtab
 #-----------------------------------------------------------------------------#
 # Copyright (C) 1995-2012 The University of Melbourne.
-# Copyright (C) 2013-2021 The Mercury team.
+# Copyright (C) 2013-2022 The Mercury team.
 # This file may only be copied under the terms of the GNU General
 # Public Licence - see the file COPYING in the Mercury distribution.
 #-----------------------------------------------------------------------------#
@@ -2332,47 +2332,34 @@ case "$ac_cv_c_compiler_gnu" in yes)
 
             # An old hack allowing non-local gotos to work with PIC on x86
             # will no longer work with GCC 5+ (see mercury_goto.h).
-            # The following disables non-local gotos if dynamic linking
-            # is enabled, even though statically linked libraries (non-PIC)
-            # would still work.
-            # Alternatively, we may wish to consider using non-PIC in shared
-            # libraries, accepting whatever drawbacks that entails.
-            #
-            # XXX Not all platforms use PIC for dynamic linking, so this should
-            # be contingent on CFLAGS_FOR_PIC which is set further down.
-            #
-            allow_pic_nonlocal_gotos=no
+            pic_conflicts_with_nonlocal_gotos=no
             case $C_COMPILER_TYPE in
                 gcc_3_*|gcc_4_*)
-                    allow_pic_nonlocal_gotos=yes
+                    ;;
+                gcc_*)
+                    MERCURY_CC_TARGETS_X86
+                    if test "$mercury_cv_cc_targets_x86" = yes
+                    then
+                        if test "$mercury_cv_enable_shared_libs" = yes
+                        then
+                            case "$host" in
+                                *cygwin*|*mingw*)
+                                    # Windows DLLs do not use PIC, and
+                                    # we do not support DLLs yet anyway.
+                                    ;;
+                                *)
+                                    # Assume shared libraries use PIC.
+                                    pic_conflicts_with_nonlocal_gotos=yes
+                                    ;;
+                            esac
+                        else
+                            MERCURY_CC_GENERATES_PIC
+                            pic_conflicts_with_nonlocal_gotos="$mercury_cv_cc_generates_pic"
+                        fi
+                    fi
                     ;;
             esac
-            case "$host" in
-                *cygwin*|*mingw*)
-                    # Windows doesn't use PIC and we don't yet support DLLs
-                    # anyway.
-                    allow_pic_nonlocal_gotos=yes
-                    ;;
-                *)
-                    # Run the compiler to check what it is targeting.
-                    # Even if $host is x86_64, gcc -m32 or gcc -mx32
-                    # may be used to target x86.
-                    AC_MSG_CHECKING(whether C compiler targets x86_64)
-                    AC_CACHE_VAL(mercury_cv_target_x86_64,
-                        AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[
-                                #ifndef __x86_64__
-                                #error "Target is not x86_64"
-                                #endif
-                            ]])],
-                            [mercury_cv_target_x86_64=yes],
-                            [mercury_cv_target_x86_64=no])
-                    )
-                    AC_MSG_RESULT($mercury_cv_target_x86_64)
-                    allow_pic_nonlocal_gotos="$mercury_cv_target_x86_64"
-                    ;;
-            esac
-            if test "$mercury_cv_enable_shared_libs" = yes && \
-                test "$allow_pic_nonlocal_gotos" = no
+            if test "$pic_conflicts_with_nonlocal_gotos" = yes
             then
                 MERCURY_MSG("gcc labels do not work with PIC on x86")
                 mercury_cv_asm_labels=no
diff --git a/m4/mercury.m4 b/m4/mercury.m4
index 9b5da241d..7a350fbfb 100644
--- a/m4/mercury.m4
+++ b/m4/mercury.m4
@@ -1,6 +1,6 @@
 #-----------------------------------------------------------------------------#
 # Copyright (C) 1999,2001-2004, 2006-2012 The University of Melbourne.
-# Copyright (C) 2013-2020 The Mercury team.
+# Copyright (C) 2013-2022 The Mercury team.
 # This file may only be copied under the terms of the GNU General
 # Public Licence - see the file COPYING in the Mercury distribution.
 #-----------------------------------------------------------------------------#
@@ -767,6 +767,52 @@ else
 fi
 ])
 
+#-----------------------------------------------------------------------------#
+#
+# Check if the C compiler targets x86.
+# Note that checking $host is insufficient as we may be cross-compiling.
+#
+
+AC_DEFUN([MERCURY_CC_TARGETS_X86], [
+AC_MSG_CHECKING(whether C compiler targets x86)
+
+AC_CACHE_VAL(mercury_cv_cc_targets_x86,
+    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[
+            #ifdef __i386__
+            #else
+            #error "target is not x86"
+            #endif
+        ]])],
+        [mercury_cv_cc_targets_x86=yes],
+        [mercury_cv_cc_targets_x86=no])
+)
+
+AC_MSG_RESULT($mercury_cv_cc_targets_x86)
+])
+
+#-----------------------------------------------------------------------------#
+#
+# Check if the C compiler is configured to generate position-independent code.
+# This may be the case even when not creating shared libraries.
+#
+
+AC_DEFUN([MERCURY_CC_GENERATES_PIC], [
+AC_MSG_CHECKING(whether C compiler generates PIC)
+
+AC_CACHE_VAL(mercury_cv_cc_generates_pic,
+    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[
+            #if defined(__PIC__) || defined(__pic__)
+            #else
+            #error "not PIC"
+            #endif
+        ]])],
+        [mercury_cv_cc_generates_pic=yes],
+        [mercury_cv_cc_generates_pic=no])
+)
+
+AC_MSG_RESULT($mercury_cv_cc_generates_pic)
+])
+
 #-----------------------------------------------------------------------------#
 #
 # Check if the POSIX threads library is pthreads-win32.
-- 
2.31.0



More information about the reviews mailing list