Repository: rust
Issue: Regression: undefined symbol `__atomic_load_4` on risvc32imac-unknown-none-elf
Labels: P-medium,regression-from-stable-to-stable,C-bug,O-riscv,ICEBreaker-Cleanup-Crew,A-LTO,A-atomic

### Description

When building for `riscv32imac-unknown-none-elf`, `rustc` is occasionally generating calls to `__atomic_load_4`. This symbol doesn't appear to exist, even though atomics are supported on this platform. Furthermore, I'm not sure if atomics are used anywhere in this codepath -- `log` is the only crate in the dependency tree that I haven't verified.

This works fine if I target `riscv32i-unknown-none-elf`, which disables atomic generation.

Furthermore, this builds fine if I use rustc `1.51`, however it fails with rustc `1.52.1`.

### Background

I'm trying to work with the crate `gdbstub` to integrate it into a kernel running on baremetal RISC-V. The target is `riscv32imac-unknown-none-elf`, which has atomics. For some reason, the compiler is generating calls to `__atomic_load_4`, which doesn't exist.

### Meta
<!--
If you're using the stable version of the compiler, you should also check if the
bug also exists in the beta or nightly versions.
-->

`rustc --version --verbose`:
```
rustc 1.52.1 (9bc8c42bb 2021-05-09)
binary: rustc
commit-hash: 9bc8c42bb2f19e745a63f3445f1ac248fb015e53
commit-date: 2021-05-09
host: x86_64-unknown-linux-gnu
release: 1.52.1
LLVM version: 12.0.0
```

<details><summary>rustc 1.51</summary>
<p>

```
user@Cuboid:/mnt/d/Code/Xous/Core/kernel$ cargo +1.51 build --target riscv32imac-unknown-none-elf
warning: unused variable: `gdb_interrupt`
   --> src/debug.rs:168:13
    |
168 |             gdb_interrupt: GdbInterrupt<'_>,
    |             ^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_gdb_interrupt`
    |
    = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `register_thread`
   --> src/debug.rs:257:13
    |
257 |             register_thread: &mut dyn FnMut(Tid),
    |             ^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_register_thread`

warning: field is never read: `pid`
   --> src/debug.rs:136:9
    |
136 |         pid: Option<xous_kernel::PID>,
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: `#[warn(dead_code)]` on by default

warning: field is never read: `tid`
   --> src/debug.rs:137:9
    |
137 |         tid: Option<xous_kernel::TID>,
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: field is never read: `thread_mask`
   --> src/debug.rs:138:9
    |
138 |         thread_mask: usize,
    |         ^^^^^^^^^^^^^^^^^^

warning: variant is never constructed: `Debug`
  --> src/services.rs:61:5
   |
61 |     Debug(usize),
   |     ^^^^^^^^^^^^

warning: 6 warnings emitted

    Finished dev [unoptimized + debuginfo] target(s) in 0.76s
user@Cuboid:/mnt/d/Code/Xous/Core/kernel$
```

</p>
</details>

<details><summary>rustc 1.52.1</summary>
<p>

```
user@Cuboid:/mnt/d/Code/Xous/Core/kernel$ cargo +stable build --target riscv32imac-unknown-none-elf
   Compiling kernel v0.8.2 (/mnt/d/Code/Xous/Core/kernel)
warning: unused variable: `gdb_interrupt`
   --> src/debug.rs:168:13
    |
168 |             gdb_interrupt: GdbInterrupt<'_>,
    |             ^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_gdb_interrupt`
    |
    = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `register_thread`
   --> src/debug.rs:257:13
    |
257 |             register_thread: &mut dyn FnMut(Tid),
    |             ^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_register_thread`

warning: field is never read: `pid`
   --> src/debug.rs:136:9
    |
136 |         pid: Option<xous_kernel::PID>,
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: `#[warn(dead_code)]` on by default

warning: field is never read: `tid`
   --> src/debug.rs:137:9
    |
137 |         tid: Option<xous_kernel::TID>,
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: field is never read: `thread_mask`
   --> src/debug.rs:138:9
    |
138 |         thread_mask: usize,
    |         ^^^^^^^^^^^^^^^^^^

warning: variant is never constructed: `Debug`
  --> src/services.rs:61:5
   |
61 |     Debug(usize),
   |     ^^^^^^^^^^^^

