Re: [PATCH v2] ld: Prevent `_tls_used` and `_load_config_used` from being garbage-collected

Jan Beulich <[email protected]> Fri, 31 Jul 2026 12:16:57 +0200
Newsgroups gmane.comp.gnu.binutils
Message-ID <[email protected]>
On 27.07.2026 18:45, LIU Hao wrote:
> 在 2026-7-27 23:55, Jan Beulich 写道:
>>> --- a/ld/emultempl/pe.em
>>> +++ b/ld/emultempl/pe.em
>>> @@ -1573,6 +1573,16 @@ gld${EMULATION_NAME}_after_open (void)
>>>
>>>      pe_output_file_set_long_section_names (link_info.output_bfd);
>>>
>>> +  /* The RVAs of these symbols will be written into the PE header, so they
>>> +     must not be collected.  */
>>> +#if defined (TARGET_IS_i386pe)
>>> +  lang_add_gc_name ("__tls_used");
>>> +  lang_add_gc_name ("__load_config_used");
>>> +#else
>>> +  lang_add_gc_name ("_tls_used");
>>> +  lang_add_gc_name ("_load_config_used");
>>> +#endif
>>
>> The #ifdef here likely wants replacing by appropriate use of
>> bfd_get_symbol_leading_char(). To play safe towards future uses, the
>> same code could then also be used in pep.em.
> 
> An updated patch is attached.

One small nit on the description: I don't think "Earlier today" can be quite
correct anymore; it certainly won't be by the time the change is getting
committed. Hence why it's best to avoid such wording from the beginning.

And then see my more general remark in the reply to v1: That aspect imo at
least wants mentioning briefly in the description, so that people finding
an issue with this behavior can - upon doing archeology - can figure that
this change was made despite understanding that it may not be correct in
all cases.

As to your use of bfd_get_symbol_leading_char(): I would have hoped you'd
do this more similarly to what is done in bfd/peXXigen.c. In particular I
didn't expect a dynamic allocation to be used here. Yet now that I look I
see that lang_add_gc_name() doesn't make a copy of the name. So all fine
there. Nevertheless I would prefer if the if/else was avoided, again like
bfd/peXXigen.c manages to do:

  /* The RVAs of these symbols will be written into the PE header, so they
     must not be collected.  */
  char sym_prefix = bfd_get_symbol_leading_char (link_info.output_bfd);
  char *sym = xstrdup ("__tls_used");
  sym[0] = sym_prefix;
  lang_add_gc_name (sym + !!sym[0]);
  sym = xstrdup ("__load_config_used");
  sym[0] = sym_prefix;
  lang_add_gc_name (sym + !!sym[0]);

Would you be amenable to switching to this approach? (Whether it's !!sym[0]
or !!sym_prefix is of course secondary.)

Jan