From patchwork Mon Jun 5 15:32:17 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: 103349 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2768778vqr; Mon, 5 Jun 2023 08:32:42 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5D4kqncuWx/j1RqEhhJPcTBBlMopNN1hNyzm7yMjkmT46P+WMYRSpUgRtSDCHnAmtxdCW3 X-Received: by 2002:ac2:4d1b:0:b0:4f5:1ac9:ab1b with SMTP id r27-20020ac24d1b000000b004f51ac9ab1bmr5076100lfi.23.1685979162755; Mon, 05 Jun 2023 08:32:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685979162; cv=none; d=google.com; s=arc-20160816; b=nfchU4u7RhQGfj8EIBIMxgQ2BR3fsNpkk10SSXC2DPiwwif4HWRqW1Ckrq/4ykEUWC gzOfkewHDwBfQTs95MXS46RK2qRi3LI5D2w893bigwyD0oDFanf6AGviZ6ls3XGTdZcg 1vji6pNr89Ukkf87CNAKyz87DcbCVEWJ5Oq4XCZOPSbsy6eC+VQLRrf4g+DoTj9KvL3u 0ke0n/bPRwkHfHIL27dwiPe56H41We/uy3t7xC+vD+VkBufznNHROL7jRgilDcOVLwwW d63nkKSv26EiZ9Mq96EiwDEA8unv4zyo5xBTAwUXpFsVeCGRYVHjFMTgtY9HAL2EudN2 besQ== 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=H/aKyzJ7QAjmspD2Rm7JZgmwvu/OQuq/2aSBYIQwf8Y=; b=g0J5+ZxbM2rJKfgG+jZUqeLu/aQ/k+IUbhF/vEvtqBRA+aUsaGAN4s6Ja311wkXHB8 ULkK3aXr+tBaP8lhbsapjFAUczMyTpJf9nu9h/VK4Zbjl/CdLunEI02X89KXsgefB40V 2fQ31jPcip2IaAKWPzYMeWl1cYl0IzpqNVPbW0/grE0H2JXvHWgeJjI7EUGKbQEOdOOG XJ+A5raiZqDWFEp+scYZdI1RQgt0mhPvOOkfPentKhzukhPF71qKSg6n7CrOg95eDuPj u6cQxRTLgg85eJcynWR4K0TikRrll5g8Uuk42ldTzbcEslOKjp0cFoKMgFY8YVcOOCub iYeg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=Cj8AxUyN; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id j17-20020aa7c0d1000000b00514b2ca0a65si5552224edp.356.2023.06.05.08.32.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Jun 2023 08:32:42 -0700 (PDT) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=Cj8AxUyN; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c 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 551B53882666 for ; Mon, 5 Jun 2023 15:32:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 551B53882666 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1685979156; bh=H/aKyzJ7QAjmspD2Rm7JZgmwvu/OQuq/2aSBYIQwf8Y=; 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=Cj8AxUyNPsgtIAV6OL3qRvVoxO7QXIh3nYTC4cJWL61vQrPp3nwzAfgC+lt3Nc+Xv tKEIvyZkUNzeU6ZpWh5gFmhBEfmAjXXhS6YFAs6eiYkcZ7oLgr4jA5EteS/QaWS+ZV XcZPXD5zGjXRLHC4X/l59qGujhycEWMdzw4fUS40= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by sourceware.org (Postfix) with ESMTPS id E458A3882664 for ; Mon, 5 Jun 2023 15:32:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E458A3882664 Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-65540715b4bso1012702b3a.0 for ; Mon, 05 Jun 2023 08:32:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685979147; x=1688571147; 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=H/aKyzJ7QAjmspD2Rm7JZgmwvu/OQuq/2aSBYIQwf8Y=; b=f0fBKtwQEaBBQ3wxPRG2BewrBzA1fI3AwQlWR2DJ+e3n485symZpfaU9kxPjHuRPnv CBsOanZev79LOkQ4fewYoE8cNusKfEKgwnCs1nviZvEI41chviIdAnY6JXURJy9fyl6t wMo7MnOPxW+eOqOoXGVVrD2ENTwVnV2tARXojUnMg42ayhIIhqpAuXgNF3csy+YZauay s55ru4/YBRnM2hu7zvZ3758xggYpbAN1lidAPA3glPB5Eb+ycM5px0qmC8QHImPFTW4d 2miUFqhwAafiVqlGX2KhMjUjBOWTFGHU/p70M36ZpKdrANWecjYRc5Wepg39d+aFM7dL qaUA== X-Gm-Message-State: AC+VfDxmVC2QO30UordDEg0CvnlY5rJF5STytxqpmjh9XaHDM0vf+hXt XBuHuVIU1sJg7Kj3qLtg/U5COJDXUU0= X-Received: by 2002:a05:6a20:7fa9:b0:10b:2214:7ab1 with SMTP id d41-20020a056a207fa900b0010b22147ab1mr2881298pzj.18.1685979146641; Mon, 05 Jun 2023 08:32:26 -0700 (PDT) Received: from gnu-cfl-3.localdomain ([172.59.160.123]) by smtp.gmail.com with ESMTPSA id e9-20020a656789000000b0052c562bd942sm5345345pgr.19.2023.06.05.08.32.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Jun 2023 08:32:26 -0700 (PDT) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 548807402B7; Mon, 5 Jun 2023 08:32:25 -0700 (PDT) To: binutils@sourceware.org Cc: Alan Modra , Kaylee Blake Subject: [PATCH v3 2/7] ELF: Discard non-alloc sections without section header Date: Mon, 5 Jun 2023 08:32:17 -0700 Message-Id: <20230605153222.1728119-3-hjl.tools@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230605153222.1728119-1-hjl.tools@gmail.com> References: <20230605153222.1728119-1-hjl.tools@gmail.com> MIME-Version: 1.0 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 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?1767877286879406811?= X-GMAIL-MSGID: =?utf-8?q?1767877286879406811?= 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 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..f1100b8c1e9 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; }