public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/vendors/ARM/heads/morello)] except.c: Fix up switching to .data.rel.ro for purecap
@ 2022-07-12 10:36 Alex Coplan
  0 siblings, 0 replies; only message in thread
From: Alex Coplan @ 2022-07-12 10:36 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:66f714a213a282bbb95b4bb2a573115e021f31ac

commit 66f714a213a282bbb95b4bb2a573115e021f31ac
Author: Alex Coplan <alex.coplan@arm.com>
Date:   Wed Jun 15 16:41:06 2022 +0100

    except.c: Fix up switching to .data.rel.ro for purecap
    
    C++ templates use COMDAT groups to keep all codegen artifacts related to
    a single template instantiation together, so they can all be discarded
    or kept together.
    
    On purecap Morello, we introduced a switch to the .data.rel.ro section
    which happens halfway through outputting the .gcc_except_table section.
    However, this switch to .data.rel.ro didn't preserve any COMDAT group
    for the current_function_decl, so the .chericap we would insert in the
    .data.rel.ro section wasn't cleaned up by the linker's COMDAT group
    handling. This could lead to "dangerous relocation: out of range" errors
    as we would have stale R_MORELLO_CAPINIT relocations being applied to
    incompatible different instantiations of the same template.
    
    Notably the switch_to_exception_section function already has logic to
    apply the COMDAT group for the current function to the .gcc_except_table
    section we switch to, so this patch reuses this logic to do the same
    when we switch to the .data.rel.ro section.
    
    gcc/ChangeLog:
    
            * except.c (switch_to_relro_section): New. Use it ...
            (dw2_output_call_site_table): ... here.
            (get_eh_named_section): New. Use it ...
            (switch_to_exception_section): ... here.

Diff:
---
 gcc/except.c | 69 ++++++++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 46 insertions(+), 23 deletions(-)

diff --git a/gcc/except.c b/gcc/except.c
index 09525244dc5..32f16ef6e78 100644
--- a/gcc/except.c
+++ b/gcc/except.c
@@ -223,6 +223,7 @@ static int sjlj_size_of_call_site_table (void);
 static void dw2_output_call_site_table (int, int);
 static void sjlj_output_call_site_table (void);
 static void switch_to_exception_section (const char * );
+static void switch_to_relro_section (const char *);
 
 \f
 void
@@ -2933,8 +2934,7 @@ dw2_output_call_site_table (int cs_format, int section)
 	      ASM_OUTPUT_LABEL (asm_out_file, reg_cap_offs_lab);
 	      /* Then switch to the relocatable RO data section, build the
 		 capability as a POINTER_PLUS and output the capability.  */
-	      switch_to_section (get_named_section (NULL, ".data.rel.ro",
-						    0));
+	      switch_to_relro_section (name);
 	      ASM_GENERATE_INTERNAL_LABEL (cap_lab, "Llpcap",
 					   call_site_base + i);
 	      ASM_OUTPUT_LABEL (asm_out_file, cap_lab);
@@ -2994,10 +2994,50 @@ sjlj_output_call_site_table (void)
   call_site_base += n;
 }
 
+static section *
+get_eh_named_section (const char *base_sec_name,
+		      unsigned flags,
+		      const char * ARG_UNUSED (fnname),
+		      section **cache = NULL)
+{
+  section *s;
+#ifdef HAVE_LD_EH_GC_SECTIONS
+  if (flag_function_sections
+      || (DECL_COMDAT_GROUP (current_function_decl) && HAVE_COMDAT_GROUP))
+    {
+      char *section_name = XNEWVEC (char,
+				    strlen (base_sec_name) + 1 /* Dot.  */
+				    + strlen (fnname) + 1 /* NUL.  */);
+      /* The EH section must match the code section, so only mark
+	 it linkonce if we have COMDAT groups to tie them together.  */
+      if (DECL_COMDAT_GROUP (current_function_decl) && HAVE_COMDAT_GROUP)
+	flags |= SECTION_LINKONCE;
+      sprintf (section_name, "%s.%s", base_sec_name, fnname);
+      s = get_section (section_name, flags, current_function_decl);
+      free (section_name);
+    }
+  else
+#endif
+    {
+      s = get_section (base_sec_name, flags, NULL);
+      if (cache)
+	*cache = s;
+    }
+  return s;
+}
+
+static void
+switch_to_relro_section (const char *fnname)
+{
+  gcc_assert (targetm_common.have_named_sections);
+  unsigned flags = SECTION_WRITE | SECTION_RELRO;
+  switch_to_section (get_eh_named_section (".data.rel.ro", flags, fnname));
+}
+
 /* Switch to the section that should be used for exception tables.  */
 
 static void
-switch_to_exception_section (const char * ARG_UNUSED (fnname))
+switch_to_exception_section (const char * fnname)
 {
   section *s;
 
@@ -3005,7 +3045,7 @@ switch_to_exception_section (const char * ARG_UNUSED (fnname))
     s = exception_section;
   else
     {
-      int flags;
+      unsigned flags;
 
       if (EH_TABLES_CAN_BE_READ_ONLY)
 	{
@@ -3022,25 +3062,8 @@ switch_to_exception_section (const char * ARG_UNUSED (fnname))
       /* Compute the section and cache it into exception_section,
 	 unless it depends on the function name.  */
       if (targetm_common.have_named_sections)
-	{
-#ifdef HAVE_LD_EH_GC_SECTIONS
-	  if (flag_function_sections
-	      || (DECL_COMDAT_GROUP (current_function_decl) && HAVE_COMDAT_GROUP))
-	    {
-	      char *section_name = XNEWVEC (char, strlen (fnname) + 32);
-	      /* The EH table must match the code section, so only mark
-		 it linkonce if we have COMDAT groups to tie them together.  */
-	      if (DECL_COMDAT_GROUP (current_function_decl) && HAVE_COMDAT_GROUP)
-		flags |= SECTION_LINKONCE;
-	      sprintf (section_name, ".gcc_except_table.%s", fnname);
-	      s = get_section (section_name, flags, current_function_decl);
-	      free (section_name);
-	    }
-	  else
-#endif
-	    exception_section
-	      = s = get_section (".gcc_except_table", flags, NULL);
-	}
+	s = get_eh_named_section (".gcc_except_table", flags, fnname,
+				  &exception_section);
       else
 	exception_section
 	  = s = flags == SECTION_WRITE ? data_section : readonly_data_section;


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-07-12 10:36 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-12 10:36 [gcc(refs/vendors/ARM/heads/morello)] except.c: Fix up switching to .data.rel.ro for purecap Alex Coplan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).