Solaris ld only gained support for section bracketing in Solaris 11.4.
Fortunately, in gdc it is only used for the minfo section, so it's easy
to provide a workaround by adding two additional startup files
drt{begin,end}.o which define __start_minfo and __stop_minfo.

This patch does just that.

I've raised a couple of questions in the PR already:

* I've introduced a new -dstartfiles option which triggers the use of
  libgphobos.spec even with -nophoboslib.  Since it's effectively
  internal to the build system, I'm not currently documenting it.

* I'm reading the spec file addition from a file: keeping it in a make
  variable would be extremely messy due to the necessary quoting.

* I've chosen to use -Wc instead of -Xcompiler throughout: it's way
  shorter when more options need to be passed and it can take several
  comma-separated options at once.

* libdruntime/gcc/drtstuff.c needs a copyright notice unless one wants
  to keep it in the public domain (also plausible).  Effectively
  something for Iain to decide.

Bootstrapped without regressions on i386-pc-solaris2.11 (Solaris 11.3),
no regressions compared to Solaris 11.4 test results.

        Rainer

-- 
-----------------------------------------------------------------------------
Rainer Orth, Center for Biotechnology, Bielefeld University


2018-11-20  Rainer Orth  <r...@cebitec.uni-bielefeld.de>

        libphobos:
        PR d/87864
        * configure.ac [!DCFG_MINFO_BRACKETING] (DRTSTUFF_SPEC): New variable.
        Substitute it.
        * libdruntime/m4/druntime/os.m4 (DRUNTIME_OS_MINFO_BRACKETING):
        New automake conditional.
        * configure: Regenerate.
        * libdruntime/gcc/drtstuff.c: New file.
        * libdruntime/Makefile.am [!DRUNTIME_OS_MINFO_BRACKETING]
        (DRTSTUFF, toolexeclib_DATA): New variables.
        (gcc/drtbegin.lo, gcc/drtend.lo): New rules.
        (libgdruntime_la_LDFLAGS): Add -dstartfiles -Bgcc -B../src.
        (libgdruntime_la_DEPENDENCIES): New variable.
        * src/Makefile.am (libgphobos_la_LDFLAGS): Add -dstartfiles
        -B../libdruntime/gcc.
        * libdruntime/Makefile.in, src/Makefile.in: Regenerate.
        * Makefile.in, testsuite/Makefile.in: Regenerate.
        * libdruntime/rt/sections_elf_shared.d (Minfo_Bracketing): Don't
        assert.
        * src/drtstuff.spec: New file.
        * src/libgphobos.spec.in (DRTSTUFF_SPEC): Substitute.
        (*lib): Only pass SPEC_PHOBOS_DEPS without -debuglib, -defaultlib,
        -nophoboslib.
        * testsuite/testsuite_flags.in <--gdcldflags> (GDCLDFLAGS): Add
        -B${BUILD_DIR}/libdruntime/gcc.

        * libdruntime/Makefile.am (unittest_static_LDFLAGS): Use -Wc
        instead of -Xcompiler.
        (libgdruntime_t_la_LDFLAGS): Likewise.
        (unittest_LDFLAGS): Likewise.
        * src/Makefile.am (unittest_static_LDFLAGS): Likewise.
        (libgphobos_t_la_LDFLAGS): Likewise.
        (unittest_LDFLAGS): Likewise.

        gcc/d:
        PR d/87864
        * lang.opt (dstartfiles): New option.
        * d-spec.cc (need_spec): New variable.
        (lang_specific_driver) <OPT_dstartfiles>: Enable need_spec.
        (lang_specific_pre_link): Also load libgphobos.spec if need_spec.

        gcc/testsuite:
        PR d/87864
        * lib/gdc.exp (gdc_link_flags): Add path to drtbegin.o/drtend.o if
        present.

# HG changeset patch
# Parent  2b02744cb551e91a3c5dc300f12ae168581adc34
Provide __start_minfo/__stop_minfo for linkers that don't (PR d/87864)

diff --git a/gcc/d/d-spec.cc b/gcc/d/d-spec.cc
--- a/gcc/d/d-spec.cc
+++ b/gcc/d/d-spec.cc
@@ -72,6 +72,9 @@ static phobos_action phobos_library = PH
    standard libraries.  */
 static bool need_phobos = true;
 
