[PATCH] Remove the stripped group section from linker output

Alan Modra amodra@gmail.com
Tue Feb 13 02:02:00 GMT 2018

On Mon, Feb 12, 2018 at 10:13:40AM -0800, H.J. Lu wrote:
> GCC 7 and above generates .debug_macro section in COMDAT group with
> -g3.  But ld fails to recognize that a group section shouldn't be in
> output when all of its members are stripped.  Update ld to remove the
> stripped group section from linker output when all members are removed
> by moving the stripped group section logic from objcopy.c to bfd.c so
> that "ld -r -S" behaves the same as "strip -g".
> OK for master?

I think that moving most of objcopy.c:is_strip_section into bfd is a
bad idea.  You've ended up with an ugly interface with two callback
functions needed by objcopy, and a function with confusing parameter

> +bfd_boolean
> +bfd_stripped_group_section_p
> +  (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
> +   bfd_boolean relocatable_link,
> +   bfd_boolean (*strip_group_section_p) (bfd *, asection *),
> +   bfd_boolean (*strip_section_p) (bfd *, asection *))
> +{
> +  if ((bfd_get_section_flags (abfd, sec) & SEC_GROUP) != 0)
> +    {
> +      asection *elt, *first;
> +
> +      /* PR binutils/3181
> +	 If we are going to strip the group signature symbol, then
> +	 strip the group section too.  */
> +      if (!relocatable_link && strip_group_section_p (abfd, sec))
> +	return TRUE;

When I first looked at the patch I thought "won't that segfault during
a final link?", because I'd seen that you pass NULL for the callback
in bfd_elf_final_link but hadn't realized that relocatable_link was
always false.  So the name "relocatable_link" is a lie, but I think it
would be better to leave objcopy.c alone and write a small function in
elflink.c that simply iterates over the group elements.

static bfd_boolean
is_discarded_group (asection *sec)
  asection *elt, *first;

  if ((sec->flags & SEC_GROUP) == 0)
    return FALSE;

  first = elt = elf_next_in_group (sec);
  while (elt != NULL)
      if (!discarded_section (elt))
	return FALSE;
      elt = elf_next_in_group (elt);
      if (elt == first)
  return TRUE;

I also think it would be a good idea to set SEC_EXCLUDE for the
stripped section, just to be consistent with what happens with other
stripped sections.

Alan Modra
Australia Development Lab, IBM

More information about the Binutils mailing list