aboutsummaryrefslogtreecommitdiffstats
path: root/main/gcc/0040-configure-Add-enable-autolink-libatomic-use-in-LINK_.patch
blob: 5474b5135ce67810a1c8b6f260a5fba330443dd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
From 72e9d86cda1f9050435327c3488a21f79b0a930a Mon Sep 17 00:00:00 2001
From: Drew DeVault <sir@cmpwn.com>
Date: Wed, 9 Dec 2020 16:07:26 +0000
Subject: [PATCH] configure: Add --enable-autolink-libatomic, use in
 LINK_GCC_C_SEQUENCE_SPEC [PR81358]

This fixes issues with RISC-V.
---
 Makefile.in           |  1 +
 gcc/config.in         |  6 ++++++
 gcc/config/gnu-user.h | 12 +++++++++++-
 gcc/configure         | 34 ++++++++++++++++++++++++++++++++--
 gcc/configure.ac      | 23 ++++++++++++++++++++++-
 gcc/doc/install.texi  |  8 ++++++++
 gcc/doc/tm.texi       |  8 +++++++-
 gcc/doc/tm.texi.in    |  8 +++++++-
 gcc/gcc.c             | 12 +++++++++++-
 9 files changed, 105 insertions(+), 7 deletions(-)

diff --git a/Makefile.in b/Makefile.in
index c7f1f84d683..cb3983ca547 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -222,6 +222,7 @@ HOST_EXPORTS = \
 	RANLIB_FOR_TARGET="$(RANLIB_FOR_TARGET)"; export RANLIB_FOR_TARGET; \
 	READELF_FOR_TARGET="$(READELF_FOR_TARGET)"; export READELF_FOR_TARGET; \
 	TOPLEVEL_CONFIGURE_ARGUMENTS="$(TOPLEVEL_CONFIGURE_ARGUMENTS)"; export TOPLEVEL_CONFIGURE_ARGUMENTS; \
+	TARGET_CONFIGDIRS="$(TARGET_CONFIGDIRS)"; export TARGET_CONFIGDIRS; \
 	HOST_LIBS="$(STAGE1_LIBS)"; export HOST_LIBS; \
 	GMPLIBS="$(HOST_GMPLIBS)"; export GMPLIBS; \
 	GMPINC="$(HOST_GMPINC)"; export GMPINC; \
diff --git a/gcc/config.in b/gcc/config.in
index 9551c0dfdf9..c8ca910df8a 100644
--- a/gcc/config.in
+++ b/gcc/config.in
@@ -106,6 +106,12 @@
 #endif
 
 
+/* Define if libatomic should always be linked. */
+#ifndef USED_FOR_TARGET
+#undef ENABLE_AUTOLINK_LIBATOMIC
+#endif
+
+
 /* Define to 1 to specify that we are using the BID decimal floating point
    format instead of DPD */
 #ifndef USED_FOR_TARGET
diff --git a/gcc/config/gnu-user.h b/gcc/config/gnu-user.h
index 7f30c363e4a..246698bfea5 100644
--- a/gcc/config/gnu-user.h
+++ b/gcc/config/gnu-user.h
@@ -109,8 +109,18 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 #define LINK_EH_SPEC "%{!static|static-pie:--eh-frame-hdr} "
 #endif
 
+#if !defined(LINK_LIBATOMIC_SPEC) && defined(ENABLE_AUTOLINK_LIBATOMIC)
+#  ifdef LD_AS_NEEDED_OPTION
+#    define LINK_LIBATOMIC_SPEC LD_AS_NEEDED_OPTION " -latomic " LD_NO_AS_NEEDED_OPTION
+#  else
+#    define LINK_LIBATOMIC_SPEC "-latomic"
+#  endif
+#elif !defined(LINK_LIBATOMIC_SPEC)
+#  define LINK_LIBATOMIC_SPEC ""
+#endif
+
 #define GNU_USER_TARGET_LINK_GCC_C_SEQUENCE_SPEC \
-  "%{static|static-pie:--start-group} %G %{!nolibc:%L} \
+  "%{static|static-pie:--start-group} %G %{!nolibc:" LINK_LIBATOMIC_SPEC " %L} \
    %{static|static-pie:--end-group}%{!static:%{!static-pie:%G}}"
 
 #undef LINK_GCC_C_SEQUENCE_SPEC
