public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Fangrui Song <i@maskray.me>
To: "H.J. Lu" <hjl.tools@gmail.com>
Cc: Fangrui Song <maskray@google.com>,
	binutils@sourceware.org, Alan Modra <amodra@gmail.com>
Subject: Re: [PATCH] ld: Add -z start-stop-gc to let __start_/__stop_ not retain C identifier name sections
Date: Mon, 1 Mar 2021 00:08:25 -0800	[thread overview]
Message-ID: <20210301080825.paqseiyw3wub6ad4@gmail.com> (raw)
In-Reply-To: <20210301070319.5pi24f4i7yrt5fmy@gmail.com>

On 2021-02-28, Fangrui Song wrote:
>On 2021-03-01, Alan Modra wrote:
>>On Sat, Feb 27, 2021 at 09:03:19PM -0800, Fangrui Song wrote:
>>>Unfortunately if there is an undefined weak "__start_xx" and no xx is retained =>
>>>undefined symbol error due to ldlang.c:undef_start_stop .
>>>
>>>I don't know how to distinguish undefined strong and undefined weak in undef_start_stop.
>>
>>OK, so let's fix that first.
>>
>>If a weak reference to a __start_foo or __stop_foo symbol ends up
>>having no definition due to all the foo sections being removed for
>>some reason, undef_start_stop currently makes the symbol strong
>>undefined.  That risks a linker undefined symbol error.  Fix that by
>>making the symbol undefweak and also undo some dynamic symbol state.
>>
>>Note that saving the state of the symbol type at the time
>>lang_init_start_stop runs is not sufficient.  The linker may have
>>merged in a shared library reference by that point and made what was
>>an undefweak in regular objects, a strong undefined.  So it is
>>necessary to look at the ELF symbol flags to decide whether an
>>undefweak is the proper resolution.
>>
>>Something probably should be done for COFF/PE too, but I'm unsure how
>>to do go about that.
>>
>>	* ldlang.c (undef_start_stop): For ELF make undefined start/stop
>>	symbols undefweak if that was how they were referenced.  Undo
>>	dynamic state too.
>>
>>diff --git a/ld/ldlang.c b/ld/ldlang.c
>>index 5ffc8444c7..a77e8fabef 100644
>>--- a/ld/ldlang.c
>>+++ b/ld/ldlang.c
>>@@ -6808,6 +6808,19 @@ undef_start_stop (struct bfd_link_hash_entry *h)
>>	}
>>      h->type = bfd_link_hash_undefined;
>>      h->u.undef.abfd = NULL;
>>+      if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
>>+	{
>>+	  const struct elf_backend_data *bed;
>>+	  struct elf_link_hash_entry *eh = (struct elf_link_hash_entry *) h;
>>+	  unsigned int was_forced = eh->forced_local;
>>+
>>+	  bed = get_elf_backend_data (link_info.output_bfd);
>>+	  (*bed->elf_backend_hide_symbol) (&link_info, eh, TRUE);
>>+	  if (!eh->ref_regular_nonweak)
>>+	    h->type = bfd_link_hash_undefweak;
>>+	  eh->def_regular = 0;
>>+	  eh->forced_local = was_forced;
>>+	}
>>    }
>>}
>
>Thanks for taking care of the feature:)
>
>
>Here is another interesting test.
>
>.weak __start_xx
>.weak __stop_xx
>
>.global _start
>_start:
>  movq __start_xx@gotpcrel(%rip), %rdi
>  movq __stop_xx@gotpcrel(%rip), %rsi
>
>.section xx,"a",unique,0
>.byte 0
>
>.section xx,"a",unique,1
>.byte 1
>
>.section xx,"a",unique,2
>.byte 2
>
>
>ld-new a.o --gc-sections --print-gc-sections -pie -z start-stop-gc can discard 3 sections.
>ld-new a.o --gc-sections --print-gc-sections -shared -z start-stop-gc can only discard 2 - there may be a lurking bug somewhere.

Alan's patch takes care of this issue.

There is still an x86 specific due to GOTPCRELX optimization. aarch64 looks good.

     ./ld-new: x.o: relocation R_X86_64_PC32 against undefined protected symbol `__start_xx' can not be used when making a shared object

I filed https://sourceware.org/bugzilla/show_bug.cgi?id=27491

  parent reply	other threads:[~2021-03-01  8:08 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-27 20:26 Fangrui Song
2021-02-27 23:47 ` H.J. Lu
2021-02-28  1:50   ` Fangrui Song
2021-02-28  2:49     ` H.J. Lu
2021-02-28  4:20 ` Alan Modra
2021-02-28  5:03   ` Fangrui Song
2021-03-01  3:59     ` Alan Modra
2021-03-01  7:03       ` Fangrui Song
2021-03-01  7:28         ` Alan Modra
2021-03-01  8:08         ` Fangrui Song [this message]
2021-03-01  4:04     ` Alan Modra

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=20210301080825.paqseiyw3wub6ad4@gmail.com \
    --to=i@maskray.me \
    --cc=amodra@gmail.com \
    --cc=binutils@sourceware.org \
    --cc=hjl.tools@gmail.com \
    --cc=maskray@google.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).