[build, c++, lto] Support COMDAT group with Sun as (PR target/40483)

Rainer Orth ro@CeBiTec.Uni-Bielefeld.DE
Mon Jun 7 18:49:00 GMT 2010


Jason Merrill <jason@redhat.com> writes:

>> ** The C++ frontend generates an artificial identifier as group
>>     signature in cp/optimize.c (cdtor_comdat_group), which isn't ever
>>     defined (and doesn't demangle at all).  To solve that problem, I've
>>     decided to set a flag on that identifier which config/sol2.c
>>     (solaris_elf_asm_comdat_section) can use to decide if it needs to
>>     define the symbol.  I've chosen IDENTIFIER_CTOR_OR_DTOR_P since it
>>     seemed to make most sense and the obvious DECL_ARTIFICIAL cannot be
>>     set on an IDENTIFIER_NODE.  The complication is that I cannot simply
>>     include cp/cp-tree.h in sol2.c since all non-C++ compilers fail to
>>     link.  So I've cheated and use the underlying TREE_LANG_FLAG_3
>>     instead.  This is clearly a hack, but worked for me.
>
> Yeah, we clearly can't use TREE_LANG_FLAG in general.  There are still
> plenty of unused language-independent flags on IDENTIFIER_NODE, though it
> seems undesirable to use one for a corner case like this.
>
> How about having sparc.c remember the names of comdat groups and then at
> EOF emit dummy definitions for any that don't have TREE_SYMBOL_REFERENCED
> set?

That was an excellent suggestion: works nicely for the C++ case and also
fixes the LTO failures I had.

Here's the patch I've tested, although it took quite some time fighting
with hashtab.[ch].  With maybe two exceptions, it only touches code I
can approve myself:

* The i386/i386.c changes, although they are done in a way which only
  affects Solaris.

* The sparc/sparc.c change, though again it is inside a Solaris-specific
  function.

Apart from the new way to detect undefined signature symbols, there's
one other change prompted by SPARC testing: the two architectures
differ in that SPARC as needs double-quoted section names, while x86 as
does not.  I had to deal with this difference already for
PUSHSECTION_FORMAT, but instead of defining yet another macro, I've
generalized this to introduce and use a new SECTION_NAME_FORMAT_INSTEAD.

Bootstrapped without regressions on i386-pc-solaris2.11,
sparc-sun-solaris2.11 bootstrap to follow.

I won't commit this yet, though, since due to a Sun assembler bug on
both architectures I get some link failures, both building libstdc++.so
and during the testsuite run.  This can be worked around for the moment
by building with LD_OPTIONS='-z relaxreloc', but I've agreed with the
Sun linker maintainers that this isn't desirable.  As soon as I've got
fixed assemblers, I'll add a configure test to detect that.

As I've said, I could probably commit this patch without further
approval, but would like others to look over it to check if I might have
missed something.

	Rainer

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


2010-03-07  Rainer Orth  <ro@CeBiTec.Uni-Bielefeld.DE>

	PR target/40483
	* configure.ac (gcc_cv_as_comdat_group_group): Check for Sun as
	COMDAT group syntax, both SPARC and x86 variants.
	(HAVE_COMDAT_GROUP): Also define if gcc_cv_as_comdat_group_group.
	* configure: Regenerate.
	* config/sol2.h (TARGET_SOLARIS): Define.
	(PUSHSECTION_FORMAT): Remove.
	(SECTION_NAME_FORMAT): Define.
	* config/sol2.c: Include target.h, hashtab.h.
	(solaris_output_init_fini): Replace PUSHSECTION_FORMAT by its
	expansion, using SECTION_NAME_FORMAT.
	(solaris_comdat_htab): New variable.
	(struct comdat_entry): Define.
	(comdat_hash): New function.
	(comdat_eq): New function.
	(solaris_elf_asm_comdat_section): New function.
	(solaris_define_comdat_signature): New function.
	(solaris_code_end): New function.
	* config/sol2-protos.h (solaris_elf_asm_comdat_section): Declare.
	(solaris_code_end): Declare.
	* config/t-sol2 (sol2.o): Add target.h, $HASHTAB_H dependencies.
	* config/i386/i386.c (ix86_code_end) [TARGET_SOLARIS]: Call
	solaris_code_end.
	(i386_solaris_elf_named_section) [!USE_GAS]: Call
	solaris_elf_asm_comdat_section for SECTION_LINKONCE sections if
	HAVE_COMDAT_GROUP.
	* config/sparc/sparc.c (sparc_solaris_elf_asm_named_section):
	Likewise.
	* config/i386/sol2-10.h (TARGET_ASM_NAMED_SECTION): Moved ...
	* config/i386/sol2.h (TARGET_ASM_NAMED_SECTION): ... here.
	* config/sparc/sol2.h (TARGET_ASM_CODE_END): Redefine.
	(PUSHSECTION_FORMAT): Remove.
	(SECTION_NAME_FORMAT): Redefine.

diff -r 279b577e5bf6 gcc/config/i386/i386.c
--- a/gcc/config/i386/i386.c	Mon Jun 07 13:23:04 2010 +0200
+++ b/gcc/config/i386/i386.c	Mon Jun 07 13:23:57 2010 +0200
@@ -7720,6 +7720,10 @@
   rtx xops[2];
   int regno;
 
+#ifdef TARGET_SOLARIS
+  solaris_code_end ();
+#endif
+
   for (regno = 0; regno < 8; ++regno)
     {
       char name[32];
@@ -28609,6 +28613,15 @@
 	       flags & SECTION_WRITE ? "aw" : "a");
       return;
     }
