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 B2B3E385E455 for ; Fri, 28 Jan 2022 03:59:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B2B3E385E455 Received: by mail-qt1-x82c.google.com with SMTP id b5so4222124qtq.11 for ; Thu, 27 Jan 2022 19:59:01 -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:mime-version :content-transfer-encoding; bh=b4XtktEQekJDi7jgn0ny7SkfOgmzbp3QgLwmH3jqRlQ=; b=mZ86Pw3uxJQQIewVnOGnF1vStAIigfJXhdORSE3fmn2LDYt+01NbPz7XBqEs4iXEVh kmBhK/XorMWpane6hP8a0RpubEJpQo7cYwZ4K2zLKkfaZ142d0UMMcXeCQ6gqJl+QLIi bAbhe91RXwM4OjcOR64Z2mfgziz2d1vvhKcbkby1829/8VvsOtt4qH6d9WbVCeRYQ/9a 1E1AHGVr5rvnkNji0XB90meDx0w0FvB8lgrJZevinsTQdJWlNpRhDgn8K32mzxlM0O3g rIjiuI7E6CL6c91me9rJ4dm5CWTmc+LXngbVvJnxRDVW6V6JZIkX4Qb+xGNIH1VIlwwx /V+w== X-Gm-Message-State: AOAM532o7Q9Rea3XMnyNkU7lRLQTtjsuzL8ezp/jZ733mCp/BKJ0igES kEXXflHZt38w9/uUpyzYE1MjQcaikA== X-Google-Smtp-Source: ABdhPJwYspecLKKrSsDWSliZqAxsvd7sUMfGipuc9MthkMthYNTx7UPJrVFt/kt7oRTV6u3Qv/R2Zg== X-Received: by 2002:a05:622a:611:: with SMTP id z17mr726986qta.559.1643342340567; Thu, 27 Jan 2022 19:59:00 -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 a128sm2376833qkf.1.2022.01.27.19.58.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jan 2022 19:59:00 -0800 (PST) From: Peilin Ye To: binutils@sourceware.org Cc: Nick Clifton , Cong Wang , Peilin Ye , Peilin Ye Subject: [RFC PATCH] bfd: Handle objcopy --only-keep-debug in _bfd_elf_init_private_section_data() Date: Thu, 27 Jan 2022 19:58:17 -0800 Message-Id: <20220128035817.65910-1-yepeilin.cs@gmail.com> X-Mailer: git-send-email 2.25.1 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 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 03:59:02 -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 sections 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. Handle this special case by checking OSEC's size in addition to its type (similar to bfd/elf.c:elf_fake_sections()). A non-debug section marked as SHT_NOBITS by --only-keep-debug should have a non-zero size. bfd/ChangeLog: * elf.c (_bfd_elf_init_private_section_data): Handle SHT_NOBITS sections marked by objcopy --only-keep-debug Reported-by: Omar Sandoval Fixes: 8c803a2dd7d3 ("elf_backend_section_flags and _bfd_elf_init_private_section_data") Signed-off-by: Peilin Ye --- Hi all, We ran into this bug yesterday, sorry for sending this right before 2.38 release date. This patch is manually tested only. Which testsuite(s) should I use to test this patch? I browsed bfd/README and bfd/doc/bfdint.texi but failed to figure out. Please advise, thank you! Thanks, Peilin Ye bfd/elf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bfd/elf.c b/bfd/elf.c index 14c2c7ba734d..0c0a3e0a870b 100644 --- a/bfd/elf.c +++ b/bfd/elf.c @@ -7887,7 +7887,7 @@ _bfd_elf_init_private_section_data (bfd *ibfd, SHF_MASKOS and SHF_MASKPROC. */ if (elf_section_type (osec) == SHT_PROGBITS || elf_section_type (osec) == SHT_NOTE - || elf_section_type (osec) == SHT_NOBITS) + || (elf_section_type (osec) == SHT_NOBITS && osec->size == 0)) elf_section_type (osec) = SHT_NULL; /* For objcopy and relocatable link, copy the ELF section type from the input file if the BFD section flags are the same. (If they -- 2.20.1