diff --git a/gcc/configure b/gcc/configure
index 808570b6c99..6c1e95e0444 100755
--- a/gcc/configure
+++ b/gcc/configure
@@ -969,6 +969,7 @@ with_documentation_root_url
 with_changes_root_url
 enable_languages
 with_multilib_list
+enable_autolink_libatomic
 with_zstd
 with_zstd_include
 with_zstd_lib
@@ -1695,6 +1696,9 @@ Optional Features:
   --disable-shared        don't provide a shared libgcc
   --disable-gcov          don't provide libgcov and related host tools
   --enable-languages=LIST specify which front-ends to build
+  --enable-autolink-libatomic
+                          enable automatic linking of libatomic (ignored if
+                          not built)
   --disable-rpath         do not hardcode runtime library paths
   --enable-sjlj-exceptions
                           arrange to use setjmp/longjmp exception handling
@@ -8009,6 +8013,33 @@ else
 fi
 
 
+# If libatomic is available, whether it should be linked automatically
+# Check whether --enable-autolink-libatomic was given.
+if test "${enable_autolink_libatomic+set}" = set; then :
+  enableval=$enable_autolink_libatomic;
+  case $enable_autolink_libatomic in
+    yes | no) ;;
+    *) as_fn_error $? "'$enable_autolink_libatomic' is an invalid value for
+--enable-autolink-libatomic.  Valid choices are 'yes' and 'no'." "$LINENO" 5 ;;
+  esac
+
+else
+  enable_autolink_libatomic=''
+fi
+
+
+if test x$enable_autolink_libatomic = xyes; then
+  if echo " ${TARGET_CONFIGDIRS} " | grep " libatomic " > /dev/null 2>&1 ; then
+
+$as_echo "#define ENABLE_AUTOLINK_LIBATOMIC 1" >>confdefs.h
+
+  else
+    { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: libatomic is not build for this target, --enable-autolink-libatomic ignored" >&5
+$as_echo "$as_me: WARNING: libatomic is not build for this target, --enable-autolink-libatomic ignored" >&2;}
+  fi
+fi
+
+
 # -------------------------
 # Checks for other programs
 # -------------------------
@@ -19131,7 +19162,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 19131 "configure"
+#line 19158 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -32267,4 +32298,3 @@ if test -n "$ac_unrecognized_opts" && test "$enable_option_checking" != no; then
 $as_echo "$as_me: WARNING: unrecognized options: $ac_unrecognized_opts" >&2;}
 fi
 
-
diff --git a/gcc/configure.ac b/gcc/configure.ac
index 9c2571de709..52fd6d8fe06 100644
--- a/gcc/configure.ac
+++ b/gcc/configure.ac
@@ -1110,6 +1110,28 @@ AC_ARG_WITH(multilib-list,
 :,
 with_multilib_list=default)
 
+# If libatomic is available, whether it should be linked automatically
+AC_ARG_ENABLE(autolink-libatomic,
+[AS_HELP_STRING([--enable-autolink-libatomic],
+		[enable automatic linking of libatomic (ignored if not built)])],
+[
+  case $enable_autolink_libatomic in
+    yes | no) ;;
+    *) AC_MSG_ERROR(['$enable_autolink_libatomic' is an invalid value for
+--enable-autolink-libatomic.  Valid choices are 'yes' and 'no'.]) ;;
+  esac
+], [enable_autolink_libatomic=''])
+
+if test x$enable_autolink_libatomic = xyes; then
+  if echo " ${TARGET_CONFIGDIRS} " | grep " libatomic " > /dev/null 2>&1 ; then
+    AC_DEFINE(ENABLE_AUTOLINK_LIBATOMIC, 1,
+	[Define if libatomic should always be linked.])
+  else
+    AC_MSG_WARN([libatomic is not build for this target, --enable-autolink-libatomic ignored])
+  fi
+fi
+
+
 # -------------------------
 # Checks for other programs
 # -------------------------
@@ -6945,4 +6967,3 @@ done
 ], 
 [subdirs='$subdirs'])
 AC_OUTPUT
