From: Michal Jires Date: Fri, 19 Dec 2025 16:09:16 +0000 (+0100) Subject: lto: Fix SegFault in ICF caused by missing body X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8e4107a1b3d430f5d3938c8068c6cd512f661e95;p=thirdparty%2Fgcc.git lto: Fix SegFault in ICF caused by missing body During LTO symbol merging, weak symbols may be resolved to external definition. We reset the symbol, so the body might be released in unreachability pass. But we didn't mark the symbol with body_removed, so ICF assumed the body was still there causing SegFault. gcc/lto/ChangeLog: * lto-symtab.cc (lto_symtab_merge_symbols): Set body_removed for symbols resolved outside of IR. gcc/testsuite/ChangeLog: * gcc.dg/lto/attr-weakref-2_0.c: New test. * gcc.dg/lto/attr-weakref-2_1.c: New test. --- diff --git a/gcc/lto/lto-symtab.cc b/gcc/lto/lto-symtab.cc index b8759a7fef5..b458462dc23 100644 --- a/gcc/lto/lto-symtab.cc +++ b/gcc/lto/lto-symtab.cc @@ -1043,6 +1043,7 @@ lto_symtab_merge_symbols (void) node->analyzed = node->definition = false; node->remove_all_references (); } + node->body_removed = true; } DECL_EXTERNAL (node->decl) = 1; } diff --git a/gcc/testsuite/gcc.dg/lto/attr-weakref-2_0.c b/gcc/testsuite/gcc.dg/lto/attr-weakref-2_0.c new file mode 100644 index 00000000000..a55ef28bd13 --- /dev/null +++ b/gcc/testsuite/gcc.dg/lto/attr-weakref-2_0.c @@ -0,0 +1,11 @@ +/* { dg-lto-do link } */ +/* { dg-lto-options {{-O2 -flto}} } */ + +#define __weak __attribute__((__weak__)) +void __weak other() {} +void __weak fn() {} + +int main() { + fn(); + other(); +} diff --git a/gcc/testsuite/gcc.dg/lto/attr-weakref-2_1.c b/gcc/testsuite/gcc.dg/lto/attr-weakref-2_1.c new file mode 100644 index 00000000000..639093fba46 --- /dev/null +++ b/gcc/testsuite/gcc.dg/lto/attr-weakref-2_1.c @@ -0,0 +1,3 @@ +/* { dg-options {{-fno-lto}} } */ + +void fn() {}