Wrong ABI used when building compiler-rt
| Bugzilla Link | 38025 |
| Resolution | FIXED |
| Resolved on | Jul 31, 2020 13:24 |
| Version | unspecified |
| OS | Linux |
| Attachments | log of compiler-rt build failure |
| CC | @m-gupta,@riyazvp,@stephenhines |
| Fixed by commit(s) | 9657446313abd4b3b44f66a30430c0d7f6ccd7b8 |
Extended Description
While trying to build ToT compiler-rt, I get the following errors:
/usr/x86_64-pc-linux-gnu/binutils-bin/2.27.0/ld.bfd.real: i386:x86-64 architecture of input file /usr/lib64/clang/7.0.0/lib/linux/libclang_rt.builtins-x86_64.a(udivdi 3.c.o)' is incompatible with i386 output /usr/x86_64-pc-linux-gnu/binutils-bin/2.27.0/ld.bfd.real: i386:x86-64 architecture of input file /usr/lib64/clang/7.0.0/lib/linux/libclang_rt.builtins-x86_64.a(udivmo
ddi4.c.o)' is incompatible with i386 output
projects/compiler-rt/lib/sanitizer_common/CMakeFiles/RTSanitizerCommonSymbolizer.i386.dir/sanitizer_unwind_linux_libcdep.cc.o: In function __sanitizer::Unwind_GetIP(_ Unwind_Context*)': /home/llozano/llvm-mage/llvm/projects/compiler-rt/lib/sanitizer_common/sanitizer_unwind_linux_libcdep.cc:100: undefined reference to _Unwind_GetIP'
projects/compiler-rt/lib/sanitizer_common/CMakeFiles/RTSanitizerCommonSymbolizer.i386.dir/sanitizer_unwind_linux_libcdep.cc.o: In function Unwind_GetIP': /home/llozano/llvm-mage/llvm/projects/compiler-rt/lib/sanitizer_common/sanitizer_unwind_linux_libcdep.cc:100: undefined reference to _Unwind_GetIP
It seems the build process is trying to use 64-bit ABI object files to build a 32-bit compiler-rt.
To reproduce you need to use a clang to build compiler-rt and that clang must use compiler-rt by default and support generating 32-ABI objects.
you can try to build your own clang build compiler or use the following from the Chrome OS sdk.
$ gsutil cp gs://chromiumos-sdk/2018/07/x86_64-cros-linux-gnu-2018.07.01.081213.tar.xz . $ tar -Jxf x86_64-cros-linux-gnu-2018.07.01.081213.tar.xz
Then:
cmake -G Ninja -DCMAKE_C_COMPILER=/path/to/sdk/usr/bin/clang -DCMAKE_CXX_COMPILER=$HOME/path/to/sdk/usr/bin/clang++ -DCLANG_DEFAULT_CXX_STDLIB=libc++ -DCLANG_DEFAULT_RTLIB=compiler-rt ../llvm/
and then:
ninja ubsan
I did a bisect on compiler-rt and found that the problem is with this commit:
Author: Petr Hosek [email protected] Date: Tue May 22 22:58:48 2018 +0000
[CMake] Support builtins as Clang default rtlib in compiler-rt
Use compiler-rt builtins when selected as default Clang rtlib and avoid
explicitly passing -rtlib= flag to avoid the "argument unused during
compilation" warning.
This is a partial alternative to D47094 that does not rely on compiler
runtime checks.
Differential Revision: https://reviews.llvm.org/D47115
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@333037 91177308-0d34-0410-b5e6-96231b3b80d8
Therefore, I will assign to Petr.
Attached a detailed log of the failure.
mentioned in issue llvm/llvm-bugzilla-archive#42409
https://reviews.llvm.org/rG965744
Have uploaded a review on Phabricator: https://reviews.llvm.org/D74133
Please review.
Any movement on this issue? I'm seeing this with a bootstrap build of 9.0.1.
I can upload to Phabricator but do you need a patch generated with the mainline (the one I uploaded here is from release_70)? BTW, I will need someone to commit the changes eventually. Also, do you think further cleanup is possible? For example, is --target=${CMAKE_CXX_COMPILER_ARCH} in find_compiler_rt required? I didn't touch it as I wasn't sure.
This is what I was going to suggest as a possible solution. I'd be happy to review this if you want to send it for review, or I can upload it to Phabricator myself if that's easier for you.
A possible fix for this bug I tried a "fix" that seems to work for me. This is what I did:
-
Do not set SANITIZER_COMMON_LINK_LIBS to COMPILER_RT_BUILTINS_LIBRARY in compiler-rt/CMakeLists.txt
-
Add an additional target argument to find_compiler_rt_library, i.e. function(find_compiler_rt_library name target variable). Then if target is non-empty pass it to --target option of clang.
-
Change add_compiler_rt_runtime to then call find_compiler_rt_library. Call get_compiler_rt_target(${arch} target) to get the target to pass to find_compiler_rt_library.
Attached is the patch file generated on release_70. I can generate it for trunk and send for review if these changes sound OK. Let me know.
I tried a "fix" that seems to work for me. This is what I did:
-
Do not set SANITIZER_COMMON_LINK_LIBS to COMPILER_RT_BUILTINS_LIBRARY in compiler-rt/CMakeLists.txt
-
Add an additional target argument to find_compiler_rt_library, i.e. function(find_compiler_rt_library name target variable). Then if target is non-empty pass it to --target option of clang.
-
Change add_compiler_rt_runtime to then call find_compiler_rt_library. Call get_compiler_rt_target(${arch} target) to get the target to pass to find_compiler_rt_library.
Attached is the patch file generated on release_70. I can generate it for trunk and send for review if these changes sound OK. Let me know.
I am running into this problem as well. What is a fix or work-around for this?
There are two different things going on:
-
Using CLANG_DEFAULT_RTLIB to decide whether to use compiler-rt introduced in r333037 and CLANG_DEFAULT_STDLIB to decided whether to use libc++ introduced in r333010 only works when using the just built Clang which is not the case here. That's definitely a regression introduced by my changes.
-
find_compiler_rt_library used in https://github.com/llvm-mirror/compiler-rt/blob/master/cmake/config-ix.cmake#L18 only considers default architecture but that isn't going to work when building both 32 and 64-bit versions since compiler-rt builtin name contains the architecture (e.g. libclang_rt.builtins-x86_64.a). This is a latent bug that has been present prior to my change.
However, even reverting r333037 and r333010 isn't necessarily the right solution. On my machine, I don't have 32-bit version of libstdc++ installed. Clang that's part of Chrome OS SDK is configured to use libc++ and SDK contains both 32-bit and 64-bit version libc++. test_target_arch (https://github.com/llvm-mirror/compiler-rt/blob/master/cmake/Modules/CompilerRTUtils.cmake#L127) succeeds for both -m32 and -m64 because Clang uses libc++. The build itself then fails during compilation because without r333010 because CMake defaults to libstdc++ and it fails to find the 32-bit version:
FAILED: lib/clang/7.0.0/lib/linux/libclang_rt.ubsan_standalone-i386.so /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/6.4.0/libstdc++.so when searching for -lstdc++ /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/6.4.0/libstdc++.a when searching for -lstdc++ /usr/bin/ld: cannot find -lstdc++ clang-7: error: linker command failed with exit code 1 (use -v to see invocation)
I think the correct solution is the one I attempted in D46857 which landed in r332683 and got reverted in r332754 because it broke Windows. That change detects the runtime and standard library to use by testing the host compiler rather than trying to guess based on various CMake flags.
I'll try to revive D46857 and update it to make sure it works on all platforms. The find_compiler_rt_library issue needs to be addressed separately but it's something we also need to resolve to allow using compiler-rt builtins with compiler-rt runtimes.
Petr said this could be fixed in r334903, related discussion is in https://reviews.llvm.org/D47169.
However, I tried today with ToT of compiler-rt and the problem is still there.
Note: This is when building llvm + clang + compiler-rt together with clang checkout in tools/clang and compiler-rt checkout in projects/compiler-rt.
Issue is caused because of linking 64-bit compiler-rt when building 32-bit sanitizers.
$ Create llvm + clang + compiler-rt checkout $ mkdir build && cd build $ cmake -G Ninja -DCMAKE_C_COMPILER=/path/to/sdk/usr/bin/clang -DCMAKE_CXX_COMPILER=$HOME/path/to/sdk/usr/bin/clang++ -DCLANG_DEFAULT_CXX_STDLIB=libc++ -DCLANG_DEFAULT_RTLIB=compiler-rt ../llvm/ $ ninja ubsan fails with error: /usr/x86_64-pc-linux-gnu/binutils-bin/2.27.0/ld.bfd.real: i386:x86-64 architecture of input file `/usr/lib64/clang/7.0.0/lib/linux/libclang_rt.builtins-x86_64.a(udivdi 3.c.o)' is incompatible with i386 output (ninja asan also fails with same error)
assigned to @petrhosek