]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
ELF: Discard non-alloc sections without section header
authorH.J. Lu <hjl.tools@gmail.com>
Wed, 31 May 2023 19:36:49 +0000 (12:36 -0700)
committerH.J. Lu <hjl.tools@gmail.com>
Thu, 29 Jun 2023 17:29:47 +0000 (10:29 -0700)
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
binutils/objcopy.c
ld/ldlang.c

index 8f6d7d1adbaa90e518da90f6c8f557baabc44b45..4584b93a7ae0abd6643603a7569e74d12b899178 100644 (file)
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -6601,6 +6601,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);
@@ -6724,9 +6728,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;
@@ -6743,6 +6749,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;
index 206d80bd0f1f27e01e0d2c9a3bb7f3a34e903cb2..3569b890c7d74728c5277edc68d14ab12d1a631b 100644 (file)
@@ -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;
        }
index f4a8b72869eb7a803962bc72da0e5433d6a369f5..b3a89bf041b651bf335ee34e9b782f92fabf27f0 100644 (file)
@@ -2538,6 +2538,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;
 }