The Solaris Compact C Type Format, CTF, was introduced back in Solaris
9.  It is the precursor to current GNU CTF, meant primarily for tools
like the low-level debugger mdb or DTrace that would like to avoid the
overhead of full DWARF-2 debugging information.  However, for a long
time creation required separate steps to convert DWARF information
(version 2 only) to CTF in the input objects and later merge this into
the final objects.  The tools to do so were available, but they were
barely documented and their use restricted to the core OS because of
this difficulty.

There's recently been a massive effort to simplify this and allow for
wider adoption.  The native linker has been extended to take GNU CTF
info in the input objects and convert that to Solaris CTF itself.  At
the same time, the massively enhanced tools and the format itself are
fully documented.

To make this even simpler to use, this patch introduces a new -gsctf
option to hide the details from users.  At compile time, it just passes
-gctf to the compiler, and at link time it invokes ld with -z ctf.

Bootstrapped without regressions on i386-pc-solaris2.11 and
sparc-sun-solaris2.11.


I've also run a full C/C++-only bootstrap where -gsctf is added to
STAGE[23]_[CT]FLAGS, together with a one-line patch to allow
suppression of the 

cc1plus: note: CTF debug info requested, but not supported for ‘GNU C++17’ 
frontend

warning via -Wno-complain-wrong-lang.  That warning would otherwise
break libstdc++ testing (PR debug/123259).

It would even be possible to support GNU ld by invoking the ctfmerge
utility in a post-link step.  However, this currently fails because
everything passed via -Wl (usually linker options) is passed to the
post-link command unchanged.  ctfmerge cannot handle this, of course.

        Rainer

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


2025-11-23  Rainer Orth  <[email protected]>

        gcc:
        * configure.ac (gcc_cv_ld_ctf): New check.
        (gcc_cv_have_ctfmerge): Likewise.
        * configure: Regenerate.
        * config.in: Regenerate.

        * config/sol2.h (SCTF_CC1_SPEC): Define.
        (LINK_SCTF_SPEC): Define.
        (LINK_SPEC): Add LINK_SCTF_SPEC.
        * config/i386/sol2.h (CC1_SPEC): Add SCTF_CC1_SPEC.
        * config/sparc/sol2.h: Likewise.
        * config/sol2.opt (gsctf): Declare.
        * config/sol2.opt.urls: Regenerate.

        * doc/invoke.texi (Option Summary): Add -gsctf.
        (Solaris 2 Options): Document it.

# HG changeset patch
# Parent  736dc6ffd2cf221c7e42417d105c752e69c839d2
Support Solaris CTF generation

diff --git a/gcc/config.in b/gcc/config.in
--- a/gcc/config.in
+++ b/gcc/config.in
@@ -1804,6 +1804,12 @@
 #endif
 
 
+/* Define if your linker supports -z ctf. */
+#ifndef USED_FOR_TARGET
+#undef HAVE_LD_CTF
+#endif
+
+
 /* Define if your linker supports --demangle option. */
 #ifndef USED_FOR_TARGET
 #undef HAVE_LD_DEMANGLE
diff --git a/gcc/config/i386/sol2.h b/gcc/config/i386/sol2.h
--- a/gcc/config/i386/sol2.h
+++ b/gcc/config/i386/sol2.h
@@ -55,7 +55,7 @@ along with GCC; see the file COPYING3.  
 #define CPP_SPEC "%(cpp_subtarget)"
 
 #undef CC1_SPEC
-#define CC1_SPEC "%(cc1_cpu) " ASAN_CC1_SPEC \
+#define CC1_SPEC "%(cc1_cpu) " ASAN_CC1_SPEC SCTF_CC1_SPEC \
   " %{mx32:%e-mx32 is not supported on Solaris}"
 
 /* GNU as understands --32 and --64, but the native Solaris
diff --git a/gcc/config/sol2.h b/gcc/config/sol2.h
--- a/gcc/config/sol2.h
+++ b/gcc/config/sol2.h
@@ -357,13 +357,29 @@ along with GCC; see the file COPYING3.  
 #define LINK_CLEARCAP_SPEC ""
 #endif
 
+/* Convenience alias for Solaris CTF generation.  */
+#ifdef HAVE_LD_CTF
+#define SCTF_CC1_SPEC " %{gsctf:-gctf} %<gsctf"
+#else
+#define SCTF_CC1_SPEC " %{gsctf:%e-gsctf is not supported in this configuration}"
+#endif
+
+/* How to generate Solaris CTF.  */
+#ifdef HAVE_LD_CTF
+/* Direct linker support.  */
+#define LINK_SCTF_SPEC " %{gsctf:-z ctf}"
+#else
+#define LINK_SCTF_SPEC \
+  " %{gsctf:%e-gsctf is not supported in this configuration}"
+#endif
+
 #undef  LINK_SPEC
 #define LINK_SPEC \
   "%{h*} %{v:-V} \
    %{!shared:%{!static:%{rdynamic: " RDYNAMIC_SPEC "}}} \
    %{static:-dn -Bstatic} \
    %{shared:-G -dy %{!mimpure-text:-z text}} " \
-   LINK_LIBGCC_MAPFILE_SPEC LINK_CLEARCAP_SPEC " \
+   LINK_LIBGCC_MAPFILE_SPEC LINK_CLEARCAP_SPEC LINK_SCTF_SPEC " \
    %{symbolic:-Bsymbolic -G -dy -z text} \
    %(link_arch) \
    %{Qy:} %{!Qn:-Qy}"
diff --git a/gcc/config/sol2.opt b/gcc/config/sol2.opt
--- a/gcc/config/sol2.opt
+++ b/gcc/config/sol2.opt
@@ -27,6 +27,10 @@ Driver Joined
 Ym,
 Driver Joined
 