+
+#ifndef USE_GAS
+  if (HAVE_COMDAT_GROUP && flags & SECTION_LINKONCE)
+    {
+      solaris_elf_asm_comdat_section (name, flags, decl);
+      return;
+    }
+#endif
+
   default_elf_asm_named_section (name, flags, decl);
 }
 
diff -r 279b577e5bf6 gcc/config/i386/sol2-10.h
--- a/gcc/config/i386/sol2-10.h	Mon Jun 07 13:23:04 2010 +0200
+++ b/gcc/config/i386/sol2-10.h	Mon Jun 07 13:23:57 2010 +0200
@@ -143,9 +143,6 @@
 /* We do not need to search a special directory for startup files.  */
 #undef MD_STARTFILE_PREFIX
 
-#undef TARGET_ASM_NAMED_SECTION
-#define TARGET_ASM_NAMED_SECTION i386_solaris_elf_named_section
-
 #undef SUBTARGET_RETURN_IN_MEMORY
 #define SUBTARGET_RETURN_IN_MEMORY(TYPE, FNTYPE) \
 	ix86_sol10_return_in_memory (TYPE, FNTYPE)
diff -r 279b577e5bf6 gcc/config/i386/sol2.h
--- a/gcc/config/i386/sol2.h	Mon Jun 07 13:23:04 2010 +0200
+++ b/gcc/config/i386/sol2.h	Mon Jun 07 13:23:57 2010 +0200
@@ -150,6 +150,9 @@
     }								\
   while (0)
 
+#undef TARGET_ASM_NAMED_SECTION
+#define TARGET_ASM_NAMED_SECTION i386_solaris_elf_named_section
+
 /* We do not need NT_VERSION notes.  */
 #undef X86_FILE_START_VERSION_DIRECTIVE
 #define X86_FILE_START_VERSION_DIRECTIVE false
diff -r 279b577e5bf6 gcc/config/sol2-protos.h
--- a/gcc/config/sol2-protos.h	Mon Jun 07 13:23:04 2010 +0200
+++ b/gcc/config/sol2-protos.h	Mon Jun 07 13:23:57 2010 +0200
@@ -22,3 +22,5 @@
 extern void solaris_register_pragmas (void);
 extern void solaris_output_init_fini (FILE *, tree);
 extern void solaris_assemble_visibility (tree, int);
+extern void solaris_elf_asm_comdat_section (const char *, unsigned int, tree);
+extern void solaris_code_end (void);
diff -r 279b577e5bf6 gcc/config/sol2.c
--- a/gcc/config/sol2.c	Mon Jun 07 13:23:04 2010 +0200
+++ b/gcc/config/sol2.c	Mon Jun 07 13:23:57 2010 +0200
@@ -25,9 +25,11 @@
 #include "output.h"
 #include "tm.h"
 #include "rtl.h"
