bazel-contrib / bazel-contrib/toolchains_llvm

Bad interaction of -fuse-ld=lld and statically linked libc++

Open
#388 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Starlark
Stars
371
Forks
283
Avg merge
1d 55m
Merged PRs (30d)
25

Description

Our platform is Ubuntu 22.04. We import the OpenUSD package as a prebuilt tarball (built with clang) into bazel targets for use within our code-base. A small test program that exercises some basic functionality in OpenUSD crashes during static initialization with an error like this:

```
a_rao@a-rao-ubuntu:$ bazel test //examples/hello_world:test_usd
test_usd: mbrtowc.c:104: __mbrtowc: Assertion `__mbsinit (data.__statep)' failed.
Aborted (core dumped)
```

After much debugging, the culprit appears to be `-fuse-ld=lld`, which also apparently causes` libc++.a`,` libc++abi.a` and `libunwind.a` to be statically linked. Either of these by themselves is fine, but together, they cause the crash.

Also: using `clang++` instead of `clang` also fixes the problem (without changing anything else).

The workaround is to add linkopts to the build rule for the test program:

```
cc.test(
name = "test_usd",
srcs = ["test_usd.cpp"],
args = ["/tmp/hello_world.usda"],
copts = [
"-Wno-#pragma-messages",
"-Wno-deprecated-volatile",
],
linkopts = [
"-fuse-ld=ld",
"-lc++",
"-lc++abi",
"-lunwind",
],
deps = [
"//external/OpenUSD:usd_tf",
"//external/OpenUSD:usd_usd",
"//external/OpenUSD:usd_usdGeom",
],
)
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the failure with bazel test //examples/hello_world:test_usd using the cc.test target and the OpenUSD dependencies described in the issue. Compare linking through clang versus clang++ and the -fuse-ld=lld workaround, then trace the toolchain's handling of libc++.a, libc++abi.a, and libunwind.a. Done means the test no longer crashes during static initialization without the workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.