[PATCH v2] ld: Prevent `_tls_used` and `_load_config_used` from being garbage-collected
LIU Hao <[email protected]> Tue, 28 Jul 2026 00:45:08 +0800
| Newsgroups | gmane.comp.gnu.binutils |
|---|---|
| Message-ID | <[email protected]> |
在 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.
I sincerely think this makes little sense for PE+. No known target for PE+ uses a symbol prefix; but
there's `USE_MINGW64_LEADING_UNDERSCORES` so meh.
--
Best regards,
LIU Hao
2004-ld-Prevent-_tls_used-and-_load_config_used-from-bein.patch
(text/plain, 2.7 KB)
From fd001e472847e0955c4f1e1bbfd684735567a028 Mon Sep 17 00:00:00 2001 From: LIU Hao <[email protected]> Date: Tue, 28 Jul 2026 00:05:24 +0800 Subject: [PATCH] ld: Prevent `_tls_used` and `_load_config_used` from being garbage-collected Earlier today I pushed some patches to mingw-w64 to make `_tls_used` only linked on demand, by referencing it indirectly through tentative definitions. However, since the startup code no longer has strong references to `_tls_used`, if LD is passed `--gc-sections`, it garbage-collects `_tls_used`, resulting in a broken executable: $ objdump -p bin/test_thread_id_cpp.exe | grep -F .tls Entry 9 ffffffffc0000000 00000028 Thread Storage Directory [.tls] This patch prevents `_tls_used` from being garbage-collected, and likewise for `_load_config_used`. Signed-off-by: LIU Hao <[email protected]> --- ld/emultempl/pe.em | 18 ++++++++++++++++++ ld/emultempl/pep.em | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/ld/emultempl/pe.em b/ld/emultempl/pe.em index 07ef2ca5953..fbf7398eb04 100644 --- a/ld/emultempl/pe.em +++ b/ld/emultempl/pe.em @@ -1573,6 +1573,24 @@ 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. */ + char sym_prefix = bfd_get_symbol_leading_char (link_info.output_bfd); + if (sym_prefix == 0) + { + lang_add_gc_name ("_tls_used"); + lang_add_gc_name ("_load_config_used"); + } + else + { + char *sym = xstrdup ("__tls_used"); + sym[0] = sym_prefix; + lang_add_gc_name (sym); + sym = xstrdup ("__load_config_used"); + sym[0] = sym_prefix; + lang_add_gc_name (sym); + } + #ifdef DLL_SUPPORT pe_process_import_defs (link_info.output_bfd, &link_info); diff --git a/ld/emultempl/pep.em b/ld/emultempl/pep.em index 25ce3963b36..bf5e2c4665c 100644 --- a/ld/emultempl/pep.em +++ b/ld/emultempl/pep.em @@ -1582,6 +1582,24 @@ gld${EMULATION_NAME}_after_open (void) pep_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. */ + char sym_prefix = bfd_get_symbol_leading_char (link_info.output_bfd); + if (sym_prefix == 0) + { + lang_add_gc_name ("_tls_used"); + lang_add_gc_name ("_load_config_used"); + } + else + { + char *sym = xstrdup ("__tls_used"); + sym[0] = sym_prefix; + lang_add_gc_name (sym); + sym = xstrdup ("__load_config_used"); + sym[0] = sym_prefix; + lang_add_gc_name (sym); + } + #ifdef DLL_SUPPORT pep_process_import_defs (link_info.output_bfd, &link_info); -- 2.55.0
OpenPGP_signature.asc
(application/pgp-signature, 840 B)
-----BEGIN PGP SIGNATURE----- wsF5BAABCAAjFiEEYmSQWY4DEzq4FUs4hfveZl3ogBsFAmpnixUFAwAAAAAACgkQhfveZl3ogBt0 6xAAow7HCzxb7/k2Em+DG2mMEnr0nS7WHcf1tv9Jiag+7XIly1ZHjhXJ2Ux/Hm66Ub+A5wxK/qXY B5Zecntuitb40B4k520bg+SKIKaRjoK5XRb0MBr7bQxErf1tsevROeGnUSToswVodU1QGEYDF+L5 VjS9HFFsjT/2Kqm7Touk/TdKCeOpJagymmx2KlcSuiehV2lBhF753Cz0cKBUBC298/NtZwizuyr4 mCDG+KLrYVYo3Bi+JNZr3D/gBk9eGAVksNYLqn0tsWoRlebwgyENF7HlCis67K8InDNj77dHkzDV /1wQplJ/dzX2waI9Y15H0QcTYqJf/YrI/8hqUAnn1c7orZf3r6EePGKJVLjDjfNiGL3Xp9edGzaj oDdmkrxPO9XE6XuWzDx/9HFvrMOGx4CjfRvpTjJYHbmNZdox5C67Sdev5NtTapc8HuOxtWXHQZ5w nvHl1ms3Noo9bFN5S/Uim7U0CqCYhpbhjZ/8OKbYmcSgYAeL5u/mVU/4xAp/zjD3WKesfTt0jttU ivuvpQNuYwXEZu50hPZg2zX7W1utl9CkKTrB7lmYX+8ObfMae2at36PfGfP6roV/riO+soRca5rG /Gcsh7N9uPnXZeNitcc4P7Stzzg1n/YN0guTOOq6u4G3qYVJblZLBdrPzzYLSPPq8+P83XCqw3Ub 2X0= =/C0K -----END PGP SIGNATURE-----