From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qt1-x82c.google.com (mail-qt1-x82c.google.com [IPv6:2607:f8b0:4864:20::82c]) by sourceware.org (Postfix) with ESMTPS id 51282385840C for ; Fri, 28 Jan 2022 23:00:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 51282385840C Received: by mail-qt1-x82c.google.com with SMTP id j12so6241684qtr.2 for ; Fri, 28 Jan 2022 15:00:54 -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=7kNATPlaIrVXR+HWSMZQAaEIRaSCB+w/IF50vbmd+0E=; b=ri2FWxDa/FXYEr9SbUdcI12cibuC0/1cezAQGGA9+/S3QcXzSZa0CJ5HJUT53UPt1t H6Do1xDVAeZkHI3R2kaXaYohqxvDckjEkBvj7+VnfDXApCf28iXGy06Ysdq7YDuT5v24 Hl2oju5Yl/OSALlTrQ+ihgxDyH5LH6Yi4jzVZSoDpoKmZGm848b77DDUkI/BKIjK+GEW IceZb+8bpzaXKXL+1ykxiflHeSkteyIj0usIuhw2T68N8yZIJdQb1Ho8u6dr/Qs+BovX 2Tsc8RSt0r4GL8v1s0bYerIEqbjYOxI2j5vjCF6zQRxiaZfNV+GZMCkYPdiPvTtNx/jt DXLA== X-Gm-Message-State: AOAM5318EoCH++oZMLY33NDNezEMUxJuCC9IMPhfQ0QnboNJ3LDdNsDl OSHLIVentrQyHO6l9DOfUzU1kxoedw== X-Google-Smtp-Source: ABdhPJzgxUMB84KlSxGW6F4LIRwSRF47s/Q0d3kQ4pw9E4aK3caoAsfSb2XdKmN0Gs1Yn/ppTER5Bg== X-Received: by 2002:ac8:58cf:: with SMTP id u15mr7829152qta.499.1643410853674; Fri, 28 Jan 2022 15:00:53 -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 s17sm3822458qtk.54.2022.01.28.15.00.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Jan 2022 15:00:53 -0800 (PST) From: Peilin Ye To: binutils@sourceware.org Cc: Nick Clifton , Alan Modra , Cong Wang , Peilin Ye , Peilin Ye Subject: [PATCH v2] objcopy: Fix --only-keep-debug Date: Fri, 28 Jan 2022 15:00:30 -0800 Message-Id: <20220128230030.67552-1-yepeilin.cs@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220128035817.65910-1-yepeilin.cs@gmail.com> References: <20220128035817.65910-1-yepeilin.cs@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.9 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: Fri, 28 Jan 2022 23:00:55 -0000 From: Peilin Ye objcopy's --only-keep-debug option has been broken for ELF files 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. As an example, Linux's scripts/package/builddeb script depends on this option to generate debug-info-only kernel 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 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