public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: "H.J. Lu" <hjl.tools@gmail.com>
To: binutils@sourceware.org
Cc: Alan Modra <amodra@gmail.com>,
	Florian Weimer <fweimer@redhat.com>,
	Kaylee Blake <klkblake@gmail.com>
Subject: [PATCH v4 2/7] ELF: Discard non-alloc sections without section header
Date: Tue,  6 Jun 2023 10:58:41 -0700	[thread overview]
Message-ID: <20230606175846.399377-3-hjl.tools@gmail.com> (raw)
In-Reply-To: <20230606175846.399377-1-hjl.tools@gmail.com>

Discard non-alloc sections when section headers are stripped.

bfd/

	PR ld/25617
	* elf.c (_bfd_elf_assign_file_positions_for_non_load): Skip
	non-load sections without section header.
	(_bfd_elf_write_object_contents): Don't set the sh_name field
	without section header.  Write out the .shstrtab section only
	if its sh_offset field isn't -1.

binutils/

	PR ld/25617
	* objcopy.c (is_strip_section_1): Remove non-alloc sections for
	--strip-section-headers.

ld/

	PR ld/25617
	* ldlang.c (lang_discard_section_p): Discard non-alloc sections
	if we are stripping section headers.
---
 bfd/elf.c          | 13 ++++++++++---
 binutils/objcopy.c |  7 ++++++-
 ld/ldlang.c        |  4 ++++
 3 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/bfd/elf.c b/bfd/elf.c
index 81eb3ef71fa..07ad15ddb2f 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -6597,6 +6597,10 @@ _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
   Elf_Internal_Ehdr *i_ehdrp;
   const struct elf_backend_data *bed;
 
+  /* Skip non-load sections without section header.  */
+  if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
+    return true;
+
   off = elf_next_file_pos (abfd);
 
   shdrpp = elf_elfsections (abfd);
@@ -6720,9 +6724,11 @@ _bfd_elf_write_object_contents (bfd *abfd)
   num_sec = elf_numsections (abfd);
   for (count = 1; count < num_sec; count++)
     {
-      i_shdrp[count]->sh_name
-	= _bfd_elf_strtab_offset (elf_shstrtab (abfd),
-				  i_shdrp[count]->sh_name);
+      /* Don't set the sh_name field without section header.  */
+      if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0)
+	i_shdrp[count]->sh_name
+	  = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
+				    i_shdrp[count]->sh_name);
       if (bed->elf_backend_section_processing)
 	if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
 	  return false;
@@ -6739,6 +6745,7 @@ _bfd_elf_write_object_contents (bfd *abfd)
   /* Write out the section header names.  */
   t = elf_tdata (abfd);
   if (elf_shstrtab (abfd) != NULL
+      && t->shstrtab_hdr.sh_offset != -1
       && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
 	  || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
     return false;
diff --git a/binutils/objcopy.c b/binutils/objcopy.c
index 6112bbb9bdb..9d6f31b4909 100644
--- a/binutils/objcopy.c
+++ b/binutils/objcopy.c
@@ -1377,6 +1377,11 @@ is_strip_section_1 (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
 	return true;
     }
 
+  /* Remove non-alloc sections for --strip-section-headers.  */
+  if (strip_section_headers
+      && (bfd_section_flags (sec) & SEC_ALLOC) == 0)
+    return true;
+
   if ((bfd_section_flags (sec) & SEC_DEBUGGING) != 0)
     {
       if (strip_symbols == STRIP_DEBUG
@@ -2730,7 +2735,7 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
 
       if (strip_section_headers)
 	{
-	  non_fatal (_("--strip_section_headers is unsupported on `%s'"),
+	  non_fatal (_("--strip-section-headers is unsupported on `%s'"),
 		     bfd_get_archive_filename (ibfd));
 	  return false;
 	}
diff --git a/ld/ldlang.c b/ld/ldlang.c
index 43ff43339a2..740fdb4ce2f 100644
--- a/ld/ldlang.c
+++ b/ld/ldlang.c
@@ -2520,6 +2520,10 @@ lang_discard_section_p (asection *section)
       && (flags & SEC_DEBUGGING) != 0)
     discard = true;
 
+  /* Discard non-alloc sections if we are stripping section headers.  */
+  else if (config.no_section_header && (flags & SEC_ALLOC) == 0)
+    discard = true;
+
   return discard;
 }
 
-- 
2.40.1


  parent reply	other threads:[~2023-06-06 17:58 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-06 17:58 [PATCH v4 0/7] ELF: Strip section header in ELF objects H.J. Lu
2023-06-06 17:58 ` [PATCH v4 1/7] " H.J. Lu
2023-06-06 17:58 ` H.J. Lu [this message]
2023-06-06 17:58 ` [PATCH v4 3/7] bfd: Improve nm and objdump without section header H.J. Lu
2023-07-01  2:12   ` Simon Marchi
2023-07-07 15:26     ` H.J. Lu
2023-07-10  3:30       ` Simon Marchi
2023-07-13  5:02         ` Alan Modra
2023-07-13  5:34           ` Fangrui Song
2023-07-13 21:58           ` Mark Wielaard
2023-07-19  6:21             ` Alan Modra
2023-06-06 17:58 ` [PATCH v4 4/7] ld: Add simple tests for -z nosectionheader H.J. Lu
2023-06-06 17:58 ` [PATCH v4 5/7] binutils: Add a --strip-section-headers test H.J. Lu
2023-06-06 17:58 ` [PATCH v4 6/7] ld: Add tests for -z nosectionheader and --strip-section-headers H.J. Lu
2023-06-29 20:56   ` H.J. Lu
2023-07-06  1:27     ` Alan Modra
2023-06-06 17:58 ` [PATCH v4 7/7] ld: Add -z nosectionheader test to bootstrap.exp H.J. Lu

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=20230606175846.399377-3-hjl.tools@gmail.com \
    --to=hjl.tools@gmail.com \
    --cc=amodra@gmail.com \
    --cc=binutils@sourceware.org \
    --cc=fweimer@redhat.com \
    --cc=klkblake@gmail.com \
    /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).