<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/54790>54790</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            LLD requires `libatomic` in 32-bit mode
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Kobzol
      </td>
    </tr>
</table>

<pre>
    Hi!

We're trying to [migrate](https://github.com/rust-lang/rust/pull/95171) the Rust compiler to use LLVM 14 on CI. I have found a build issue that happens when we try to build `lld 14.0.0` with `clang 14.0.0` when cross-compiling to `i686` from a `x64` host system. Even though the CMake check claims that `LIBATOMIC` is not required:
```
-- Performing Test HAVE_CXX_ATOMICS_WITHOUT_LIB
-- Performing Test HAVE_CXX_ATOMICS_WITHOUT_LIB - Success
-- Performing Test HAVE_CXX_ATOMICS64_WITHOUT_LIB
-- Performing Test HAVE_CXX_ATOMICS64_WITHOUT_LIB - Success
-- Performing Test LLVM_HAS_ATOMICS
-- Performing Test LLVM_HAS_ATOMICS - Success
```
linking `lld` later fails with this error:
```
[100%] Linking CXX executable ../../bin/lld
../../lib/liblldCOFF.a(DebugTypes.cpp.o): In function `void llvm::function_ref<void (unsigned int)>::callback_fn<lld::coff::TypeMerger::mergeTypesWithGHash()::$_5>(int, unsigned int)':
DebugTypes.cpp:(.text._ZN4llvm12function_refIFvjEE11callback_fnIZN3lld4coff10TypeMerger19mergeTypesWithGHashEvE3$_5EEvij+0x15b): undefined reference to `__atomic_load'
DebugTypes.cpp:(.text._ZN4llvm12function_refIFvjEE11callback_fnIZN3lld4coff10TypeMerger19mergeTypesWithGHashEvE3$_5EEvij+0x1d1): undefined reference to `__atomic_compare_exchange'
clang-14: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [bin/lld] Error 1
make[1]: *** [tools/lld/CMakeFiles/lld.dir/all] Error 2
make: *** [all] Error 2
```
The CMake invocation looks like this:
```
cmake
/checkout/src/llvm-project/lld
-DCMAKE_INSTALL_MESSAGE=LAZY
-DCMAKE_C_COMPILER_LAUNCHER=sccache
-DCMAKE_CXX_COMPILER_LAUNCHER=sccache
-DCMAKE_C_COMPILER=clang
-DCMAKE_CXX_COMPILER=clang++
-DCMAKE_ASM_COMPILER=clang
-DCMAKE_C_FLAGS=-ffunction-sections -fdata-sections -fPIC --target=i686-unknown-linux-gnu -mstackrealign -fdebug-prefix-map=/checkout=/rustc/llvm
-DCMAKE_CXX_FLAGS=-ffunction-sections -fdata-sections -fPIC --target=i686-unknown-linux-gnu -fdebug-prefix-map=/checkout=/rustc/llvm
-DCMAKE_SHARED_LINKER_FLAGS= -Wl,-Bsymbolic -static-libstdc++
-DCMAKE_MODULE_LINKER_FLAGS= -Wl,-Bsymbolic -static-libstdc++
-DCMAKE_EXE_LINKER_FLAGS= -Wl,-Bsymbolic -static-libstdc++
-DLLVM_CONFIG_PATH=/checkout/obj/build/bootstrap/debug/llvm-config-wrapper
-DLLVM_INCLUDE_TESTS=OFF
-DCMAKE_CXX_STANDARD=14
-DCMAKE_INSTALL_PREFIX=/checkout/obj/build/i686-unknown-linux-gnu/lld
-DCMAKE_ASM_FLAGS= -ffunction-sections -fdata-sections -fPIC --target=i686-unknown-linux-gnu -mstackrealign
-DCMAKE_BUILD_TYPE=Release
```

The full error log can be seen [here](https://github.com/rust-lang-ci/rust/commit/0d989c1dad843f2f127f1f5670d949c43f178dc2/checks/5799020258/logs), the link error is near the bottom. The build has succeeded for numerous other platforms (https://github.com/rust-lang-ci/rust/runs/5799020258?check_suite_focus=true), but it fails for `i686` specifically.

I have tried to install `libatomic` on the build system and link to it, in that case, `lld` is successfully compiled. However, we would like to avoid linking to `libatomic`, and the CMake check seems to be wrong here.
Do you have any suggestions on how this could be fixed? Thanks!
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzNV0tv2zgQ_jXKhZAgye-DD4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