| Message ID | 8eb0155d-cbb3-4780-a81f-c360aa27bdde@126.com |
|---|---|
| State | New |
| Headers |
Return-Path: <binutils-bounces~patchwork=sourceware.org@sourceware.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D5F0F4BAE7F5 for <patchwork@sourceware.org>; Mon, 27 Jul 2026 15:39:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D5F0F4BAE7F5 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=126.com header.i=@126.com header.a=rsa-sha256 header.s=s110527 header.b=Gw+JdRfH X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from m16.mail.126.com (m16.mail.126.com [117.135.210.6]) by sourceware.org (Postfix) with ESMTPS id 53BFA4BAE7F3 for <binutils@sourceware.org>; Mon, 27 Jul 2026 15:38:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 53BFA4BAE7F3 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=126.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=126.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 53BFA4BAE7F3 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=117.135.210.6 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1785166731; cv=none; b=Joa9bimfO5y78oW67oJhbK/pKDbWZeF+AZ7/KhTLp3fZkNpNX5EtDXeTOXOzLfU9bizSBZliS3R6iyAcg6gT4wy/23pUEcdvt/x9aTHPdlGFaBwsuzvbHDBgU4LbG0nJKyjZDH8A1YuCHvoeyf9rvZEdsX+7hE9bBU7UUoz2rZk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1785166731; c=relaxed/simple; bh=RbLOcncKsadAvg/aVhC/Ycvo0xgto5NlKrWVRYKAnyk=; h=DKIM-Signature:Message-ID:Date:MIME-Version:To:From:Subject; b=o1hldW+41BintD+177WC97cl5p8udRrbPIEG5hU01dJIiBNBCho0kE3EbO4jfF7K6InaqtuopxBsv+rxiYp8D4o8/I5H0N7aWAj3JjS6Jt3SxWgSih/zPYvZZ5pUAaB1ONyCVaONfMZKo/1cYq72O3UVbogg8CfdxN3m1cCEDes= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=126.com header.i=@126.com header.a=rsa-sha256 header.s=s110527 header.b=Gw+JdRfH DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 53BFA4BAE7F3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=Message-ID:Date:MIME-Version:To:From:Subject: Content-Type; bh=RbLOcncKsadAvg/aVhC/Ycvo0xgto5NlKrWVRYKAnyk=; b=Gw+JdRfHfHW8mQ+cLsXDqAu+BFdiAK1kRFp7PY+TOxI6JV4h4l+SHwIPeiTsxi uxN6aW/r9KCKRCzb6iXGx4yZUvBFIaO9SSJ27DWOo9YMbx/Tm0aDwa+8TDMN0vwU PjT/R4ywXQdh+DFNIuHXBywI+WkAoql2XpG70RPfmg/DI= Received: from [192.168.71.2] (unknown []) by gzga-smtp-mtada-g0-4 (Coremail) with SMTP id _____wDHF42Ce2dq8gbLEQ--.25330S2; Mon, 27 Jul 2026 23:38:42 +0800 (CST) Message-ID: <8eb0155d-cbb3-4780-a81f-c360aa27bdde@126.com> Date: Mon, 27 Jul 2026 23:38:42 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: binutils@sourceware.org, Jan Beulich <jbeulich@suse.com> From: LIU Hao <lh_mouse@126.com> Subject: ld: Prevent `_tls_used` and `_load_config_used` from being garbage-collected Autocrypt: addr=lh_mouse@126.com; keydata= xsFNBGYM6IcBEACoBF3fd3C+pIaYeyfgXx0xWuGH6wbyOwvF6Xyk6vo7gls8zgC9Lr/pJaeG i5QK+R9PHug0e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AAHCwXsEGAEIACYWIQRiZJBZjgMTOrgVSziF+95mXeiAGwUCZgzoigUJBaOagAIbDAAKCRCF +95mXeiAG98gD/iD39xJE60hmbfSNXPF3SUMOe23G1x8vGE74M6TJMPhzVDyfvQ3gCfZwg46 q3msIM/K9nuDt8HoBkBVtYBOLi3wQrNjAqmnYZTILNbr1Oinh/LlDdvh5od/QNwHF6zY733l Vlg2nky7sydK1GF3LdKajNyNnKJDnrt+dnm0Gf2Jzm2MQ4HuzxPTtJHvzVNKcnzn5wb2l+Em V8iAnoQdEE/OQg293SbEvpgdMwrvdW5ujHJ3aGcvvzdFR7oMVgq+Y8pkIs3LcGV6ItltOtaC 9R+FRk4Uf1iRWMOlrkbM9iOcJgtpQhRpZJ2Synsv+2xUH8iFZY+yxNnj4w4nbfE7pjFAsoqf Hhv8CvrK6sKrNYrqWQz26kYMVeCogXN9b89rpB0/E38F6MBa81EqHro0QbA2RVa+WHWhoJtU tVDj0F5NlGC4qYODt/xpE5VbU7Jd2z64eB2mnltUzrPaMK+hOip6mGyNh9gHbM/0aO7/Q9s1 RY0ph+Aj2066NJdqWRDdPmRKwgKe1cIXar+Xw4R2Y1qrDZPxOk29crTkC/wiF/smS6FxJLiR k/3FE8EDZQDGI7oJWoBxXOwiuQhtWkLT4CetPNYT0GdPkPdEzdhz8P9+sFVIY1p/gdX+XIN/ g76F9ko5gDM9HP8aP32iFEPcz9rCoXK+AEt6XFXrtEEFITkT Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="------------cSKT5xU3L0NHcoL9NhEoGisp" X-CM-TRANSID: _____wDHF42Ce2dq8gbLEQ--.25330S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxAryUurW8Cry7Jry5try7KFg_yoW5Jw47pa s8CFn2k3yfJ3Z3A34fAFy8ZFySvwn5CayktrnxC34qvr4UCFy0qr4ayr17AF15JFn3AF43 X3y5AFWrtFnI93DanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jOvtAUUUUU= X-Originating-IP: [124.79.125.131] X-CM-SenderInfo: 5okbz0xxvhqiyswou0bp/xtbBswJzuWpne4K3IQAA3Z X-Spam-Status: No, score=-3132.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_PBL, SPF_HELO_NONE, SPF_PASS, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Binutils mailing list <binutils.sourceware.org> List-Unsubscribe: <https://sourceware.org/mailman/options/binutils>, <mailto:binutils-request@sourceware.org?subject=unsubscribe> List-Archive: <https://sourceware.org/pipermail/binutils/> List-Post: <mailto:binutils@sourceware.org> List-Help: <mailto:binutils-request@sourceware.org?subject=help> List-Subscribe: <https://sourceware.org/mailman/listinfo/binutils>, <mailto:binutils-request@sourceware.org?subject=subscribe> Errors-To: binutils-bounces~patchwork=sourceware.org@sourceware.org |
| Series |
ld: Prevent `_tls_used` and `_load_config_used` from being garbage-collected
|
|
Checks
| Context | Check | Description |
|---|---|---|
| linaro-tcwg-bot/tcwg_binutils_build--master-arm | fail | Patch failed to apply |
| linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 | fail | Patch failed to apply |
Commit Message
LIU Hao
July 27, 2026, 3:38 p.m. UTC
Hello, Below is a patch that is required for LD to work with `--gc-sections` with recent mingw-w64, tested on x86_64-w64-mingw32 and i686-w64-mingw32. I don't have access to binutils Git so please commit it for me. Also please consider backporting. From 9a98b90d43739f5979a999acda3beb21b17442b2 Mon Sep 17 00:00:00 2001 From: LIU Hao <lh_mouse@126.com> Date: Mon, 27 Jul 2026 23:11:54 +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 <lh_mouse@126.com> --- ld/emultempl/pe.em | 10 ++++++++++ ld/emultempl/pep.em | 5 +++++ 2 files changed, 15 insertions(+) -- 2.55.0 From 9a98b90d43739f5979a999acda3beb21b17442b2 Mon Sep 17 00:00:00 2001 From: LIU Hao <lh_mouse@126.com> Date: Mon, 27 Jul 2026 23:11:54 +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 <lh_mouse@126.com> --- ld/emultempl/pe.em | 10 ++++++++++ ld/emultempl/pep.em | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/ld/emultempl/pe.em b/ld/emultempl/pe.em index 07ef2ca5953..475e0349ff8 100644 --- 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 + #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 14be8435685..1ab5c4d88f7 100644 --- a/ld/emultempl/pep.em +++ b/ld/emultempl/pep.em @@ -1624,6 +1624,11 @@ 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. */ + lang_add_gc_name ("_tls_used"); + lang_add_gc_name ("_load_config_used"); + #ifdef DLL_SUPPORT pep_process_import_defs (link_info.output_bfd, &link_info);
Comments
On 27.07.2026 17:38, LIU Hao wrote: > From: LIU Hao <lh_mouse@126.com> > > 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`. But it does so not knowing what environment the executable targets. Aiui both names aren't exactly "reserved" in the PE world, they're more like reserved in combination with certain C or other runtime libraries, I suppose. In an EFI application, for example, they may exist but have an entirely different purpose. Then again I realize that bfd/peXXigen.c already makes a similar assumption. > --- 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. Jan
diff --git a/ld/emultempl/pe.em b/ld/emultempl/pe.em index 07ef2ca5953..475e0349ff8 100644 --- 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 + #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 14be8435685..1ab5c4d88f7 100644 --- a/ld/emultempl/pep.em +++ b/ld/emultempl/pep.em @@ -1624,6 +1624,11 @@ 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. */ + lang_add_gc_name ("_tls_used"); + lang_add_gc_name ("_load_config_used"); + #ifdef DLL_SUPPORT pep_process_import_defs (link_info.output_bfd, &link_info);