-
diff --git a/gcc/doc/install.texi b/gcc/doc/install.texi
index 7d98ec4190a..a58d5a05be2 100644
--- a/gcc/doc/install.texi
+++ b/gcc/doc/install.texi
@@ -2169,6 +2169,14 @@ files, but these changed header paths may conflict with some compilation
 environments.  Enabled by default, and may be disabled using
 @option{--disable-canonical-system-headers}.
 
+@item --enable-autolink-libatomic
+@itemx --disable-autolink-libatomic
+Tell GCC that it should automatically link libatomic; if supported by
+the linker, the file is only linked as needed. This flag is ignored
+when libatomic is not built. Note that this conigure flag is in particular
+useful when building an offloading-target compiler; as for those, a
+user had to specify @code{-foffload=target=-latomic} otherwise.
+
 @item --with-glibc-version=@var{major}.@var{minor}
 Tell GCC that when the GNU C Library (glibc) is used on the target it
 will be version @var{major}.@var{minor} or later.  Normally this can
diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi
index fcb7245e95c..67b485e8d0c 100644
--- a/gcc/doc/tm.texi
+++ b/gcc/doc/tm.texi
@@ -381,7 +381,13 @@ the argument @option{-lgcc} to tell the linker to do the search.
 
 @defmac LINK_GCC_C_SEQUENCE_SPEC
 The sequence in which libgcc and libc are specified to the linker.
-By default this is @code{%G %L %G}.
+By default this is @code{%G LINK_LIBATOMIC_SPEC %L %G}.
+@end defmac
+
+@defmac LINK_LIBATOMIC_SPEC
+This macro is used in the default @code{LINK_GCC_C_SEQUENCE_SPEC} to link
+libatomic. By default, it is unset unless @code{ENABLE_AUTOLINK_LIBATOMIC}
+is set.
 @end defmac
 
 @defmac POST_LINK_SPEC
diff --git a/gcc/doc/tm.texi.in b/gcc/doc/tm.texi.in
index c17209daa51..3ec63f6d091 100644
--- a/gcc/doc/tm.texi.in
+++ b/gcc/doc/tm.texi.in
@@ -381,7 +381,13 @@ the argument @option{-lgcc} to tell the linker to do the search.
 
 @defmac LINK_GCC_C_SEQUENCE_SPEC
 The sequence in which libgcc and libc are specified to the linker.
-By default this is @code{%G %L %G}.
+By default this is @code{%G LINK_LIBATOMIC_SPEC %L %G}.
+@end defmac
+
+@defmac LINK_LIBATOMIC_SPEC
+This macro is used in the default @code{LINK_GCC_C_SEQUENCE_SPEC} to link
+libatomic. By default, it is unset unless @code{ENABLE_AUTOLINK_LIBATOMIC}
+is set.
 @end defmac
 
 @defmac POST_LINK_SPEC
diff --git a/gcc/gcc.c b/gcc/gcc.c
index 40e07354b3d..81bd50b4d7c 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -877,13 +877,23 @@ proper position among the other output files.  */
 # define ASM_DEBUG_SPEC ""
 #endif
 
+#if !defined(LINK_LIBATOMIC_SPEC) && defined(ENABLE_AUTOLINK_LIBATOMIC)
+#  ifdef LD_AS_NEEDED_OPTION
+#    define LINK_LIBATOMIC_SPEC LD_AS_NEEDED_OPTION " -latomic " LD_NO_AS_NEEDED_OPTION
+#  else
+#    define LINK_LIBATOMIC_SPEC "-latomic"
+#  endif
+#elif !defined(LINK_LIBATOMIC_SPEC)
+#  define LINK_LIBATOMIC_SPEC ""
+#endif
+
 /* Here is the spec for running the linker, after compiling all files.  */
 
 /* This is overridable by the target in case they need to specify the
    -lgcc and -lc order specially, yet not require them to override all
    of LINK_COMMAND_SPEC.  */
 #ifndef LINK_GCC_C_SEQUENCE_SPEC
-#define LINK_GCC_C_SEQUENCE_SPEC "%G %{!nolibc:%L %G}"
+#define LINK_GCC_C_SEQUENCE_SPEC "%G %{!nolibc:" LINK_LIBATOMIC_SPEC " %L %G}"
 #endif
 
 #ifdef ENABLE_DEFAULT_SSP
-- 
2.33.0