+#include "target.h"
 #include "tm_p.h"
 #include "toplev.h"
 #include "ggc.h"
+#include "hashtab.h"
 
 tree solaris_pending_aligns, solaris_pending_inits, solaris_pending_finis;
 
@@ -105,14 +107,14 @@
 {
   if (lookup_attribute ("init", DECL_ATTRIBUTES (decl)))
     {
-      fprintf (file, PUSHSECTION_FORMAT, ".init");
+      fprintf (file, "\t.pushsection\t" SECTION_NAME_FORMAT "\n", ".init");
       ASM_OUTPUT_CALL (file, decl);
       fprintf (file, "\t.popsection\n");
     }
 
   if (lookup_attribute ("fini", DECL_ATTRIBUTES (decl)))
     {
-      fprintf (file, PUSHSECTION_FORMAT, ".fini");
+      fprintf (file, "\t.pushsection\t" SECTION_NAME_FORMAT "\n", ".fini");
       ASM_OUTPUT_CALL (file, decl);
       fprintf (file, "\t.popsection\n");
     }
@@ -156,3 +158,130 @@
 	   "in this configuration; ignored");
 #endif
 }
+
+/* Hash table of group signature symbols.  */
+
+static htab_t solaris_comdat_htab;
+
+/* Group section information entry stored in solaris_comdat_htab.  */
+
+typedef struct comdat_entry
+{
+  const char *name;
+  unsigned int flags;
+  tree decl;
+  const char *sig;
+} comdat_entry;
+
+/* Helper routines for maintaining solaris_comdat_htab.  */
+
+static hashval_t
+comdat_hash (const void *p)
+{
+  const comdat_entry *entry = (const comdat_entry *) p;
+
+  return htab_hash_string (entry->sig);
+}
+
+static int
+comdat_eq (const void *p1, const void *p2)
+{
+  const comdat_entry *entry1 = (const comdat_entry *) p1;
+  const comdat_entry *entry2 = (const comdat_entry *) p2;
+
+  return strcmp (entry1->sig, entry2->sig) == 0;
+}
+
+/* Output assembly to switch to COMDAT group section NAME with attributes
+   FLAGS and group signature symbol DECL, using Sun as syntax.  */
+
+void
+solaris_elf_asm_comdat_section (const char *name, unsigned int flags, tree decl)
+{
+  const char *signature;
+  char *section;
+  comdat_entry entry, **slot;
+
+  if (TREE_CODE (decl) == IDENTIFIER_NODE)
+    signature = IDENTIFIER_POINTER (decl);
+  else
+    signature = IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl));
+
+  /* Sun as requires group sections to be fragmented, i.e. to have names of
+     the form <section>%<fragment>.  Strictly speaking this is only
+     necessary to support cc -xF, but is enforced globally in violation of
+     the ELF gABI.  We keep the section names generated by GCC (generally
+     of the form .text.<signature>) and append %<signature> to pacify as,
+     despite the redundancy.  */
+  section = concat (name, "%", signature, NULL);
+
+  /* Clear SECTION_LINKONCE flag so targetm.asm_out.named_section only
+     emits this as a regular section.  Emit section before .group
+     directive since Sun as treats undeclared sections as @progbits,
+     which conflicts with .bss* sections which are @nobits.  */
+  targetm.asm_out.named_section (section, flags & ~SECTION_LINKONCE, decl);
+  
+  /* Sun as separates declaration of a group section and of the group
+     itself, using the .group directive and the #comdat flag.  */
+  fprintf (asm_out_file, "\t.group\t%s," SECTION_NAME_FORMAT ",#comdat\n",
+	   signature, section);
+
+  /* Unlike GNU as, group signature symbols need to be defined explicitly
+     for Sun as.  With a few exceptions, this is already the case.  To
+     identify the missing ones without changing the affected frontents,
+     remember the signature symbols and emit those not marked
+     TREE_SYMBOL_REFERENCED in solaris_code_end.  */
+  if (solaris_comdat_htab == NULL)
+    solaris_comdat_htab = htab_create (37, comdat_hash, comdat_eq, NULL);
+
+  entry.sig = signature;
+  slot = (comdat_entry **) htab_find_slot (solaris_comdat_htab, &entry, INSERT);
+
+  if (slot != NULL && *slot == NULL)
+    {
+      *slot = XCNEW (comdat_entry);
+      /* Remember fragmented section name.  */
+      (*slot)->name = section;
+      /* Emit as regular section, .group declaration has already been done.  */
+      (*slot)->flags = flags & ~SECTION_LINKONCE;
+      (*slot)->decl = decl;
+      (*slot)->sig = signature;
+    }
+}
+
+/* Define unreferenced COMDAT group signature symbol corresponding to SLOT.  */
+
+static int
+solaris_define_comdat_signature (void **slot, void *aux ATTRIBUTE_UNUSED)
+{
+  comdat_entry *entry = *(comdat_entry **) slot;
+  tree decl = entry->decl;
+
+  if (TREE_CODE (decl) != IDENTIFIER_NODE)
+    decl = DECL_COMDAT_GROUP (decl);
+
+  if (!TREE_SYMBOL_REFERENCED (decl))
+    {
+      /* Switch to group section, otherwise Sun as complains
+	 `Group Id symbol defined outside of group'.  */
+      switch_to_section (get_section (entry->name, entry->flags, entry->decl));
+
+      targetm.asm_out.globalize_label (asm_out_file, entry->sig);
+      ASM_OUTPUT_LABEL (asm_out_file, entry->sig);
+      TREE_SYMBOL_REFERENCED (decl) = 1;
+    }
+
+  /* Continue with scan.  */
+  return 1;
+}
+
+/* Emit unreferenced COMDAT group signature symbols for Sun as.  */
+
+void
+solaris_code_end (void)
+{
+  if (solaris_comdat_htab == NULL)
+    return;
+
+  htab_traverse (solaris_comdat_htab, solaris_define_comdat_signature, NULL);
+}
diff -r 279b577e5bf6 gcc/config/sol2.h
--- a/gcc/config/sol2.h	Mon Jun 07 13:23:04 2010 +0200
+++ b/gcc/config/sol2.h	Mon Jun 07 13:23:57 2010 +0200
@@ -19,6 +19,9 @@
 along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
 
