From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qv1-xf2c.google.com (mail-qv1-xf2c.google.com [IPv6:2607:f8b0:4864:20::f2c]) by sourceware.org (Postfix) with ESMTPS id 810EC3858D35 for ; Tue, 1 Feb 2022 00:21:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 810EC3858D35 Received: by mail-qv1-xf2c.google.com with SMTP id g11so14528961qvu.3 for ; Mon, 31 Jan 2022 16:21:43 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=MwPIpcq2lIF2E6Zk0BdJbpb+Qmf92kcmUpyVwJ+74q0=; b=35KhLtBjQBFvH2YTLJKyhEl98vsLt/Rva/kU3xy89BYRzI7XIg02aq29EZqnBxe/LP 01wzwr4Rd7dN0Hm/3zpE1PShy5Wm/lXBOnyTIx3iw+f5QeR24TL8obx5Wywif6JFcySn 1tk43fx1UQIakyIJorUiyc47FEMxu5EriHFBh3TiqxpM20yRPf5i3a2N4F7uEvDv/hDK Ejn4BUKVzLYeQpw06G7K/Ax3Gbpc4wBShFFFrhboQ4iruDVqnxam3nulc3wTaJEHEVby 4TRvTQv4ANR9+UOSfnWsyNyePXRSP3DLuwZGpgwdyYRdFY/LQY4cjSgqQcI+Mj5LAycF uyLQ== X-Gm-Message-State: AOAM5336RaS6mLtC7xkcT6kKGhPp59A5ECyDPuLADSDrKbsMbzSEzT9e DNKFAuL4oClKs1eFkv4d5CfwnLsKQw== X-Google-Smtp-Source: ABdhPJyGoKZMHxGWa9c4bpp2jDLvFr3SzVnjBl8l2nSz3eQmZiChepr1vWd/3Y28S+7erM0ulDXCEA== X-Received: by 2002:a05:6214:401e:: with SMTP id kd30mr19840906qvb.90.1643674902902; Mon, 31 Jan 2022 16:21:42 -0800 (PST) Received: from bytedance.attlocal.net (ec2-52-52-7-82.us-west-1.compute.amazonaws.com. [52.52.7.82]) by smtp.gmail.com with ESMTPSA id w3sm9472048qta.13.2022.01.31.16.21.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Jan 2022 16:21:42 -0800 (PST) From: Peilin Ye To: binutils@sourceware.org Cc: Nick Clifton , Alan Modra , Cong Wang , Peilin Ye , Peilin Ye Subject: [PATCH v3 1/2] objcopy: Fix --only-keep-debug for ELF relocatables Date: Mon, 31 Jan 2022 16:20:46 -0800 Message-Id: <20220201002046.70469-1-yepeilin.cs@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220128230030.67552-1-yepeilin.cs@gmail.com> References: <20220128230030.67552-1-yepeilin.cs@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.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.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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-List-Received-Date: Tue, 01 Feb 2022 00:21:45 -0000 From: Peilin Ye objcopy's --only-keep-debug option has been broken for ELF relocatables since commit 8c803a2dd7d3 ("elf_backend_section_flags and _bfd_elf_init_private_section_data"): 1. binutils/objcopy.c:setup_section() marks non-debug OSECs as SHT_NOBITS, then calls bfd_copy_private_section_data(), see "mark_nobits"; 2. If ISEC and OSEC share the same section flags, bfd/elf.c:_bfd_elf_init_private_section_data() restores OSEC's section type back to ISEC's section type, effectively undoing "mark_nobits". This makes objcopy's --only-keep-debug a no-op for ELF relocatables. As an example, Linux's scripts/package/builddeb script depends on this option to generate debug-info-only kernel (.ko) modules. As suggested by Nick and Alan, fix --only-keep-debug by marking non-debug OSECs as SHT_NOBITS after calling bfd_copy_private_section_data() in setup_section(). binutils/ChangeLog: * objcopy.c (setup_section): For --only-keep-debug, mark non-debug output sections as SHT_NOBITS after calling bfd_copy_private_section_data() Reported-by: Omar Sandoval Fixes: 8c803a2dd7d3 ("elf_backend_section_flags and _bfd_elf_init_private_section_data") Suggested-by: Alan Modra Signed-off-by: Peilin Ye --- change since v2: - mention ELF relocatables in commit message change since v1: - fix objcopy instead of touching _bfd_elf_init_private_section_data() (suggested by Nick and Alan) binutils/objcopy.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/binutils/objcopy.c b/binutils/objcopy.c index d16d8ee67e4d..d53aa5c6000f 100644 --- a/binutils/objcopy.c +++ b/binutils/objcopy.c @@ -4085,9 +4085,6 @@ setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg) goto loser; } - if (make_nobits) - elf_section_type (osection) = SHT_NOBITS; - size = bfd_section_size (isection); size = bfd_convert_section_size (ibfd, isection, obfd, size); if (copy_byte >= 0) @@ -4181,6 +4178,9 @@ setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg) goto loser; } + if (make_nobits) + elf_section_type (osection) = SHT_NOBITS; + /* All went well. */ return; -- 2.20.1