This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[RFC PATCH] gas .section directives with comdat inheritance


This patch adds a new magic flag to .section directives.
If ? appears among the "FLAGS" letters, then it means to
imply any G flag and NAME[,comdat] from the current section
to the new section.

This scenario I'm trying to get at is using inline asm to emit annotation
sections.  That is, C code will contain:

	asm("0: someinstruction\n"
	    ".pushsection .annotations,\"\"\n"
	    ".long 0b\n"
	    ".popsection");

This works fine in C and in lots of C++ code.  But when this appears inside
C++ methods that are being compiled into comdat sections, it breaks down.
We really need that to be doing:

	    .pushsection .annotations,"G",current_comdat_name,comdat

for whatever "current_comdat_name" the compiler is compiling to.
With this patch, it can do:

	asm("0: someinstruction\n"
	    ".pushsection .annotations,\"?\"\n"
	    ".long 0b\n"
	    ".popsection");

and this has the effect of either:
	    .pushsection .annotations,""
or 
	    .pushsection .annotations,"G",the_right_comdat_name,comdat
as appropriate.

There may be better ways to attack this on the compiler side.  But I
haven't really thought of them.  OTOH, this gas change was really easy to
get working.


Thanks,
Roland


2010-08-10  Roland McGrath  <roland@redhat.com>

	* config/obj-elf.c (obj_elf_parse_section_letters): Take new
	boolean result parameter CLONE; set it if '?' flag letter seen.
	(obj_elf_section): Update caller.  Handle that flag by copying
	the LINKONCE and GROUP_NAME state from NOW_SEG.
	* doc/as.texinfo (Section): Document the ? flag.

Index: gas/config/obj-elf.c
===================================================================
RCS file: /cvs/src/src/gas/config/obj-elf.c,v
retrieving revision 1.128
diff -u -r1.128 obj-elf.c
--- gas/config/obj-elf.c	15 Jul 2010 14:34:41 -0000	1.128
+++ gas/config/obj-elf.c	10 Aug 2010 22:17:30 -0000
@@ -741,9 +741,10 @@
 }
 
 static bfd_vma
-obj_elf_parse_section_letters (char *str, size_t len)
+obj_elf_parse_section_letters (char *str, size_t len, bfd_boolean *clone)
 {
   bfd_vma attr = 0;
+  *clone = FALSE;
 
   while (len > 0)
     {
@@ -773,6 +774,9 @@
 	case 'T':
 	  attr |= SHF_TLS;
 	  break;
+	case '?':
+	  *clone = TRUE;
+	  break;
 	/* Compatibility.  */
 	case 'm':
 	  if (*(str - 1) == 'a')
@@ -974,13 +978,15 @@
 
       if (*input_line_pointer == '"')
 	{
+	  bfd_boolean clone;
+
 	  beg = demand_copy_C_string (&dummy);
 	  if (beg == NULL)
 	    {
 	      ignore_rest_of_line ();
 	      return;
 	    }
-	  attr |= obj_elf_parse_section_letters (beg, strlen (beg));
+	  attr |= obj_elf_parse_section_letters (beg, strlen (beg), &clone);
 
 	  SKIP_WHITESPACE ();
 	  if (*input_line_pointer == ',')
@@ -1032,6 +1038,11 @@
 	      attr &= ~SHF_MERGE;
 	    }
 
+	  if ((attr & SHF_GROUP) != 0 && clone)
+	    {
+	      as_warn (_("? section flag ignored with G present"));
+	      clone = FALSE;
+	    }
 	  if ((attr & SHF_GROUP) != 0 && *input_line_pointer == ',')
 	    {
 	      ++input_line_pointer;
@@ -1051,6 +1062,16 @@
 	      as_warn (_("group name for SHF_GROUP not specified"));
 	      attr &= ~SHF_GROUP;
 	    }
+
+	  if (clone)
+	    {
+	      const char *now_group = elf_group_name (now_seg);
+	      if (now_group != NULL)
+		{
+		  group_name = xstrdup (now_group);
+		  linkonce = (now_seg->flags & SEC_LINK_ONCE) != 0;
+		}
+	    }
 	}
       else
 	{
Index: gas/doc/as.texinfo
===================================================================
RCS file: /cvs/src/src/gas/doc/as.texinfo,v
retrieving revision 1.221
diff -u -r1.221 as.texinfo
--- gas/doc/as.texinfo	9 Aug 2010 18:08:18 -0000	1.221
+++ gas/doc/as.texinfo	10 Aug 2010 22:17:31 -0000
@@ -5898,6 +5898,8 @@ section contains zero terminated strings
 section is a member of a section group
 @item T
 section is used for thread-local-storage
+@item ?
+section is a member of the previously-current section's group, if any
 @end table
 
 The optional @var{type} argument may contain one of the following constants:
@@ -5962,6 +5964,13 @@ the Merge flag should come first, like t
 .section @var{name} , "@var{flags}"MG, @@@var{type}, @var{entsize}, @var{GroupName}[, @var{linkage}]
 @end smallexample
 
+If @var{flags} contains the @code{?} symbol then it may not also contain the
+@code{G} symbol and the @var{GroupName} or @var{linkage} fields should not be
+present.  Instead, @code{?} says to consider the section that's current before
+this directive.  If that section used @code{G}, then the new section will use
+@code{G} with those same @var{GroupName} and @var{linkage} fields implicitly.
+If not, then the @code{?} symbol has no effect.
+
 If no flags are specified, the default flags depend upon the section name.  If
 the section name is not recognized, the default will be for the section to have
 none of the above flags: it will not be allocated in memory, nor writable, nor


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]