[PATCH] Workaround ld 2.18 --gc-sections issues (PR middle-end/46790)

Jakub Jelinek jakub@redhat.com
Tue Feb 22 12:12:00 GMT 2011


On Tue, Feb 22, 2011 at 09:24:49AM +0100, Jakub Jelinek wrote:
> GNU linkers around 2.18 don't handle --gc-sections together with
> Honza's recent changes where say main is put into
> .text.startup.main
> section instead of
> .text.main
> but .gcc_except_table hunks go into
> .gcc_except_table.main
> Even older linkers will just emit one big .gcc_except_table and don't
> have this problem.
> IMHO either we can add a workaround as in the patch below (this one
> will cause it to just use .text.main instead of .text.startup.main or
> .text.foo instead of .text.exit.foo like in 4.5 and earlier with old buggy
> linkers), or force .gcc_except_table instead of .gcc_except_table.main
> even for 2.18-ish linkers instead of just 2.17-ish and earlier
> (untested such patch is in the PR), or maybe (but would leave that to Honza)
> arrange for .gcc_except_table hunks in those cases to go into
> .gcc_except_table.startup.main or .gcc_except_table.exit.foo
> sections.

The last variant is implemented in this untested patch if you prefer it
instead (Honza prefers the HAVE_LD_EH_GC_SECTIONS_BUG variant):

2011-02-22  Jakub Jelinek  <jakub@redhat.com>

	PR middle-end/46790
	* except.c (switch_to_exception_section): If current function
	is being put into sections like .text.startup.main, .text.exit.foo,
	.text.unlikely.bar or .text.hot.baz, put exception table into
	.gcc_except_table.startup.main etc. instead of .gcc_except_table.main.

--- gcc/except.c.jj	2011-01-25 18:40:08.000000000 +0100
+++ gcc/except.c	2011-02-22 11:38:00.000000000 +0100
@@ -2854,8 +2854,32 @@ switch_to_exception_section (const char 
 #ifdef HAVE_LD_EH_GC_SECTIONS
 	  if (flag_function_sections)
 	    {
-	      char *section_name = XNEWVEC (char, strlen (fnname) + 32);
-	      sprintf (section_name, ".gcc_except_table.%s", fnname);
+	      char *section_name;
+	      const char *suffix = fnname;
+	      section *sect;
+	      /* If varasm decided to put the current function
+		 into special section like .text.startup.main or
+		 .text.exit.fnname, use .gcc_except_table.startup.main
+		 or .gcc_except_table.exit.fnname instead of
+		 .gcc_except_table.main or .gcc_except_table.fnname,
+		 otherwise --gc-sections might misbehave in older linkers.  */
+	      if (current_function_decl
+		  && DECL_HAS_IMPLICIT_SECTION_NAME_P (current_function_decl)
+		  && (sect = function_section (current_function_decl)) != NULL
+		  && SECTION_STYLE (sect) == SECTION_NAMED
+		  && sect->named.decl == current_function_decl
+		  && sect->named.name)
+		{
+		  size_t len = strlen (sect->named.name);
+		  size_t fnname_len = strlen (fnname);
+		  if (len > fnname_len + 6
+		      && strncmp (sect->named.name, ".text.", 6) == 0
+		      && memcmp (sect->named.name + len - fnname_len,
+				 fnname, fnname_len) == 0)
+		    suffix = sect->named.name + 6;
+		}
+	      section_name = XNEWVEC (char, strlen (suffix) + 32);
+	      sprintf (section_name, ".gcc_except_table.%s", suffix);
 	      s = get_section (section_name, flags, NULL);
 	      free (section_name);
 	    }

	Jakub



More information about the Gcc-patches mailing list