Repository: rust
Issue: "-Zbuild_std" produces a broken std on riscv32gc-unknown-linux-gnu
Labels: C-bug,O-riscv

I tried this code:

```rust
fn main() {
    // test if allocation is totally broken
    let string = String::from("Hello RISC-V from Rust!");
    println!("{}", string);
}
```

with this .cargo/config
```toml
[unstable]
build-std = ["std", "panic_abort"]
build-std-features = ["panic_immediate_abort"]

[build]
target= "riscv32gc-unknown-linux-gnu"

[target.riscv32gc-unknown-linux-gnu]
linker = "/opt/riscv/bin/riscv32-unknown-linux-gnu-gcc"
rustflags = [
    "-C", "link-arg=-march=rv32gc",
    "-C", "link-arg=-mabi=ilp32d",
    "-C", "linker-flavor=gcc"
]
```

and a rv32gc ilp32d gcc linker from https://github.com/riscv/riscv-gnu-toolchain

on a vexriscv-smp soc generated by https://github.com/enjoy-digital/litex

I expected to see "Hello RISC-V from Rust!" on stdout.

Instead, this happened: 
```
[   17.788737] hello-test[114]: unhandled signal 11 code 0x1 at 0x00000000 in libc.so.6[95a48000+152000]
[   17.789848] CPU: 3 PID: 114 Comm: hello-test Not tainted 5.12.0-rc4 #2
[   17.790412] epc : 95ad1578 ra : 95ad14f8 sp : 9d5eab10
[   17.790950]  gp : 692d9800 tp : 95bc9630 t0 : 00000000
[   17.792687]  t1 : 692c8bbc t2 : 00000000 s0 : 692da480
[   17.794541]  s1 : 00000017 a0 : 00000000 a1 : 6c6c6548
[   17.795586]  a2 : 00000008 a3 : fffffff0 a4 : 692da474
[   17.802431]  a5 : 00000000 a6 : 4952206f a7 : ffffffff
[   17.803684]  s2 : 00000000 s3 : 00000000 s4 : 00000017
[   17.809056]  s5 : 95bee3a8 s6 : 9d5eae0c s7 : 00000000
[   17.810680]  s8 : 00000000 s9 : 69390680 s10: 6938e408
[   17.811760]  s11: 00000001 t3 : 95ad1424 t4 : 00000000
[   17.818930]  t5 : 0000000f t6 : 00000080
[   17.820843] status: 00000020 badaddr: 00000000 cause: 0000000f
Segmentation fault
```
GDB backtrace indicates the data for the slice (stdout) is null.
```
(gdb) bt
#0  core::panicking::panic (expr=...) at /home/paul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/panicking.rs:41
#1  0x40024458 in core::slice::raw::from_raw_parts<u8> (data=0x0, len=0)
    at /home/paul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/slice/raw.rs:89
#2  0x40016256 in alloc::vec::{impl#10}::deref<u8, alloc::alloc::Global> (self=0x4004520c <std::io::stdio::STDOUT+28>)
    at /home/paul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:2370
#3  0x40005dc8 in std::io::buffered::bufwriter::BufWriter<std::io::stdio::StdoutRaw>::buffer<std::io::stdio::StdoutRaw> (
    self=0x4004520c <std::io::stdio::STDOUT+28>)
    at /home/paul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/io/buffered/bufwriter.rs:252
#4  0x4000b4d8 in std::io::buffered::linewritershim::LineWriterShim<std::io::stdio::StdoutRaw>::buffered<std::io::stdio::StdoutRaw> (self=0x3fffefc0)
    at /home/paul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/io/buffered/linewritershim.rs:38
#5  0x4000b4f8 in std::io::buffered::linewritershim::LineWriterShim<std::io::stdio::StdoutRaw>::flush_if_completed_line<std::io::stdio::StdoutRaw> (
    self=0x3fffefc0) at /home/paul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/io/buffered/linewritershim.rs:45
#6  0x4000b59e in std::io::buffered::linewritershim::{impl#1}::write_all<std::io::stdio::StdoutRaw> (self=0x3fffefc0, buf=...)
    at /home/paul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/io/buffered/linewritershim.rs:253
#7  0x40013692 in std::io::buffered::linewriter::{impl#1}::write_all<std::io::stdio::StdoutRaw> (self=0x4004520c <std::io::stdio::STDOUT+28>, buf=...)
    at /home/paul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/io/buffered/linewriter.rs:206
#8  0x40006686 in std::io::stdio::{impl#14}::write_all (self=0x3ffff404, buf=...)
    at /home/paul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/io/stdio.rs:872
#9  0x4000b932 in std::io::Write::write_fmt::{impl#0}::write_str<std::io::stdio::StdoutLock> (self=0x3ffff380, s=...)
    at /home/paul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/io/mod.rs:1656
#10 0x4001fcc2 in core::fmt::Formatter::pad (self=0x3ffff208, s=...)
    at /home/paul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:1399
#11 0x4002040e in core::fmt::{impl#16}::fmt (self=..., f=0x3ffff208)
    at /home/paul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:2148
#12 0x4000393c in alloc::string::{impl#20}::fmt (self=0x3ffff578, f=0x3ffff208)
    at /home/paul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/string.rs:2121
#13 0x4001f316 in core::fmt::write (output=..., args=...)
    at /home/paul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:1150
#14 0x40006a4a in std::io::Write::write_fmt<std::io::stdio::StdoutLock> (self=0x3ffff404, fmt=...)
    at /home/paul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/io/mod.rs:1667
#15 0x400065f2 in std::io::stdio::{impl#13}::write_fmt (self=0x3ffff44c, args=...)
    at /home/paul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/io/stdio.rs:852
#16 0x400065a2 in std::io::stdio::{impl#12}::write_fmt (self=0x3ffff4d4, args=...)
    at /home/paul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/io/stdio.rs:826
#17 0x40006786 in std::io::stdio::print_to<std::io::stdio::Stdout> (args=..., global_s=0x40006312 <std::io::stdio::stdout>, label=...)
    at /home/paul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/io/stdio.rs:1192
#18 0x400069fa in std::io::stdio::_print (args=...)
    at /home/paul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/io/stdio.rs:1205
#19 0x40003804 in hello_test::main () at src/main.rs:4
```

Another bit of code I tried using rayon failed a malloc, it seems std is just generally broken on this architecture.

`rustc --version --verbose`:
```
rustc 1.57.0-nightly (e30b68353 2021-09-05)
binary: rustc
commit-hash: e30b68353fe22b00f40d021e7914eeb78473b3c1
commit-date: 2021-09-05
host: x86_64-unknown-linux-gnu
release: 1.57.0-nightly
LLVM version: 13.0.0
```

</p>
</details>


<!-- TRIAGEBOT_START -->

<!-- TRIAGEBOT_ASSIGN_START -->

<!-- TRIAGEBOT_ASSIGN_DATA_START$${"user":"SpriteOvO"}$$TRIAGEBOT_ASSIGN_DATA_END -->

<!-- TRIAGEBOT_ASSIGN_END -->
<!-- TRIAGEBOT_END -->