Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UBSAN] Undefined references with -Wl,--no-undefined #60578

Open
Lastique opened this issue Feb 7, 2023 · 3 comments
Open

[UBSAN] Undefined references with -Wl,--no-undefined #60578

Lastique opened this issue Feb 7, 2023 · 3 comments
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' compiler-rt:ubsan Undefined behavior sanitizer

Comments

@Lastique
Copy link

Lastique commented Feb 7, 2023

When linking a shared library with -Wl,--no-undefined switch and UBSan enabled, the linking fails with unresolved references to UBSan symbols.

Here's a test case:

ubsan_unresolved_test.cpp:

int test(int n)
{
    return n + 10;
}

Commands:

clang++ -stdlib=libc++ -fsanitize=undefined -fno-sanitize-recover=undefined -o ubsan_unresolved_test.o -c ubsan_unresolved_test.cpp
clang++ -stdlib=libc++ -fsanitize=undefined -fuse-ld=gold -Wl,--no-undefined -shared -o ubsan_unresolved_test.so ubsan_unresolved_test.o

Output:

ubsan_unresolved_test.o:ubsan_unresolved_test.cpp:function test(int): error: undefined reference to '__ubsan_handle_add_overflow_abort'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Clang version:

$ clang -v
Ubuntu clang version 14.0.0-1ubuntu1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Candidate multilib: .;@m64
Selected multilib: .;@m64

This is on Kubuntu 22.04, x86-64.

The workaround is to add -lubsan to the linker command line.

@EugeneZelenko
Copy link
Contributor

Isn't this gold problem?

@Lastique
Copy link
Author

Lastique commented Feb 7, 2023

I'm assuming it is the clang driver that is responsible for adding UBSAN libraries to the gold command line.

Also, I should note that the problem does not reproduce with gcc 11.3 on the same system. It also uses gold.

Lastique added a commit to boostorg/log that referenced this issue Feb 28, 2023
@Endilll Endilll added clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' compiler-rt:ubsan Undefined behavior sanitizer and removed new issue labels Sep 20, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Sep 20, 2023

@llvm/issue-subscribers-clang-driver

When linking a shared library with `-Wl,--no-undefined` switch and UBSan enabled, the linking fails with unresolved references to UBSan symbols.

Here's a test case:

ubsan_unresolved_test.cpp:

int test(int n)
{
    return n + 10;
}

Commands:

clang++ -stdlib=libc++ -fsanitize=undefined -fno-sanitize-recover=undefined -o ubsan_unresolved_test.o -c ubsan_unresolved_test.cpp
clang++ -stdlib=libc++ -fsanitize=undefined -fuse-ld=gold -Wl,--no-undefined -shared -o ubsan_unresolved_test.so ubsan_unresolved_test.o

Output:

ubsan_unresolved_test.o:ubsan_unresolved_test.cpp:function test(int): error: undefined reference to '__ubsan_handle_add_overflow_abort'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Clang version:

$ clang -v
Ubuntu clang version 14.0.0-1ubuntu1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Candidate multilib: .;@<!-- -->m64
Selected multilib: .;@<!-- -->m64

This is on Kubuntu 22.04, x86-64.

The workaround is to add -lubsan to the linker command line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' compiler-rt:ubsan Undefined behavior sanitizer
Projects
None yet
Development

No branches or pull requests

4 participants