From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by sourceware.org (Postfix) with ESMTPS id 83D9A3858C5F for ; Tue, 6 Jun 2023 17:58:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 83D9A3858C5F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-1b04706c85fso59934885ad.0 for ; Tue, 06 Jun 2023 10:58:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686074330; x=1688666330; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=D+k74oemB3xoEteOuIy1KkkFyu/f9iBtXbtQV7LGftw=; b=UDBXzlW4udn7EJeP8x5G/vdG9MBuvMQxr1Z/V1I5ZOHDpCrwyfWHTgkSAIu3pR91+4 Myf43NDjEzoykB/SUukM+v/ZTBzuS4zg5QjMhaOOuIb80QvKiqsHfWBMj8VMie//A/qq iSXX+pDUjpxCYeUVtMAAnPqKmJxFIDfC4u8uBuC46NmYZ3rXhbgJS+KK8GWYSbsDzB6M nJdhsTJIUbXQhh/mIvPkVy075jx8/xJbxGkC0uv6UttYjJQ4s9GFh5iRCNICY0DWLPtW Vt0im5QQLqPdlvr/h/Za4KzgRkOF0z4odCIwdlt7anM2PtrDeWOp6pyF8d/Xgo9JnS7Q TDBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686074330; x=1688666330; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=D+k74oemB3xoEteOuIy1KkkFyu/f9iBtXbtQV7LGftw=; b=MxZcD0VmcmOptBxckWlNQ9NSzVknT9Z03igPg3d4ljQH1D8IGy26lQEfTivch5weVc 12KabLGIAy5trLV0KibKzcadhaGPhgrdSrIoo1161gO0/M58gKT1mv4ixFExvIoTVIxT FoDI6mNsjs454cJErH8UUUIZ107ntkq6h4JU1CoH0hFJJbHx4fLjgdCq7SaC0LFuJ0KM 83LOqYivT60yWpnrdJkMdoHDkHmvxcApTf+EEYSrdTS47efKF9OPyX2Q6fsK1MelzFf2 qSLwXS8v9kmtAqKRHOpdAooAhTp12AkFRZP78caCGyqOkfJLV5Piv6bYMjifpcjAgjAS DSYw== X-Gm-Message-State: AC+VfDwHOuxj3mOOubgHuECiiPin93Ccgfb1ELOsanclVxqx6JVTgahb L7DjwDlE/Gr6srZMqoMm4Ro= X-Google-Smtp-Source: ACHHUZ6FMcbHhGyF5l2torQWXnY31IIGXyLo8aod65xmvZXFJJvwyAtf9Ktt2EGN+ggdNprMvlXH8Q== X-Received: by 2002:a17:903:124d:b0:1b0:25d2:6f11 with SMTP id u13-20020a170903124d00b001b025d26f11mr3517255plh.17.1686074330463; Tue, 06 Jun 2023 10:58:50 -0700 (PDT) Received: from gnu-cfl-3.localdomain ([172.59.160.123]) by smtp.gmail.com with ESMTPSA id f3-20020a170902ce8300b001ae365072ccsm8829322plg.122.2023.06.06.10.58.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jun 2023 10:58:50 -0700 (PDT) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 23ED4740301; Tue, 6 Jun 2023 10:58:49 -0700 (PDT) From: "H.J. Lu" To: binutils@sourceware.org Cc: Alan Modra , Florian Weimer , Kaylee Blake Subject: [PATCH v4 2/7] ELF: Discard non-alloc sections without section header Date: Tue, 6 Jun 2023 10:58:41 -0700 Message-Id: <20230606175846.399377-3-hjl.tools@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230606175846.399377-1-hjl.tools@gmail.com> References: <20230606175846.399377-1-hjl.tools@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3025.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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