public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Peilin Ye <yepeilin.cs@gmail.com>
To: Alan Modra <amodra@gmail.com>
Cc: binutils@sourceware.org, Cong Wang <xiyou.wangcong@gmail.com>,
	Peilin Ye <peilin.ye@bytedance.com>,
	Nick Clifton <nickc@redhat.com>
Subject: Re: [RFC PATCH] bfd: Handle objcopy --only-keep-debug in _bfd_elf_init_private_section_data()
Date: Sat, 29 Jan 2022 17:08:44 -0800	[thread overview]
Message-ID: <20220130010844.GA69456@bytedance> (raw)
In-Reply-To: <20220129091540.GA68164@bytedance>

On Sat, Jan 29, 2022 at 01:15:40AM -0800, Peilin Ye wrote:
> It seems that --only-keep-debug is broken for Linux kernel .ko modules,
> but it works just fine on tmpdir/striprog (built from testprog.c) used
> by this testsuite...

I figured this out; it's because --only-keep-debug is only broken for
ELF relocatables.

For dynamically linked and statically linked ELF executables, their
non-debug output sections' type is changed 3 times:

  1. In setup_section() "make_nobits", set to SHT_NOBITS;
  2. Then in _bfd_elf_init_private_section_data(), restored back to input
     section's type, as we already discussed;
  3. Finally in assign_file_positions_for_load_sections(), they are set
     once again to SHT_NOBITS:

	for (i = 0; i < m->count; i++)
		if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
	      	/* If we aren't making room for this section, then
		   it must be SHT_NOBITS regardless of what we've
		   set via struct bfd_elf_special_section.  */
			elf_section_type (m->sections[i]) = SHT_NOBITS;
							    ^^^^^^^^^^

For a complete call graph:

copy_object()
  [...]
    setup_section()				/* 1. set to SHT_NOBITS */
      bfd_copy_private_section_data()
        _bfd_elf_copy_private_section_data()
          _bfd_elf_init_private_section_data()	/* 2. restore to ISEC's type */
  [...]
    copy_section()
      bfd_set_section_contents()
        _bfd_elf_set_section_contents()
          _bfd_elf_compute_section_file_positions()
            assign_file_positions_except_relocs()
              if (EXEC_P or DYNAMIC)		/* ELF executables? e.g. tmpdir/striprog */
                assign_file_positions_for_load_sections()	/* 3. set again to SHT_NOBITS */
              else				/* ELF relocatables? e.g. Linux modules */

Frankly, I don't semantically understand what this code is trying to do
here yet.  Since an ELF relocatable is neither EXEC_P nor DYNAMIC,
assign_file_positions_for_load_sections() is not called, therefore its
non-debug OSECs remain the old (ISEC's) type.

In conclusion, I think currently --only-keep-debug is only broken for
ELF relocatables, although I'm not 100% sure.

I will add this information in v3's commit message, and come up with a
better testsuite (probably test this on a .o file or something).

Thanks,
Peilin Ye
<peilin.ye@bytedance.com>


  reply	other threads:[~2022-01-30  1:08 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
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 [this message]
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=20220130010844.GA69456@bytedance \
    --to=yepeilin.cs@gmail.com \
    --cc=amodra@gmail.com \
    --cc=binutils@sourceware.org \
    --cc=nickc@redhat.com \
    --cc=peilin.ye@bytedance.com \
    --cc=xiyou.wangcong@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).