From patchwork Wed May 31 20:06:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 101576 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp3146749vqr; Wed, 31 May 2023 13:08:43 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4WKMk8Fm/uzd5w6Qd7xgeS/zbB3Ws4q/Q4q7c7sko3CYQWNaExzg5r+AknAENi8LW3Gzy2 X-Received: by 2002:ac2:4a77:0:b0:4f3:aa09:e7e8 with SMTP id q23-20020ac24a77000000b004f3aa09e7e8mr56023lfp.44.1685563723477; Wed, 31 May 2023 13:08:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685563723; cv=none; d=google.com; s=arc-20160816; b=AqMLpCJUjRadCmcRcEkn+KSt0xWGbEiiQR2jH+t6cghmaEvf8X1EH3808onuM3RQPu YpQgPZ/HNCiOE703izPBwtG2SdaEUm8aDoAtBbTOwKhZVweSMBk5c6kUWxWX+oWDGx4u GVM8hW6UP7VXnnD/aOCNeUNwOg7LbS5HSvlAu/aasOoBjEMlmMGJBsp93Y2bbLxhw+Bh BvhIyqzD0s1aoQtJL1C5fx07+uUHRR5jLmGs5xDpKiSNM1ZoA79A8ffKvBLcjIErJUQc z7al0KE67a4ZYrb2JggfKoQwkWBhdBkmrpLVRJpnKNe3Igt/zOckl4cRGzK5OicM+Tbx B5ZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=wPPE9K9rgaF9goy5HtpahswFybuROBiBq1/Gg+74KPY=; b=EDNY6Hi3XUY4yxo1FjqceooZ9ZXdsdgH9C3ZXxWYW958rdlOgSgAZ1XRIhBPhkIboC vBNpGjNE0XLzFxUiy3x6IORf3dFDMhSqPITHcWXI79lGrrdjKFOtAhUfaPOkB+oyRUyg wSTzlOOKFD+TmDgeCKLgORRQc1E4g1mdtegpnbtsTj8lVxM9Xl6LVXaDdaDRUHb97puR xSDvFmVq8DmZHonIici4bnYmh8Qy6r2tubvWOUENs1elOYBME+bafyQHd3mvrW3nyh7f QPqTYaZ/Z5LLs9QkMkYsUoKmlkXmxhJhbRU+KmoJruIEIuFvNEesUl+5Et3MP2BrXjQC 21uQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=KaELiKT+; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id d12-20020aa7d5cc000000b0050bc4bf4239si6749653eds.235.2023.05.31.13.08.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 May 2023 13:08:43 -0700 (PDT) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=KaELiKT+; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2BEF73851ABB for ; Wed, 31 May 2023 20:07:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2BEF73851ABB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1685563638; bh=wPPE9K9rgaF9goy5HtpahswFybuROBiBq1/Gg+74KPY=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=KaELiKT+hnpJEeksxVOYEpT75bvjcLJ5+Nng7kGK2C1RKiW96mqvB8c7AQCpxnhe3 UZmtGS0CZA5e5f5+QwYo9BMvO+N0DLkdCWLiZEPh+lxCMZxIygSi1x7wp75QufxXhM 340xPuZDDgYJCu2hzUQf0jnK/XuEhI3JiJ4GtAW8= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by sourceware.org (Postfix) with ESMTPS id B80633857838 for ; Wed, 31 May 2023 20:06:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B80633857838 Received: by mail-pg1-x532.google.com with SMTP id 41be03b00d2f7-53f70f7c2d2so38226a12.3 for ; Wed, 31 May 2023 13:06:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685563581; x=1688155581; 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=wPPE9K9rgaF9goy5HtpahswFybuROBiBq1/Gg+74KPY=; b=DiZDLX2Gvc5d0Dj8Utqa0ve4iZZc7PyGDunD5NYYEbWUhyov01JobYNyazn3pQqJWP u/r9cUvIHNCPs1sSyBdZuzo9+KgekXUX/1ppjmTHU4iAfW9s7kgXKeGzA7im3jowdkaa jXUigpvO60Q2O4DN+b2evLJH8KaOtP1Vsl+A67op08V5Vsac40+a+45yEeIpAez3RMDE RVy3YumAEGQqVjCpG7jIum+oU+uBZ31ONcjCpIVCLj0nQvlQ1nxLLEeVK3/Hehljzegb dXGUr7O0QIECkBQXG37IIwGQsDm9jLwaWu9NmXIbAVIaJriaEFSmy7/9gzxVYNtVmVS9 A1ZA== X-Gm-Message-State: AC+VfDyy03iUChdb5hACi0BlDMV7B/1O4LjtbWf4ArqbM+lmn4M2vgq3 ZqUZCwWushG9ZjwxszI473A= X-Received: by 2002:a05:6a20:3d28:b0:10e:ff8f:8edc with SMTP id y40-20020a056a203d2800b0010eff8f8edcmr6107738pzi.55.1685563581414; Wed, 31 May 2023 13:06:21 -0700 (PDT) Received: from gnu-cfl-3.localdomain ([172.59.160.123]) by smtp.gmail.com with ESMTPSA id y10-20020a17090322ca00b001aaec7a2a62sm1781836plg.188.2023.05.31.13.06.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 May 2023 13:06:20 -0700 (PDT) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id EDAF874023A; Wed, 31 May 2023 13:06:19 -0700 (PDT) To: binutils@sourceware.org Cc: Kaylee Blake , Alan Modra , Andreas Schwab Subject: [PATCH v2 2/7] ELF: Discard non-alloc sections without section header Date: Wed, 31 May 2023 13:06:12 -0700 Message-Id: <20230531200617.951996-3-hjl.tools@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230531200617.951996-1-hjl.tools@gmail.com> References: <20230531200617.951996-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3025.2 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 X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: "H.J. Lu via Binutils" From: "H.J. Lu" Reply-To: "H.J. Lu" Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767441667106056277?= X-GMAIL-MSGID: =?utf-8?q?1767441667106056277?= 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 | 5 +++++ ld/ldlang.c | 4 ++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/bfd/elf.c b/bfd/elf.c index 94954a8fbb9..6c8533cfb97 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 910d03f175e..1e067e9b194 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 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; }