public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Nick Clifton <nickc@redhat.com>
To: Peilin Ye <yepeilin.cs@gmail.com>, binutils@sourceware.org
Cc: Cong Wang <xiyou.wangcong@gmail.com>,
	Peilin Ye <peilin.ye@bytedance.com>
Subject: Re: [RFC PATCH] bfd: Handle objcopy --only-keep-debug in _bfd_elf_init_private_section_data()
Date: Fri, 28 Jan 2022 12:42:08 +0000	[thread overview]
Message-ID: <316159a4-8784-67eb-13e1-8feee4c76308@redhat.com> (raw)
In-Reply-To: <20220128035817.65910-1-yepeilin.cs@gmail.com>

Hi Peilin,

> 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.

I have a feeling that this change is going to have wider repercussions
than simply fixing this problem.  It is going to affect those sections
created by elf_fake_sections() for example, and I bet that there will
be other scenarios that have not occurred to me.

Possibly a better fix would be to change objcopy instead so that it can
cope with the behaviour of bfd_copy_private_section_data().  For example,
please could you try out this patch and see if it fixes the problem ?


diff --git a/binutils/objcopy.c b/binutils/objcopy.c
index d16d8ee67e4..14b76d8a65a 100644
--- a/binutils/objcopy.c
+++ b/binutils/objcopy.c
@@ -4180,6 +4180,10 @@ setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
        err = _("failed to copy private data");
        goto loser;
      }
+  /* Since isec.flags == osec.flags, bfd_copy_private_section_data()
+     will reset the section type.  */
+  if (make_nobits)
+    elf_section_type (osection) = SHT_NOBITS;

    /* All went well.  */
    return;


> This patch is manually tested only.  Which testsuite(s)
> should I use to test this patch? 

Please run the binutils testsuite.  ie:

   cd <build-dir>/binutils
   make check

Cheers
   Nick



  reply	other threads:[~2022-01-28 12:42 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-28  3:58 Peilin Ye
2022-01-28 12:42 ` Nick Clifton [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=316159a4-8784-67eb-13e1-8feee4c76308@redhat.com \
    --to=nickc@redhat.com \
    --cc=binutils@sourceware.org \
    --cc=peilin.ye@bytedance.com \
    --cc=xiyou.wangcong@gmail.com \
    --cc=yepeilin.cs@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).