+gsctf
+Driver RejectNegative
+Generate Solaris CTF.
+
 mclear-hwcap
 Target
 Clear hardware capabilities when linking.
diff --git a/gcc/config/sol2.opt.urls b/gcc/config/sol2.opt.urls
--- a/gcc/config/sol2.opt.urls
+++ b/gcc/config/sol2.opt.urls
@@ -3,6 +3,9 @@
 G
 UrlSuffix(gcc/System-V-Options.html#index-G-5)
 
+gsctf
+UrlSuffix(gcc/Solaris-2-Options.html#index-gsctf)
+
 mclear-hwcap
 UrlSuffix(gcc/Solaris-2-Options.html#index-mclear-hwcap)
 
diff --git a/gcc/config/sparc/sol2.h b/gcc/config/sparc/sol2.h
--- a/gcc/config/sparc/sol2.h
+++ b/gcc/config/sparc/sol2.h
@@ -240,7 +240,7 @@ extern const char *host_detect_local_cpu
 %{m64:%{m32:%emay not use both -m32 and -m64}} \
 %{m64:-mptr64 -mstack-bias -mno-v8plus \
   %{!mcpu*:-%{!mv8plus:mcpu=v9}}} \
-" ASAN_CC1_SPEC
+" ASAN_CC1_SPEC SCTF_CC1_SPEC
 #else
 #define CC1_SPEC "\
 %{m32:%{m64:%emay not use both -m32 and -m64}} \
@@ -248,7 +248,7 @@ extern const char *host_detect_local_cpu
   %{!mcpu*:%{!mv8plus:-mcpu=v9}}} \
 %{mv8plus:-m32 -mptr32 -mno-stack-bias \
   %{!mcpu*:-mcpu=v9}} \
-" ASAN_CC1_SPEC
+" ASAN_CC1_SPEC SCTF_CC1_SPEC
 #endif
 
 /* Support for a compile-time default CPU, et cetera.  The rules are:
diff --git a/gcc/configure b/gcc/configure
--- a/gcc/configure
+++ b/gcc/configure
@@ -33235,6 +33235,21 @@ cat >>confdefs.h <<_ACEOF
 { $as_echo "$as_me:${as_lineno-$LINENO}: result: $gcc_cv_ld_compress_debug" >&5
 $as_echo "$gcc_cv_ld_compress_debug" >&6; }
 
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking linker support for -z ctf" >&5
+$as_echo_n "checking linker support for -z ctf... " >&6; }
+gcc_cv_ld_ctf=no
+# Check for Solaris ld 1.3315 introduced in Solaris 11.4 SRU 84.  ld -z ctf
+# already went in in SRU 81, but lacked GNU CTF to Solaris CTF conversion.
+if $gcc_cv_ld --help 2>&1 | grep -- '-z ctf' > /dev/null \
+  && test x$gnu_ld = xno && test "$ld_vers_minor" -ge 3315; then
+  gcc_cv_ld_ctf=yes
+
+$as_echo "#define HAVE_LD_CTF 1" >>confdefs.h
+
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $gcc_cv_ld_ctf" >&5
+$as_echo "$gcc_cv_ld_ctf" >&6; }
+
 if test x"$ld64_flag" = x"yes"; then
 
   # Set defaults for possibly untestable items.
diff --git a/gcc/configure.ac b/gcc/configure.ac
--- a/gcc/configure.ac
+++ b/gcc/configure.ac
@@ -6449,6 +6449,17 @@ AC_DEFINE_UNQUOTED(LD_COMPRESS_DEBUG_OPT
 [Define to the linker option to enable compressed debug sections.])
 AC_MSG_RESULT($gcc_cv_ld_compress_debug)
 
+AC_MSG_CHECKING(linker support for -z ctf)
+gcc_cv_ld_ctf=no
+# Check for Solaris ld 1.3315 introduced in Solaris 11.4 SRU 84.  ld -z ctf
+# already went in in SRU 81, but lacked GNU CTF to Solaris CTF conversion.
+if $gcc_cv_ld --help 2>&1 | grep -- '-z ctf' > /dev/null \
+  && test x$gnu_ld = xno && test "$ld_vers_minor" -ge 3315; then
+  gcc_cv_ld_ctf=yes
+  AC_DEFINE(HAVE_LD_CTF, 1, [Define if your linker supports -z ctf.])
+fi
+AC_MSG_RESULT($gcc_cv_ld_ctf)
+
 if test x"$ld64_flag" = x"yes"; then
 
   # Set defaults for possibly untestable items.
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -1450,7 +1450,7 @@ See RS/6000 and PowerPC Options.
 
 @emph{Solaris 2 Options} (@ref{Solaris 2 Options})
 @gccoptlist{-mclear-hwcap  -mno-clear-hwcap  -mimpure-text  -mno-impure-text
--pthreads}
+-gsctf -pthreads}
 
 @emph{SPARC Options} (@ref{SPARC Options})
 @gccoptlist{-mcpu=@var{cpu-type}
@@ -34513,6 +34513,12 @@ using @option{-mimpure-text}, you should
 These switches are supported in addition to the above on Solaris 2:
 
 @table @gcctabopt
+@opindex gsctf
+@item -gsctf
+Generate Solaris CTF.  Needs to be used both for compilation and
+linking.  See @command{ctf(7)} for more information.  This is only
+supported since Solaris 11.4 SRU 84 where the necessary toolchain
+support was added.
 @opindex pthreads
 @item -pthreads
 This is a synonym for @option{-pthread}.

Reply via email to