public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Alan Modra <amodra@gmail.com>
To: binutils@sourceware.org
Subject: coff/pe/xcoff and --extract-symbols
Date: Fri, 28 Jul 2023 14:09:56 +0930	[thread overview]
Message-ID: <ZMNGnMF/epkJ1snn@squeak.grove.modra.org> (raw)

This fixes failure of the "extract symbols" test for rs6000, where
--extract-symbols generates a non-zero sized .text.  By the look of
coffcode.h the same problem might occur for coff/pe too, but doesn't
happen to trigger a test failure.

bfd/
	* coffcode.h (coff_compute_section_file_positions): Don't
	adjust size of !SEC_LOAD sections.
binutils/
	* objcopy.c (setup_section): Clear SEC_LOAD for --extract-symbol.

diff --git a/bfd/coffcode.h b/bfd/coffcode.h
index f37d029ef75..6c8080c841e 100644
--- a/bfd/coffcode.h
+++ b/bfd/coffcode.h
@@ -3176,8 +3176,9 @@ coff_compute_section_file_positions (bfd * abfd)
 	     created and not just improve performances with gdb.
 	  */
 
-	  if ((!strcmp (current->name, _TEXT)
-	       || !strcmp (current->name, _DATA))
+	  if ((current->flags & SEC_LOAD) != 0
+	      && (!strcmp (current->name, _TEXT)
+		  || !strcmp (current->name, _DATA))
 	      && (previous == NULL || strcmp(previous->name, _TDATA)))
 	    {
 	      bfd_vma align = 4096;
@@ -3190,7 +3191,8 @@ coff_compute_section_file_positions (bfd * abfd)
 		sofar += align + vma_off - sofar_off;
 	    }
 #endif
-	  if (previous != NULL)
+	  if (previous != NULL
+	      && (previous->flags & SEC_LOAD) != 0)
 	    previous->size += sofar - old_sofar;
 	}
 
diff --git a/binutils/objcopy.c b/binutils/objcopy.c
index beb655cafc2..b2b7ab6ab7e 100644
--- a/binutils/objcopy.c
+++ b/binutils/objcopy.c
@@ -4161,13 +4161,25 @@ setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
       flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
       flags = check_new_section_flags (flags, obfd, bfd_section_name (isection));
     }
-  else if (strip_symbols == STRIP_NONDEBUG
-	   && (flags & (SEC_ALLOC | SEC_GROUP)) != 0
-	   && !is_nondebug_keep_contents_section (ibfd, isection))
+  else
     {
-      flagword clr = SEC_HAS_CONTENTS | SEC_LOAD | SEC_GROUP;
-
-      if (bfd_get_flavour (obfd) == bfd_target_elf_flavour)
+      flagword clr = 0;
+
+      /* For --extract-symbols where section sizes are zeroed, clear
+	 SEC_LOAD to indicate to coff_compute_section_file_positions that
+	 section sizes should not be adjusted for ALIGN_SECTIONS_IN_FILE.
+	 We don't want to clear SEC_HAS_CONTENTS as that will result
+	 in symbols being classified as 'B' by nm.  */
+      if (extract_symbol)
+	clr = SEC_LOAD;
+      /* If only keeping debug sections then we'll be keeping section
+	 sizes in headers but making the sections have no contents.  */
+      else if (strip_symbols == STRIP_NONDEBUG
+	       && (flags & (SEC_ALLOC | SEC_GROUP)) != 0
+	       && !is_nondebug_keep_contents_section (ibfd, isection))
+	clr = SEC_HAS_CONTENTS | SEC_LOAD | SEC_GROUP;
+
+      if (clr && bfd_get_flavour (obfd) == bfd_target_elf_flavour)
 	{
 	  /* PR 29532: Copy group sections intact as otherwise we end up with
 	     empty groups.  This prevents separate debug info files from
@@ -4175,7 +4187,7 @@ setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
 	     originally contained groups.  */
 	  if (flags & SEC_GROUP)
 	    clr = SEC_LOAD;
-	  else
+	  if ((clr & SEC_HAS_CONTENTS) != 0)
 	    make_nobits = true;
 
 	  /* Twiddle the input section flags so that it seems to

-- 
Alan Modra
Australia Development Lab, IBM

                 reply	other threads:[~2023-07-28  4:40 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZMNGnMF/epkJ1snn@squeak.grove.modra.org \
    --to=amodra@gmail.com \
    --cc=binutils@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).