From d03dbe54292d782568e12d3ba952ee8380784555 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sun, 20 Sep 2020 01:40:36 +0200
Subject: [PATCH] havelib: Avoid linking with libc.a on GNU systems.

Reported by Bruce Dubbs <bruce.dubbs@gmail.com> in
<https://lists.gnu.org/archive/html/grep-devel/2020-09/msg00052.html>.

* m4/lib-link.m4 (AC_LIB_LINKFLAGS_BODY): When processing the
dependency_libs value of a .la file, ignore '-lc' options on GNU
systems.
---
 ChangeLog      |  9 +++++++++
 m4/lib-link.m4 | 17 +++++++++++++++--
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index ebaa5fb2af..f094e97f15 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2020-09-19  Bruno Haible  <bruno@clisp.org>
+
+	havelib: Avoid linking with libc.a on GNU systems.
+	Reported by Bruce Dubbs <bruce.dubbs@gmail.com> in
+	<https://lists.gnu.org/archive/html/grep-devel/2020-09/msg00052.html>.
+	* m4/lib-link.m4 (AC_LIB_LINKFLAGS_BODY): When processing the
+	dependency_libs value of a .la file, ignore '-lc' options on GNU
+	systems.
+
 2020-09-19  Bruno Haible  <bruno@clisp.org>
 
 	Fix recognition of musl libc on Alpine Linux 3.10.
diff --git a/m4/lib-link.m4 b/m4/lib-link.m4
index eecf70ec76..5ce1aa24ae 100644
--- a/m4/lib-link.m4
+++ b/m4/lib-link.m4
@@ -1,4 +1,4 @@
-# lib-link.m4 serial 31
+# lib-link.m4 serial 32
 dnl Copyright (C) 2001-2020 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -631,7 +631,20 @@ AC_DEFUN([AC_LIB_LINKFLAGS_BODY],
                     ;;
                   -l*)
                     dnl Handle this in the next round.
-                    names_next_round="$names_next_round "`echo "X$dep" | sed -e 's/^X-l//'`
+                    dnl But on GNU systems, ignore -lc options, because
+                    dnl   - linking with libc is the default anyway,
+                    dnl   - linking with libc.a may produce an error
+                    dnl     "/usr/bin/ld: dynamic STT_GNU_IFUNC symbol `strcmp' with pointer equality in `/usr/lib/libc.a(strcmp.o)' can not be used when making an executable; recompile with -fPIE and relink with -pie"
+                    dnl     or may produce an executable that always crashes, see
+                    dnl     <https://lists.gnu.org/archive/html/grep-devel/2020-09/msg00052.html>.
+                    dep=`echo "X$dep" | sed -e 's/^X-l//'`
+                    if test "X$dep" != Xc \
+                       || case $host_os in
+                            linux* | gnu* | k*bsd*-gnu) false ;;
+                            *)                          true ;;
+                          esac; then
+                      names_next_round="$names_next_round $dep"
+                    fi
                     ;;
                   *.la)
                     dnl Handle this in the next round. Throw away the .la's
-- 
2.39.5