+/* We are compiling for Solaris 2 now.  */
+#define TARGET_SOLARIS 1
+
 /* We use stabs-in-elf for debugging, because that is what the native
    toolchain uses.  */
 #undef PREFERRED_DEBUGGING_TYPE
@@ -259,9 +262,8 @@
   { "init",      0, 0, true,  false,  false, NULL },			\
   { "fini",      0, 0, true,  false,  false, NULL }
 
-/* Solaris/x86 as and gas support the common ELF .section/.pushsection
-   syntax.  */
-#define PUSHSECTION_FORMAT	"\t.pushsection\t%s\n"
+/* Solaris/x86 as and gas support unquoted section names.  */
+#define SECTION_NAME_FORMAT	"%s"
 
 /* This is how to declare the size of a function.  For Solaris, we output
    any .init or .fini entries here.  */
diff -r 279b577e5bf6 gcc/config/sparc/sol2.h
--- a/gcc/config/sparc/sol2.h	Mon Jun 07 13:23:04 2010 +0200
+++ b/gcc/config/sparc/sol2.h	Mon Jun 07 13:23:57 2010 +0200
@@ -181,11 +181,15 @@
 #undef TARGET_ASM_NAMED_SECTION
 #define TARGET_ASM_NAMED_SECTION sparc_solaris_elf_asm_named_section
 
-/* Solaris/SPARC as uses a non-standard .section/.pushsection syntax.
-   While gas supports it, too, we prefer the standard variant.  */
+/* Emit COMDAT group signature symbols for Sun as.  */
+#undef TARGET_ASM_CODE_END
+#define TARGET_ASM_CODE_END solaris_code_end
+
+/* Solaris/SPARC as requires doublequoted section names.  While gas
+   supports that, too, we prefer the standard variant.  */
 #ifndef USE_GAS
-#undef PUSHSECTION_FORMAT
-#define PUSHSECTION_FORMAT	"\t.pushsection\t\"%s\"\n"
+#undef SECTION_NAME_FORMAT
+#define SECTION_NAME_FORMAT	"\"%s\""
 #endif
 
 #define MD_UNWIND_SUPPORT "config/sparc/sol2-unwind.h"
