Skip to content

guix: re-enable exported symbol checking for RISC-V #28095

Closed

Description

@fanquake

This is currently disabled, when it can likely be fixed and re-enabled:

if binary.header.machine_type == lief.ELF.ARCH.RISCV or name in IGNORE_EXPORTS:

A Guix build with the exception dropped shows there are symbols being exported in the RISC-V bins:

/opt/homebrew/opt/binutils/bin/objdump -T bitcoind

0000000000080500  w   DF .text	00000000000000a6  Base        _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1IS3_EEPKcRKS3_
000000000007f6a0  w   DF .text	0000000000000064  Base        _ZNSt7__cxx1115basic_stringbufIcSt11char_traitsIcESaIcEED1Ev
000000000007e9da  w   DF .text	0000000000000024  Base        _ZNKSt5ctypeIcE8do_widenEc

std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string<std::allocator<char> >(char const*, std::allocator<char> const&)
std::__cxx11::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::~basic_stringbuf()
std::ctype<char>::do_widen(char) const

Activity

  1. hebasto commented on Jul 19, 2023

    @hebasto
    Member

    Historically, symbol checking for RISC-V was skipped from the beginning for the following reason:

    Need to skip RISC-V for now, the linker would export so many symbols.

  2. fanquake commented on Jul 19, 2023

    @fanquake
    MemberAuthor

    I don't think that's relevant anymore? I've listed the exported symbols above. It's 3.

  3. self-assigned this
    on Apr 11, 2024
  4. laanwj commented on Apr 11, 2024

    @laanwj
    Member

    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:

    std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string<std::allocator<char> >(char const*, std::allocator<char> const&)
    std::__cxx11::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::~basic_stringbuf()
    std::ctype<char>::do_widen(char) const
    
  5. laanwj commented on Apr 29, 2024

    @laanwj
    Member

    Holding this off until #29987 is.merged.

  6. fanquake commented on Mar 5, 2025

    @fanquake
    MemberAuthor

    Holding this off until #29987 is.merged.

    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?

  7. laanwj commented on Mar 6, 2025

    @laanwj
    Member

    Yes, thanks for the reminder.

  8. laanwj commented on Mar 6, 2025

    @laanwj
    Member

    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.

  9. laanwj commented on Mar 6, 2025

    @laanwj
    Member

    Reproduction (with gcc 13.3.0 , binutils 2.38, everything from guix build context):

    #include <iostream>
    
    int main()
    {
        std::string test{"12345"};
        std::cout << test << std::endl;
    
        return 0;
    }
    $ 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.

  10. laanwj commented on Mar 8, 2025

    @laanwj
    Member

    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++):

    $LD --sysroot=/ --eh-frame-hdr -melf64lriscv \
        -dynamic-linker /gnu/store/whzxf4x97y7j2gcrg8p90n3iqiy9ssal-glibc-cross-riscv64-linux-gnu-2.31/lib/ld-linux-riscv64-lp64d.so.1 \
        -pie -o ${BINARY} \
        /gnu/store/whzxf4x97y7j2gcrg8p90n3iqiy9ssal-glibc-cross-riscv64-linux-gnu-2.31/lib/Scrt1.o \
        /gnu/store/f1blyzzq50h5gx0aimp7d0m72xvmdk3a-gcc-cross-riscv64-linux-gnu-13.3.0-lib/lib/gcc/riscv64-linux-gnu/13.3.0/crti.o \
        /gnu/store/f1blyzzq50h5gx0aimp7d0m72xvmdk3a-gcc-cross-riscv64-linux-gnu-13.3.0-lib/lib/gcc/riscv64-linux-gnu/13.3.0/crtbeginS.o \
        -L/gnu/store/f1blyzzq50h5gx0aimp7d0m72xvmdk3a-gcc-cross-riscv64-linux-gnu-13.3.0-lib/lib  \
        -L/gnu/store/f1blyzzq50h5gx0aimp7d0m72xvmdk3a-gcc-cross-riscv64-linux-gnu-13.3.0-lib/lib/gcc/riscv64-linux-gnu/13.3.0 \
        -L/gnu/store/whzxf4x97y7j2gcrg8p90n3iqiy9ssal-glibc-cross-riscv64-linux-gnu-2.31/lib \
        -L/gnu/store/25fk4fj4n7r3djxq9hqn54mf4pcs0irj-glibc-cross-riscv64-linux-gnu-2.31-static/lib \
        -L/gnu/store/f1blyzzq50h5gx0aimp7d0m72xvmdk3a-gcc-cross-riscv64-linux-gnu-13.3.0-lib/lib/gcc/riscv64-linux-gnu/13.3.0 \
        -L/gnu/store/f1blyzzq50h5gx0aimp7d0m72xvmdk3a-gcc-cross-riscv64-linux-gnu-13.3.0-lib/lib/gcc/riscv64-linux-gnu/13.3.0/../../../../riscv64-linux-gnu/lib \
        -L/gnu/store/whzxf4x97y7j2gcrg8p90n3iqiy9ssal-glibc-cross-riscv64-linux-gnu-2.31/lib \
        $OBJECT \
        --exclude-libs ALL -Bstatic -lstdc++ -Bdynamic -lm -lgcc_s -lgcc \
        -L/gnu/store/whzxf4x97y7j2gcrg8p90n3iqiy9ssal-glibc-cross-riscv64-linux-gnu-2.31/lib \
        -rpath-link=/gnu/store/whzxf4x97y7j2gcrg8p90n3iqiy9ssal-glibc-cross-riscv64-linux-gnu-2.31/lib \
        -rpath-link=/gnu/store/f1blyzzq50h5gx0aimp7d0m72xvmdk3a-gcc-cross-riscv64-linux-gnu-13.3.0-lib/riscv64-linux-gnu/lib \
        -lgcc_s -lc -lgcc_s -lgcc \
        /gnu/store/f1blyzzq50h5gx0aimp7d0m72xvmdk3a-gcc-cross-riscv64-linux-gnu-13.3.0-lib/lib/gcc/riscv64-linux-gnu/13.3.0/crtendS.o \
        /gnu/store/f1blyzzq50h5gx0aimp7d0m72xvmdk3a-gcc-cross-riscv64-linux-gnu-13.3.0-lib/lib/gcc/riscv64-linux-gnu/13.3.0/crtn.o
    

    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.

  11. laanwj commented on Mar 11, 2025

    @laanwj
    Member

    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.

  12. laanwj commented on Mar 12, 2025

    @laanwj
    Member
  13. fanquake commented on Nov 27, 2025

    @fanquake
    MemberAuthor

    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:

    # riscv64-linux-gnu-ld --version
    GNU ld (GNU Binutils for Debian) 2.44
    # 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"
    0000000000016820 l    d  .text	0000000000000000              .text
    0000000000017b54  w   DF .text	00000000000000f0  Base        _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPKcEEvT_S8_St20forward_iterator_tag

    but with 2.45, the spurious symbol is no-longer present:

    # riscv64-linux-gnu-ld --version
    GNU ld (GNU Binutils for Debian) 2.45.50.20251125
    # 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"
    0000000000016780 l    d  .text	0000000000000000              .text

    However using binutils 2.45.1 doesn't seem to have fixed the issue with our Guix build.

  14. fanquake commented on Mar 9, 2026

    @fanquake
    MemberAuthor

    PR to fix this in #34757.

  15. added a commit that references this issue on Apr 16, 2026
    c4361e5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions