From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 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-pf1-x431.google.com with SMTP id d2e1a72fcca58-65540715b4bso1012702b3a.0 for ; Mon, 05 Jun 2023 08:32:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685979147; x=1688571147; 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=H/aKyzJ7QAjmspD2Rm7JZgmwvu/OQuq/2aSBYIQwf8Y=; b=emoaSRlVuWCPt8f2BdHnm4HQocar8IhkQUT6GMSU3mddMPlO6vhY3/F9oaTcSqqV0G YSQh47uIoGcwytiJ20W/ywjxUZFK91OOJxpBhBf0GN2RGKpilTWP85LnuJs67F48EKyX TQw22zkdj7VH5Irq/JrWFFP2BQx36dp3oF2WQ/wKAopFfERy1x/nnJbkIR6BjiPamqT6 wntrNjp/VMHfb1yUtTqDJPIePUWQg+gmKXanxG0fI2ilC4auPo2kXgwAgGP5Sr0IGCwX zI6OL8SC1W1I1F/zjtBhDSzccPPDJHvxwKZ3GjSIMuFKuHAfvHfguL0NZN3vhCthqHqq 2scA== 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-Google-Smtp-Source: ACHHUZ6tD6WNG5iXU3PUUGQIv1T8F+jXOWfCawu63Copz5JPfuK+rXnYnItUXgXi9OmMOn+U3We9ZQ== 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) From: "H.J. Lu" 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 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 | 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; } -- 2.40.1