diff -r 279b577e5bf6 gcc/config/sparc/sparc.c
--- a/gcc/config/sparc/sparc.c	Mon Jun 07 13:23:04 2010 +0200
+++ b/gcc/config/sparc/sparc.c	Mon Jun 07 13:23:57 2010 +0200
@@ -8070,6 +8070,12 @@
 sparc_solaris_elf_asm_named_section (const char *name, unsigned int flags,
 				     tree decl ATTRIBUTE_UNUSED)
 {
+  if (HAVE_COMDAT_GROUP && flags & SECTION_LINKONCE)
+    {
+      solaris_elf_asm_comdat_section (name, flags, decl);
+      return;
+    }
+
   fprintf (asm_out_file, "\t.section\t\"%s\"", name);
 
   if (!(flags & SECTION_DEBUG))
diff -r 279b577e5bf6 gcc/config/t-sol2
--- a/gcc/config/t-sol2	Mon Jun 07 13:23:04 2010 +0200
+++ b/gcc/config/t-sol2	Mon Jun 07 13:23:57 2010 +0200
@@ -25,6 +25,6 @@
 
 # Solaris-specific attributes
 sol2.o: $(srcdir)/config/sol2.c $(CONFIG_H) $(SYSTEM_H) coretypes.h \
-  tree.h output.h $(TM_H) $(TM_P_H) toplev.h $(GGC_H)
+  tree.h output.h $(TM_H) target.h $(TM_P_H) toplev.h $(GGC_H) $(HASHTAB_H)
 	$(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) \
 	  $(srcdir)/config/sol2.c
diff -r 279b577e5bf6 gcc/configure.ac
--- a/gcc/configure.ac	Mon Jun 07 13:23:04 2010 +0200
+++ b/gcc/configure.ac	Mon Jun 07 13:23:57 2010 +0200
@@ -2524,10 +2524,36 @@
  [.section .text,"axG",@progbits,.foo,comdat])
 if test $gcc_cv_as_comdat_group = yes; then
   gcc_cv_as_comdat_group_percent=no
+  gcc_cv_as_comdat_group_group=no
 else
  gcc_GAS_CHECK_FEATURE(COMDAT group support, gcc_cv_as_comdat_group_percent,
    [elf,2,16,0], [--fatal-warnings],
    [.section .text,"axG",%progbits,.foo,comdat])
+ if test $gcc_cv_as_comdat_group_percent = yes; then
+   gcc_cv_as_comdat_group_group=no
+ else
+   # Sun as uses a completely different syntax.
+   case "${target}" in
+     sparc*-*-solaris2*)
+       gcc_GAS_CHECK_FEATURE(COMDAT group support,
+         gcc_cv_as_comdat_group_group,
+         ,,
+         [.group foo,".text%foo",#comdat
+          .section ".text%foo", #alloc,#execinstr,#progbits
+          .globl  foo
+          foo:])
+       ;;
+     i?86-*-solaris2*)
+       gcc_GAS_CHECK_FEATURE(COMDAT group support,
+         gcc_cv_as_comdat_group_group,
+         ,,
+         [.group foo,.text%foo,#comdat
+          .section .text%foo, "ax", @progbits
+          .globl  foo
+          foo:])
+       ;;
+   esac
+ fi
 fi
 if test x"$ld_is_gold" = xyes; then
   comdat_group=yes
@@ -2579,9 +2605,12 @@
 if test $comdat_group = no; then
   gcc_cv_as_comdat_group=no
   gcc_cv_as_comdat_group_percent=no
+  gcc_cv_as_comdat_group_group=no
 fi
 AC_DEFINE_UNQUOTED(HAVE_COMDAT_GROUP,
-  [`if test $gcc_cv_as_comdat_group = yes || test $gcc_cv_as_comdat_group_percent = yes; then echo 1; else echo 0; fi`],
+  [`if test $gcc_cv_as_comdat_group = yes \
+    || test $gcc_cv_as_comdat_group_percent = yes \
+    || test $gcc_cv_as_comdat_group_group = yes; then echo 1; else echo 0; fi`],
 [Define 0/1 if your assembler and linker support COMDAT groups.])
 
 gcc_GAS_CHECK_FEATURE([line table discriminator support],



More information about the Gcc-patches mailing list