public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [RFC PATCH] bfd: Handle objcopy --only-keep-debug in _bfd_elf_init_private_section_data()
@ 2022-01-28  3:58 Peilin Ye
  2022-01-28 12:42 ` Nick Clifton
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Peilin Ye @ 2022-01-28  3:58 UTC (permalink / raw)
  To: binutils; +Cc: Nick Clifton, Cong Wang, Peilin Ye, Peilin Ye

From: Peilin Ye <peilin.ye@bytedance.com>

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 <osandov@osandov.com>
Fixes: 8c803a2dd7d3 ("elf_backend_section_flags and _bfd_elf_init_private_section_data")
Signed-off-by: Peilin Ye <peilin.ye@bytedance.com>
---
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
<peilin.ye@bytedance.com>

 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

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-02-01  0:22 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-28  3:58 [RFC PATCH] bfd: Handle objcopy --only-keep-debug in _bfd_elf_init_private_section_data() Peilin Ye
2022-01-28 12:42 ` Nick Clifton
2022-01-28 12:52 ` Alan Modra
2022-01-28 23:37   ` Peilin Ye
2022-01-29  9:15   ` Peilin Ye
2022-01-30  1:08     ` Peilin Ye
2022-01-28 23:00 ` [PATCH v2] objcopy: Fix --only-keep-debug Peilin Ye
2022-02-01  0:20   ` [PATCH v3 1/2] objcopy: Fix --only-keep-debug for ELF relocatables Peilin Ye
2022-02-01  0:21   ` [PATCH v3 2/2] binutils/testsuite: Test " Peilin Ye

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).