error: linking with `rust-lld` failed: exit code: 1
  |
  = note: "rust-lld" "-flavor" "gnu" "-plugin-opt=O0" "-plugin-opt=mcpu=generic-rv32" "-L" "/home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/riscv32imac-unknown-none-elf/lib" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/kernel-a49a1beda364ba61.kernel.2tnp50fl-cgu.0.rcgu.o" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/kernel-a49a1beda364ba61.kernel.2tnp50fl-cgu.1.rcgu.o" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/kernel-a49a1beda364ba61.kernel.2tnp50fl-cgu.10.rcgu.o" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/kernel-a49a1beda364ba61.kernel.2tnp50fl-cgu.11.rcgu.o" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/kernel-a49a1beda364ba61.kernel.2tnp50fl-cgu.12.rcgu.o" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/kernel-a49a1beda364ba61.kernel.2tnp50fl-cgu.13.rcgu.o" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/kernel-a49a1beda364ba61.kernel.2tnp50fl-cgu.14.rcgu.o" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/kernel-a49a1beda364ba61.kernel.2tnp50fl-cgu.15.rcgu.o" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/kernel-a49a1beda364ba61.kernel.2tnp50fl-cgu.2.rcgu.o" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/kernel-a49a1beda364ba61.kernel.2tnp50fl-cgu.3.rcgu.o" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/kernel-a49a1beda364ba61.kernel.2tnp50fl-cgu.4.rcgu.o" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/kernel-a49a1beda364ba61.kernel.2tnp50fl-cgu.5.rcgu.o" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/kernel-a49a1beda364ba61.kernel.2tnp50fl-cgu.6.rcgu.o" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/kernel-a49a1beda364ba61.kernel.2tnp50fl-cgu.7.rcgu.o" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/kernel-a49a1beda364ba61.kernel.2tnp50fl-cgu.8.rcgu.o" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/kernel-a49a1beda364ba61.kernel.2tnp50fl-cgu.9.rcgu.o" "-o" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/kernel-a49a1beda364ba61" "--gc-sections" "-L" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps" "-L" "/mnt/d/Code/Xous/Core/kernel/target/debug/deps" "-L" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/build/kernel-b2cf221a795a62c2/out" "-L" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/build/kernel-b2cf221a795a62c2/out" "-L" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/build/riscv-4ccdf2c7a1922587/out" "-L" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/build/xous-1a7daf4c65ecd197/out" "-L" "/home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/riscv32imac-unknown-none-elf/lib" "-Bstatic" "--whole-archive" "-lkernel" "--no-whole-archive" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/libgdbstub_arch-56fd0be17381d6a4.rlib" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/libxous-39428ed2b3a43c31.rlib" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/libriscv-be84ff84a45e1b85.rlib" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/libbit_field-e83fefc77bfd4655.rlib" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/libbare_metal-4609200987315921.rlib" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/libgdbstub-517875492397d3c0.rlib" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/libcfg_if-5e47378e83c91cff.rlib" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/libnum_traits-34dfc229d6370397.rlib" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/libmanaged-9d0d80e606c7b727.rlib" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/liblog-4269a8ed40a0932a.rlib" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/libcfg_if-2c07cfa8a1d4ab2f.rlib" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/libutralib-18b768caf3ee14fc.rlib" "/mnt/d/Code/Xous/Core/kernel/target/riscv32imac-unknown-none-elf/debug/deps/libbitflags-46bd593bb6538427.rlib" "/home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/riscv32imac-unknown-none-elf/lib/librustc_std_workspace_core-525cd988d4e88ede.rlib" "/home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/riscv32imac-unknown-none-elf/lib/libcore-d1be523e2bc04c86.rlib" "/home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/riscv32imac-unknown-none-elf/lib/libcompiler_builtins-f400fa914b91ce44.rlib" "-Tlink.x" "-Bdynamic"
  = note: rust-lld: error: undefined symbol: __atomic_load_4
          >>> referenced by atomic.rs:0 (/home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/sync/atomic.rs:0)
          >>>               lto.tmp:(core::sync::atomic::AtomicUsize::load::h0594e1bc6e9a0e87)
          >>> referenced by atomic.rs:0 (/home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/sync/atomic.rs:0)
          >>>               lto.tmp:(core::sync::atomic::atomic_load::h507cc031d576ee38)


error: aborting due to previous error; 6 warnings emitted

error: could not compile `kernel`

To learn more, run the command again with --verbose.
user@Cuboid:/mnt/d/Code/Xous/Core/kernel$
```

</p>
</details>