From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1039) id 6F0FB3858D39; Thu, 29 Jun 2023 17:36:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6F0FB3858D39 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: H.J. Lu To: bfd-cvs@sourceware.org Subject: [binutils-gdb] ELF: Discard non-alloc sections without section header X-Act-Checkin: binutils-gdb X-Git-Author: H.J. Lu X-Git-Refname: refs/heads/master X-Git-Oldrev: 96cc7918c18cdb7bd0331d836fa7f05341732580 X-Git-Newrev: 02c1ba6c94d4bb3f53dfeeb4401c8434c7834a32 Message-Id: <20230629173618.6F0FB3858D39@sourceware.org> Date: Thu, 29 Jun 2023 17:36:18 +0000 (GMT) X-BeenThere: binutils-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 29 Jun 2023 17:36:18 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D02c1ba6c94d4= bb3f53dfeeb4401c8434c7834a32 commit 02c1ba6c94d4bb3f53dfeeb4401c8434c7834a32 Author: H.J. Lu Date: Wed May 31 12:36:49 2023 -0700 ELF: Discard non-alloc sections without section header =20 Discard non-alloc sections when section headers are stripped. =20 bfd/ =20 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. =20 binutils/ =20 PR ld/25617 * objcopy.c (is_strip_section_1): Remove non-alloc sections for --strip-section-headers. =20 ld/ =20 PR ld/25617 * ldlang.c (lang_discard_section_p): Discard non-alloc sections if we are stripping section headers. Diff: --- 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 8f6d7d1adba..4584b93a7ae 100644 --- a/bfd/elf.c +++ b/bfd/elf.c @@ -6601,6 +6601,10 @@ _bfd_elf_assign_file_positions_for_non_load (bfd *ab= fd) Elf_Internal_Ehdr *i_ehdrp; const struct elf_backend_data *bed; =20 + /* Skip non-load sections without section header. */ + if ((abfd->flags & BFD_NO_SECTION_HEADER) !=3D 0) + return true; + off =3D elf_next_file_pos (abfd); =20 shdrpp =3D elf_elfsections (abfd); @@ -6724,9 +6728,11 @@ _bfd_elf_write_object_contents (bfd *abfd) num_sec =3D elf_numsections (abfd); for (count =3D 1; count < num_sec; count++) { - i_shdrp[count]->sh_name - =3D _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) =3D=3D 0) + i_shdrp[count]->sh_name + =3D _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 =3D elf_tdata (abfd); if (elf_shstrtab (abfd) !=3D NULL + && t->shstrtab_hdr.sh_offset !=3D -1 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) !=3D 0 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) return false; diff --git a/binutils/objcopy.c b/binutils/objcopy.c index 206d80bd0f1..3569b890c7d 100644 --- a/binutils/objcopy.c +++ b/binutils/objcopy.c @@ -1377,6 +1377,11 @@ is_strip_section_1 (bfd *abfd ATTRIBUTE_UNUSED, asec= tion *sec) return true; } =20 + /* Remove non-alloc sections for --strip-section-headers. */ + if (strip_section_headers + && (bfd_section_flags (sec) & SEC_ALLOC) =3D=3D 0) + return true; + if ((bfd_section_flags (sec) & SEC_DEBUGGING) !=3D 0) { if (strip_symbols =3D=3D STRIP_DEBUG @@ -2730,7 +2735,7 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_inf= o_type *input_arch) =20 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 f4a8b72869e..b3a89bf041b 100644 --- a/ld/ldlang.c +++ b/ld/ldlang.c @@ -2538,6 +2538,10 @@ lang_discard_section_p (asection *section) && (flags & SEC_DEBUGGING) !=3D 0) discard =3D true; =20 + /* Discard non-alloc sections if we are stripping section headers. */ + else if (config.no_section_header && (flags & SEC_ALLOC) =3D=3D 0) + discard =3D true; + return discard; }