You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
guix: re-enable exported symbol checking for RISC-V #28095
i tried to figure this out today but haven't got very far. Curiously, there are even more (weak) symbols exported by the current RISC-V build than mentioned in the OP-
0000000000091fd8 w DF .text 0000000000000998 Base _ZZSt8to_arrayINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEELm35EESt5arrayINSt9remove_cvIT_E4typeEXT0_EEOAT0__S8_ENKUlTpTnmSt16integer_sequenceImJXspT_EEEE_clIJLm0ELm1ELm2ELm3ELm4ELm5ELm6ELm7ELm8ELm9ELm10ELm11ELm12ELm13ELm14ELm15ELm16ELm17ELm18ELm19ELm20ELm21ELm22ELm23ELm24ELm25ELm26ELm27ELm28ELm29ELm30ELm31ELm32ELm33ELm34EEEEDaSF_
0000000000093ade w DF .text 0000000000000148 Base std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::replace(unsigned long, unsigned long, char const*, unsigned long)
0000000000091efa w DF .text 000000000000005e Base std::array<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, 35ul>::~array()
00000000000929de w DF .text 0000000000000042 Base std::any::reset()
000000000009389e w DF .text 000000000000008a Base std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_append(char const*, unsigned long)
0000000000093928 w DF .text 00000000000000be Base std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::append(char const*)
0000000000091d88 w DF .text 000000000000006e Base std::any::_Manager_internal<node::NodeContext*>::_S_manage(std::any::_Op, std::any const*, std::any::_Arg*)
000000000009372c w DF .text 0000000000000172 Base std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_mutate(unsigned long, unsigned long, char const*, unsigned long)
0000000000092a20 w DF .text 0000000000000068 Base std::__cxx11::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::~basic_stringbuf()
0000000000091fa4 w DF .text 0000000000000034 Base std::_Function_base::~_Function_base()
0000000000092afe w DF .text 000000000000004c Base std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_dispose()
0000000000091cb0 w DF .text 0000000000000028 Base std::ctype<char>::do_widen(char) const
One thing we can say for sure: the large number of symbols here makes it impractical to add specific exceptions.
Haven't been able to explain why there is a difference between RISC-V and other platforms here. i couldn't find any ABI documentation that mentions a difference in handling weak symbols.
Next step i'll see if i can isolate a small test case that can be compiled on different cross-compilers to reproduce the issue.
$ x86_64-linux-gnu-g++ test.cpp -o test.x64 -Wl,--exclude-libs,ALL -fvisibility=hidden -static-libstdc++
$ x86_64-linux-gnu-objdump -T test.x64|grep "\.text"
(no output)
$ riscv64-linux-gnu-g++ test.cpp -o test.rv -Wl,--exclude-libs,ALL -fvisibility=hidden -static-libstdc++
$ riscv64-linux-gnu-objdump -T test.rv|grep "\.text"
00000000000176b0 l d .text 0000000000000000 .text
0000000000018810 w DF .text 00000000000000fe Base _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPKcEEvT_S8_St20forward_iterator_tag
On x86_64, even providing just -static-libstdc++ gives no exported weak symbols from the binary.
On riscv64, leaving out -fvisibility=hidden gives one exported symbol main. Leaving out -fvisibility=hidden makes the build export a ton of symbols.
As the versions of all build tools and libraries are the same for both platforms, i suspect this is a linker bug specific to RISC-V.
(i've checked the intermediate .o files and the forward_iterator_tag symbols in those have the same flags, so expect it is not the compiler)
All in all, this appears to be an upstream issue, not something we can fix in our build. When i get around to it i'll check with the latest binutils to see if it has been fixed upstream in the meantime.
The problem still exists in the latest binutils built from git (d07a59a5ca830bf74705471f6bea6db1a47da2b5 as of 2025-03-07).
i've been digging a bit as to what causes some weak symbols (but not all) to end up in the binary: overall, if a weak function symbol is in both libstdc++.a and in the .o file, it will end up exported in the binary. This behavior exists on riscv64 but not x86_64.
The only exception is the monstriosity _ZZSt8to_arrayINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEELm35EESt5arrayINSt9remove_cvIT_E4typeEXT0_EEOAT0__S8_ENKUlTpTnmSt16integer_sequenceImJXspT_EEEE_clIJLm0ELm1ELm2ELm3ELm4ELm5ELm6ELm7ELm8ELm9ELm10ELm11ELm12ELm13ELm14ELm15ELm16ELm17ELm18ELm19ELm20ELm21ELm22ELm23ELm24ELm25ELm26ELm27ELm28ELm29ELm30ELm31ELm32ELm33ELm34EEEEDaSF_, which does not appear in libstdc++, and which objdump cannot demangle. It is likely coming from inside bitcoin's code. But will focus on the libstdc++ ones first.
If you want to know what the actual ld invocation looks like for linking a simple binary (normally this is hidden two levels, inside a collect2 call inside g++):
i've compared the command line between riscv64 and x86_64, but did not notice any differences that could explain the behavior. So i'm still fairly convinced it's internal to binutils.
It almost looks like ld flag --no-export-dynamic, which is the default, doesn't work on RISC-V. Not even if added explicitly. Without --exclude-libs ALL, all weak symbols imported from libraries end up in the exported binary. With --exclude-libs ALL only the ones that are exported by the .o file (the behavior we're seeing).
Passing --export-dynamic to the x86_64 build approximates what we're seeing on RISC-V. But not entirely, we get more symbols than that. So some filtering is happening there, just less.
Edit: narrowed it down to the following code in allocate_dynrelocs in bfd/elf64-riscv.c:
/* Make sure this symbol is output as a dynamic symbol.
Undefined weak syms won't yet be marked as dynamic. */
if (h->dynindx == -1
&& !h->forced_local)
{
if (! bfd_elf_link_record_dynamic_symbol (info, h))
return false;
}
Commenting out the bfd_elf_link_record_dynamic_symbol call makes the extraneous symbols disappear. So it looks like this has something to do with dynamic relocations in the binary. This explains why it is RISCV specific, but doesn't rule out that the symbols are spurious.
Activity
hebasto commentedon Jul 19, 2023
Historically, symbol checking for RISC-V was skipped from the beginning for the following reason:
fanquake commentedon Jul 19, 2023
I don't think that's relevant anymore? I've listed the exported symbols above. It's 3.
laanwj commentedon Apr 11, 2024
i'll try to figure out why it exports those three symbols, and if it is indication of a problem, and if not, add them to the exceptions
demangled, these are:
laanwj commentedon Apr 29, 2024
Holding this off until #29987 is.merged.
fanquake commentedon Mar 5, 2025
This has gone in, and I've confirmed that this is still an issue with current master (0391d7e). @laanwj any chance you still wanted to investigate?
laanwj commentedon Mar 6, 2025
Yes, thanks for the reminder.
laanwj commentedon Mar 6, 2025
i tried to figure this out today but haven't got very far. Curiously, there are even more (weak) symbols exported by the current RISC-V build than mentioned in the OP-
One thing we can say for sure: the large number of symbols here makes it impractical to add specific exceptions.
Haven't been able to explain why there is a difference between RISC-V and other platforms here. i couldn't find any ABI documentation that mentions a difference in handling weak symbols.
Next step i'll see if i can isolate a small test case that can be compiled on different cross-compilers to reproduce the issue.
laanwj commentedon Mar 6, 2025
Reproduction (with gcc 13.3.0 , binutils 2.38, everything from guix build context):
x86_64, even providing just-static-libstdc++gives no exported weak symbols from the binary.riscv64, leaving out-fvisibility=hiddengives one exported symbolmain. Leaving out-fvisibility=hiddenmakes the build export a ton of symbols.As the versions of all build tools and libraries are the same for both platforms, i suspect this is a linker bug specific to RISC-V.
(i've checked the intermediate .o files and the
forward_iterator_tagsymbols in those have the same flags, so expect it is not the compiler)All in all, this appears to be an upstream issue, not something we can fix in our build. When i get around to it i'll check with the latest binutils to see if it has been fixed upstream in the meantime.
laanwj commentedon Mar 8, 2025
The problem still exists in the latest binutils built from git (d07a59a5ca830bf74705471f6bea6db1a47da2b5 as of 2025-03-07).
i've been digging a bit as to what causes some weak symbols (but not all) to end up in the binary: overall, if a weak function symbol is in both
libstdc++.aand in the.ofile, it will end up exported in the binary. This behavior exists on riscv64 but not x86_64.The only exception is the monstriosity
_ZZSt8to_arrayINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEELm35EESt5arrayINSt9remove_cvIT_E4typeEXT0_EEOAT0__S8_ENKUlTpTnmSt16integer_sequenceImJXspT_EEEE_clIJLm0ELm1ELm2ELm3ELm4ELm5ELm6ELm7ELm8ELm9ELm10ELm11ELm12ELm13ELm14ELm15ELm16ELm17ELm18ELm19ELm20ELm21ELm22ELm23ELm24ELm25ELm26ELm27ELm28ELm29ELm30ELm31ELm32ELm33ELm34EEEEDaSF_, which does not appear in libstdc++, and which objdump cannot demangle. It is likely coming from inside bitcoin's code. But will focus on the libstdc++ ones first.If you want to know what the actual
ldinvocation looks like for linking a simple binary (normally this is hidden two levels, inside acollect2call insideg++):i've compared the command line between riscv64 and x86_64, but did not notice any differences that could explain the behavior. So i'm still fairly convinced it's internal to binutils.
laanwj commentedon Mar 11, 2025
It almost looks like ld flag
--no-export-dynamic, which is the default, doesn't work on RISC-V. Not even if added explicitly. Without--exclude-libs ALL, all weak symbols imported from libraries end up in the exported binary. With--exclude-libs ALLonly the ones that are exported by the.ofile (the behavior we're seeing).Passing
--export-dynamicto the x86_64 build approximates what we're seeing on RISC-V. But not entirely, we get more symbols than that. So some filtering is happening there, just less.Edit: narrowed it down to the following code in
allocate_dynrelocsinbfd/elf64-riscv.c:Commenting out the
bfd_elf_link_record_dynamic_symbolcall makes the extraneous symbols disappear. So it looks like this has something to do with dynamic relocations in the binary. This explains why it is RISCV specific, but doesn't rule out that the symbols are spurious.laanwj commentedon Mar 12, 2025
Filed an upstream issue for binutils: https://sourceware.org/bugzilla/show_bug.cgi?id=32783
fanquake commentedon Nov 27, 2025
While there hasn't been a response to our upstream issue: https://sourceware.org/bugzilla/show_bug.cgi?id=32783, the test case seems to be improved as of binutils 2.45. I can recreate the issue using
2.44. i.e:but with
2.45, the spurious symbol is no-longer present:However using binutils 2.45.1 doesn't seem to have fixed the issue with our Guix build.
fanquake commentedon Mar 9, 2026
PR to fix this in #34757.
Merge bitcoin#34757: guix: re-enable riscv exported symbol checking