+/* If true, do load libgphobos.spec even if not needed otherwise.  */
+static bool need_spec = false;
+
 void
 lang_specific_driver (cl_decoded_option **in_decoded_options,
 		      unsigned int *in_decoded_options_count,
@@ -144,6 +147,10 @@ lang_specific_driver (cl_decoded_option 
 
       switch (decoded_options[i].opt_index)
 	{
+	case OPT_dstartfiles:
+	  need_spec = true;
+	  break;
+
 	case OPT_nostdlib:
 	case OPT_nodefaultlibs:
 	  phobos_library = PHOBOS_NOLINK;
@@ -491,7 +498,7 @@ lang_specific_driver (cl_decoded_option 
 int
 lang_specific_pre_link (void)
 {
-  if (phobos_library != PHOBOS_NOLINK && need_phobos)
+  if ((phobos_library != PHOBOS_NOLINK && need_phobos) || need_spec)
     do_spec ("%:include(libgphobos.spec)");
 
   return 0;
diff --git a/gcc/d/lang.opt b/gcc/d/lang.opt
--- a/gcc/d/lang.opt
+++ b/gcc/d/lang.opt
@@ -162,6 +162,10 @@ defaultlib=
 Driver Joined
 Default library to use instead of phobos.
 
+dstartfiles
+Driver
+Do link the standard D startup files in the compilation.
+
 -verbose
 D Alias(v)
 
diff --git a/gcc/testsuite/lib/gdc.exp b/gcc/testsuite/lib/gdc.exp
--- a/gcc/testsuite/lib/gdc.exp
+++ b/gcc/testsuite/lib/gdc.exp
@@ -119,6 +119,10 @@ proc gdc_link_flags { paths } {
     if { $gccpath != "" } {
         # Path to libgphobos.spec.
         append flags "-B${gccpath}/libphobos/src "
+	# Path to drtbegin.o/drtend.o.
+        if { [file exists "${gccpath}/libphobos/libdruntime/gcc/drtbegin.o"] } {
+            append flags "-B${gccpath}/libphobos/libdruntime/gcc "
+        }
 
         if { [file exists "${gccpath}/libphobos/src/.libs/libgphobos.a"] \
              || [file exists "${gccpath}/libphobos/src/.libs/libgphobos.${shlib_ext}"] } {
diff --git a/libphobos/configure.ac b/libphobos/configure.ac
--- a/libphobos/configure.ac
+++ b/libphobos/configure.ac
@@ -146,6 +146,14 @@ DRUNTIME_LIBRARIES_DLOPEN
 DRUNTIME_LIBRARIES_ZLIB
 DRUNTIME_INSTALL_DIRECTORIES
 
+# Add drtbegin.o/drtend.o to startfile/endfile specs in libgphobos.spec
+if test "$DCFG_MINFO_BRACKETING" = "false"; then
+    DRTSTUFF_SPEC=$srcdir/src/drtstuff.spec
+else
+    DRTSTUFF_SPEC=/dev/null
+fi
+AC_SUBST_FILE(DRTSTUFF_SPEC)
+
 # Add dependencies for libgphobos.spec file
 SPEC_PHOBOS_DEPS="$LIBS"
 AC_SUBST(SPEC_PHOBOS_DEPS)
diff --git a/libphobos/libdruntime/Makefile.am b/libphobos/libdruntime/Makefile.am
--- a/libphobos/libdruntime/Makefile.am
+++ b/libphobos/libdruntime/Makefile.am
@@ -74,6 +74,20 @@ endif
 if DRUNTIME_OS_SOLARIS
     ALL_DRUNTIME_COMPILE_DSOURCES += $(DRUNTIME_DSOURCES_SOLARIS)
 endif
+
+# Provide __start_minfo, __stop_minfo if linker doesn't.
+if !DRUNTIME_OS_MINFO_BRACKETING
+    DRTSTUFF = gcc/drtbegin.o gcc/drtend.o
+
+    toolexeclib_DATA = $(DRTSTUFF)
+
+gcc/drtbegin.o: gcc/drtstuff.c
+	$(COMPILE) -DDRT_BEGIN -c -o $@ $<
+
+gcc/drtend.o: gcc/drtstuff.c
+	$(COMPILE) -DDRT_END -c -o $@ $<
+endif
+
 # Generated by configure
 ALL_DRUNTIME_COMPILE_DSOURCES += $(DRUNTIME_DSOURCES_GENERATED)
 
@@ -104,14 +118,16 @@ endif
 toolexeclib_LTLIBRARIES = libgdruntime.la
 libgdruntime_la_SOURCES = $(ALL_DRUNTIME_SOURCES)
 libgdruntime_la_LIBTOOLFLAGS =
-libgdruntime_la_LDFLAGS = -Xcompiler -nophoboslib -version-info $(libtool_VERSION)
+libgdruntime_la_LDFLAGS = -Wc,-nophoboslib,-dstartfiles,-B../src,-Bgcc \
+    -version-info $(libtool_VERSION)
 libgdruntime_la_LIBADD = $(LIBATOMIC) $(LIBBACKTRACE)
+libgdruntime_la_DEPENDENCIES = $(DRTSTUFF)
 
 # For static unittest, link objects directly
 unittest_static_SOURCES = ../testsuite/test_runner.d $(DRUNTIME_CSOURCES) \
     $(DRUNTIME_SSOURCES)
 unittest_static_LIBTOOLFLAGS =
-unittest_static_LDFLAGS = -Xcompiler -nophoboslib
+unittest_static_LDFLAGS = -Wc,-nophoboslib
 unittest_static_LDADD = $(DRUNTIME_TEST_OBJECTS) $(LIBATOMIC) $(LIBBACKTRACE)
 EXTRA_unittest_static_DEPENDENCIES = $(DRUNTIME_TEST_OBJECTS)
 
@@ -120,14 +136,14 @@ libgdruntime_t_la_SOURCES = $(DRUNTIME_C
 libgdruntime_t_la_LIBTOOLFLAGS =
 # Automake by default does not generate shared libs for non-installed
 # libraries. Use -rpath to force shared lib generation.
-libgdruntime_t_la_LDFLAGS = -Xcompiler -nophoboslib -rpath /foo -shared
+libgdruntime_t_la_LDFLAGS = -Wc,-nophoboslib -rpath /foo -shared
 libgdruntime_t_la_LIBADD = $(DRUNTIME_TEST_LOBJECTS) $(LIBATOMIC) $(LIBBACKTRACE)
 EXTRA_libgdruntime_t_la_DEPENDENCIES = $(DRUNTIME_TEST_LOBJECTS)
 
 # For unittest
 unittest_SOURCES = ../testsuite/test_runner.d
 unittest_LIBTOOLFLAGS =
-unittest_LDFLAGS = -Xcompiler -nophoboslib
+unittest_LDFLAGS = -Wc,-nophoboslib
 unittest_LDADD = libgdruntime_t.la
 
 # Extra install and clean rules.
diff --git a/libphobos/libdruntime/gcc/drtstuff.c b/libphobos/libdruntime/gcc/drtstuff.c
new file mode 100644
--- /dev/null
+++ b/libphobos/libdruntime/gcc/drtstuff.c
@@ -0,0 +1,16 @@
+/* FIXME: Needs comment, copyright.  */
+
+/* Avoid interference with target without support for named sections.  */
+#ifdef __ELF__
+
+#ifdef DRT_BEGIN
+void *__start_minfo[]
+__attribute__((used, section("minfo"), aligned(sizeof(void *)))) = { };
+#endif
+
+#ifdef DRT_END
+void *__stop_minfo[]
+__attribute__((used, section("minfo"), aligned(sizeof(void *)))) = { };
+#endif
+
+#endif /* __ELF__ */
diff --git a/libphobos/libdruntime/rt/sections_elf_shared.d b/libphobos/libdruntime/rt/sections_elf_shared.d
--- a/libphobos/libdruntime/rt/sections_elf_shared.d
+++ b/libphobos/libdruntime/rt/sections_elf_shared.d
@@ -347,7 +347,6 @@ else
 ///////////////////////////////////////////////////////////////////////////////
 
 import gcc.config;
-static assert(Minfo_Bracketing, "Can't use _d_dso_registry interface");
 
 /*
  * This data structure is generated by the compiler, and then passed to
diff --git a/libphobos/m4/druntime/os.m4 b/libphobos/m4/druntime/os.m4
--- a/libphobos/m4/druntime/os.m4
+++ b/libphobos/m4/druntime/os.m4
@@ -180,6 +180,7 @@ AC_DEFUN([DRUNTIME_OS_MINFO_BRACKETING],
     [AC_MSG_RESULT([no])
      DCFG_MINFO_BRACKETING=false])
   AC_SUBST(DCFG_MINFO_BRACKETING)
+  AM_CONDITIONAL([DRUNTIME_OS_MINFO_BRACKETING], [test "$DCFG_MINFO_BRACKETING" = "true"])
   AC_LANG_POP([C])
 ])
 
diff --git a/libphobos/src/Makefile.am b/libphobos/src/Makefile.am
--- a/libphobos/src/Makefile.am
+++ b/libphobos/src/Makefile.am
@@ -57,14 +57,15 @@ toolexeclib_DATA = libgphobos.spec
 toolexeclib_LTLIBRARIES = libgphobos.la
 libgphobos_la_SOURCES = $(ALL_PHOBOS_SOURCES) $(ZLIB_SRC)
 libgphobos_la_LIBTOOLFLAGS =
-libgphobos_la_LDFLAGS = -Xcompiler -nophoboslib -version-info $(libtool_VERSION)
+libgphobos_la_LDFLAGS = -Wc,-nophoboslib,-dstartfiles,-B../libdruntime/gcc \
+    -version-info $(libtool_VERSION)
 libgphobos_la_LIBADD = ../libdruntime/libgdruntime.la
 libgphobos_la_DEPENDENCIES = libgphobos.spec
 
 # For static unittest, link objects directly
 unittest_static_SOURCES = ../testsuite/test_runner.d $(ZLIB_SRC)
 unittest_static_LIBTOOLFLAGS =
-unittest_static_LDFLAGS = -Xcompiler -nophoboslib -static-libtool-libs
+unittest_static_LDFLAGS = -Wc,-nophoboslib -static-libtool-libs
 unittest_static_LDADD = $(PHOBOS_TEST_OBJECTS) \
     ../libdruntime/libgdruntime.la
 EXTRA_unittest_static_DEPENDENCIES = $(PHOBOS_TEST_OBJECTS)
@@ -72,7 +73,7 @@ EXTRA_unittest_static_DEPENDENCIES = $(P
 # For unittest with dynamic library
 libgphobos_t_la_SOURCES = $(ZLIB_SRC)
 libgphobos_t_la_LIBTOOLFLAGS =
-libgphobos_t_la_LDFLAGS = -Xcompiler -nophoboslib -rpath /foo -shared
+libgphobos_t_la_LDFLAGS = -Wc,-nophoboslib -rpath /foo -shared
 libgphobos_t_la_LIBADD = $(PHOBOS_TEST_LOBJECTS) \
     ../libdruntime/libgdruntime.la
 EXTRA_libgphobos_t_la_DEPENDENCIES = $(PHOBOS_TEST_LOBJECTS)
@@ -80,7 +81,7 @@ EXTRA_libgphobos_t_la_DEPENDENCIES = $(P
 # For unittest
 unittest_SOURCES = ../testsuite/test_runner.d
 unittest_LIBTOOLFLAGS =
-unittest_LDFLAGS = -Xcompiler -nophoboslib -shared
+unittest_LDFLAGS = -Wc,-nophoboslib -shared
 unittest_LDADD = libgphobos_t.la ../libdruntime/libgdruntime.la
 
 # Extra install and clean rules.
diff --git a/libphobos/src/drtstuff.spec b/libphobos/src/drtstuff.spec
new file mode 100644
--- /dev/null
+++ b/libphobos/src/drtstuff.spec
@@ -0,0 +1,5 @@
+%rename startfile startfile_orig
+*startfile: %(startfile_orig) drtbegin.o%s
+
+%rename endfile endfile_orig
+*endfile: %(endfile_orig) drtend.o%s
diff --git a/libphobos/src/libgphobos.spec.in b/libphobos/src/libgphobos.spec.in
--- a/libphobos/src/libgphobos.spec.in
+++ b/libphobos/src/libgphobos.spec.in
@@ -6,6 +6,7 @@
 
 %rename link linkorig_gdc_renamed
 *link: %(linkorig_gdc_renamed) @OS_LINK_SPEC@
+@DRTSTUFF_SPEC@
 
 %rename lib liborig_gdc_renamed
-*lib: @SPEC_PHOBOS_DEPS@ %(liborig_gdc_renamed)
+*lib: %{debuglib|defaultlib|nophoboslib: ; :@SPEC_PHOBOS_DEPS@} %(liborig_gdc_renamed)
diff --git a/libphobos/testsuite/testsuite_flags.in b/libphobos/testsuite/testsuite_flags.in
--- a/libphobos/testsuite/testsuite_flags.in
+++ b/libphobos/testsuite/testsuite_flags.in
@@ -40,6 +40,7 @@ case ${query} in
       ;;
     --gdcldflags)
       GDCLDFLAGS="-B${BUILD_DIR}/src
+                  -B${BUILD_DIR}/libdruntime/gcc
                   -L${BUILD_DIR}/libdruntime/.libs
                   -L${BUILD_DIR}/src/.libs"
       echo ${GDCLDFLAGS}

Reply via email to