Repository: rust
Issue: Rust produces unlinkable code when writing large arrays
Labels: A-linkage,T-compiler,C-bug,A-array

<!--
Thank you for filing a bug report! 🐛 Please provide a short summary of the bug,
along with any information you feel relevant to replicating the bug.
-->

I tried this code:

```rust
fn main() {
    let mut file = std::fs::File::create("zeros.dat").unwrap();
    std::io::Write::write_all(&mut file, &[0; 2 * 1024 * 1024 * 1024]).unwrap();
}
```

I expected to see this happen: Either the code compiles or the rust compiler tells me I can't have arrays this large.

Instead, this happened: The rust compiler emits objects that cannot be linked.

<details><summary>gcc error message:</summary>

```
   Compiling foo v0.1.0 (/home/msrd0/foo)
error: linking with `cc` failed: exit status: 1
  |
  = note: "cc" "-m64" "/tmp/rustcQ0KVYh/symbols.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.11qzgizkeveaglq7.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.14w8hd0h2ijbkz7.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.16fv1eqlyjkq4zc7.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.1b2yiixjznziz9u9.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.21dhuhdnz1ux056p.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.258ucpumfqphcw2s.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.2g2dfz0v6mrxvl69.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.2ugn6ecjb3k061ti.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.2z3jeagapreyxcl.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.3bel81cwg5s05x6.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.3etxvvkl1fwp2cvk.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.41r16b8d0x92nnle.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.42ui0su1wcedhkit.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.4esz2e4sghaebwy9.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.4ii608bc1oot1vxo.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.4sbozhfrwxgfzwd6.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.4yn80cvsjwkd94yv.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.5wo5zdzd3vfvsak.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.8vndcpbsd4lqnmy.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.m9zb96hgncput9z.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.28m6ql6y38gj855u.rcgu.o" "-Wl,--as-needed" "-L" "/home/msrd0/foo/target/debug/deps" "-L" "/usr/lib64/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-8e20380b4b9fd6be.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-a06485200ce26f7a.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-2dd0454343e4f4fc.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libmemchr-d0ea83c0a7b3e9a0.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-04397e76f19401d3.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-db7410605ab6dc00.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-fe4d24a59cfc3703.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-51d7bdeefbe4cf60.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-a2986b1c83374eb4.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-d021298679525c17.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-07477ce913344fbe.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-bfcfd0b3e74b40cd.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-a99d4acd8904850e.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-95ea7685e5bfdc81.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-5dd797956cdd0919.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-2bcc936797a96b7b.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-0d5f5dfbd9c7aca5.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-9fb4851878f34383.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-0fa80fcac3958bad.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "/usr/lib64/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro,-znow" "-nodefaultlibs"
  = note: /usr/lib/gcc/x86_64-pc-linux-gnu/12.2.1/../../../../lib/Scrt1.o: in function `_start':
          (.text+0x21): relocation truncated to fit: R_X86_64_GOTPCRELX against symbol `__libc_start_main@@GLIBC_2.34' defined in .text section in /usr/lib/libc.so.6
          /usr/lib/gcc/x86_64-pc-linux-gnu/12.2.1/../../../../lib/Scrt1.o:(.eh_frame+0x20): relocation truncated to fit: R_X86_64_PC32 against `.text'
          /usr/lib/gcc/x86_64-pc-linux-gnu/12.2.1/../../../../lib/crti.o: in function `_init':
          (.init+0xb): relocation truncated to fit: R_X86_64_REX_GOTPCRELX against undefined symbol `__gmon_start__'
          /usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-8e20380b4b9fd6be.rlib(std-8e20380b4b9fd6be.std.680b7a42-cgu.0.rcgu.o): in function `<&T as core::fmt::Debug>::fmt':
          /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/core/src/fmt/mod.rs:2372:(.text._ZN42_$LT$$RF$T$u20$as$u20$core..fmt..Debug$GT$3fmt17h1adde5f64974830dE+0x5): relocation truncated to fit: R_X86_64_GOTPCREL against symbol `<std::io::error::ErrorKind as core::fmt::Debug>::fmt' defined in .text._ZN62_$LT$std..io..error..ErrorKind$u20$as$u20$core..fmt..Debug$GT$3fmt17hb68a8fe6fbe4de31E section in /usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-8e20380b4b9fd6be.rlib(std-8e20380b4b9fd6be.std.680b7a42-cgu.0.rcgu.o)
          /usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-8e20380b4b9fd6be.rlib(std-8e20380b4b9fd6be.std.680b7a42-cgu.0.rcgu.o): in function `<() as core::fmt::Debug>::fmt':
          /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/core/src/fmt/mod.rs:2606:(.text._ZN42_$LT$$RF$T$u20$as$u20$core..fmt..Debug$GT$3fmt17h3c973e93015fb545E+0x6): relocation truncated to fit: R_X86_64_PC32 against `.rodata..Lanon.76d0013ef10d81820c68bd9553577be5.7'
          /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/core/src/fmt/mod.rs:2606:(.text._ZN42_$LT$$RF$T$u20$as$u20$core..fmt..Debug$GT$3fmt17h3c973e93015fb545E+0x11): relocation truncated to fit: R_X86_64_GOTPCREL against symbol `core::fmt::Formatter::pad' defined in .text._ZN4core3fmt9Formatter3pad17h7b2f3f7c2fcfbcefE section in /usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-9fb4851878f34383.rlib(core-9fb4851878f34383.core.6eddfc0e-cgu.0.rcgu.o)
          /usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-8e20380b4b9fd6be.rlib(std-8e20380b4b9fd6be.std.680b7a42-cgu.0.rcgu.o): in function `<&T as core::fmt::Debug>::fmt':
          /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/core/src/fmt/mod.rs:2372:(.text._ZN42_$LT$$RF$T$u20$as$u20$core..fmt..Debug$GT$3fmt17hb0d06b63b8dda733E+0xf): relocation truncated to fit: R_X86_64_GOTPCREL against symbol `<str as core::fmt::Debug>::fmt' defined in .text._ZN40_$LT$str$u20$as$u20$core..fmt..Debug$GT$3fmt17h5f9a6196fe3d41bbE section in /usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-9fb4851878f34383.rlib(core-9fb4851878f34383.core.6eddfc0e-cgu.0.rcgu.o)
          /usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-8e20380b4b9fd6be.rlib(std-8e20380b4b9fd6be.std.680b7a42-cgu.0.rcgu.o): in function `<&T as core::fmt::Debug>::fmt':
          /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/core/src/fmt/mod.rs:2372:(.text._ZN42_$LT$$RF$T$u20$as$u20$core..fmt..Debug$GT$3fmt17hc3866b0e36e0abcfE+0x8): relocation truncated to fit: R_X86_64_GOTPCREL against symbol `<alloc::ffi::c_str::NulError as core::fmt::Debug>::fmt' defined in .text._ZN64_$LT$alloc..ffi..c_str..NulError$u20$as$u20$core..fmt..Debug$GT$3fmt17h5c863d7b47b35cf3E section in /usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-2bcc936797a96b7b.rlib(alloc-2bcc936797a96b7b.alloc.ff465fb3-cgu.0.rcgu.o)
          /usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-8e20380b4b9fd6be.rlib(std-8e20380b4b9fd6be.std.680b7a42-cgu.0.rcgu.o): in function `core::fmt::num::<impl core::fmt::Debug for i32>::fmt':
          /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/core/src/fmt/num.rs:186:(.text._ZN42_$LT$$RF$T$u20$as$u20$core..fmt..Debug$GT$3fmt17hc45608b2b33b1cdeE+0xf): relocation truncated to fit: R_X86_64_GOTPCREL against symbol `core::fmt::Formatter::debug_lower_hex' defined in .text._ZN4core3fmt9Formatter15debug_lower_hex17h150383ba3e818144E section in /usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-9fb4851878f34383.rlib(core-9fb4851878f34383.core.6eddfc0e-cgu.0.rcgu.o)
          /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/core/src/fmt/num.rs:187:(.text._ZN42_$LT$$RF$T$u20$as$u20$core..fmt..Debug$GT$3fmt17hc45608b2b33b1cdeE+0x26): relocation truncated to fit: R_X86_64_GOTPCREL against symbol `core::fmt::num::<impl core::fmt::LowerHex for i32>::fmt' defined in .text._ZN4core3fmt3num53_$LT$impl$u20$core..fmt..LowerHex$u20$for$u20$i32$GT$3fmt17h0f65dc7553fe3768E section in /usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-9fb4851878f34383.rlib(core-9fb4851878f34383.core.6eddfc0e-cgu.0.rcgu.o)
          /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/core/src/fmt/num.rs:188:(.text._ZN42_$LT$$RF$T$u20$as$u20$core..fmt..Debug$GT$3fmt17hc45608b2b33b1cdeE+0x2f): additional relocation overflows omitted from the output
          /usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-8e20380b4b9fd6be.rlib(std-8e20380b4b9fd6be.std.680b7a42-cgu.0.rcgu.o): in function `core::sync::atomic::atomic_load':
          /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/core/src/sync/atomic.rs:2987:(.text._ZN4core3ptr65drop_in_place$LT$std..sync..mutex..MutexGuard$LT$$LP$$RP$$GT$$GT$17h49bf14e00eebc588E+0xc): failed to convert GOTPCREL relocation against '_ZN3std9panicking11panic_count18GLOBAL_PANIC_COUNT17h28c96439d8f79da8E'; relink with --no-relax
          collect2: error: ld returned 1 exit status


error: could not compile `foo` due to previous error
```

</details>

<details><summary>clang/mold error message:</summary>

```
   Compiling foo v0.1.0 (/home/msrd0/foo)
error: linking with `/usr/bin/clang` failed: exit status: 1
  |
  = note: "/usr/bin/clang" "-m64" "/tmp/rustcwsfhuO/symbols.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.11qzgizkeveaglq7.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.14w8hd0h2ijbkz7.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.16fv1eqlyjkq4zc7.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.1b2yiixjznziz9u9.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.21dhuhdnz1ux056p.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.258ucpumfqphcw2s.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.2g2dfz0v6mrxvl69.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.2ugn6ecjb3k061ti.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.2z3jeagapreyxcl.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.3bel81cwg5s05x6.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.3etxvvkl1fwp2cvk.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.41r16b8d0x92nnle.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.42ui0su1wcedhkit.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.4esz2e4sghaebwy9.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.4ii608bc1oot1vxo.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.4sbozhfrwxgfzwd6.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.4yn80cvsjwkd94yv.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.5wo5zdzd3vfvsak.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.8vndcpbsd4lqnmy.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.m9zb96hgncput9z.rcgu.o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.28m6ql6y38gj855u.rcgu.o" "-Wl,--as-needed" "-L" "/home/msrd0/foo/target/debug/deps" "-L" "/usr/lib64/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-8e20380b4b9fd6be.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-a06485200ce26f7a.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-2dd0454343e4f4fc.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libmemchr-d0ea83c0a7b3e9a0.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-04397e76f19401d3.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-db7410605ab6dc00.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-fe4d24a59cfc3703.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-51d7bdeefbe4cf60.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-a2986b1c83374eb4.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-d021298679525c17.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-07477ce913344fbe.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-bfcfd0b3e74b40cd.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-a99d4acd8904850e.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-95ea7685e5bfdc81.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-5dd797956cdd0919.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-2bcc936797a96b7b.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-0d5f5dfbd9c7aca5.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-9fb4851878f34383.rlib" "/usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-0fa80fcac3958bad.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "/usr/lib64/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro,-znow" "-nodefaultlibs" "-fuse-ld=mold"
  = note: mold: error: /home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.11qzgizkeveaglq7.rcgu.o:(.text._ZN3foo4main17h8e740826d4aea723E): relocation R_X86_64_PC32 against .rodata..L__unnamed_1 out of range: -2147506555 is not in [-2147483648, 2147483648)
          mold: error: /home/msrd0/foo/target/debug/deps/foo-93a7457f5a7d66c8.11qzgizkeveaglq7.rcgu.o:(.text._ZN3foo4main17h8e740826d4aea723E): relocation R_X86_64_PC32 against .rodata..L__unnamed_3 out of range: -2147506581 is not in [-2147483648, 2147483648)
          clang-15: error: linker command failed with exit code 1 (use -v to see invocation)


error: could not compile `foo` due to previous error

```

</details>

### 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.67.1 (d5a82bbd2 2023-02-07) (Arch Linux rust 1:1.67.1-1)
binary: rustc
commit-hash: d5a82bbd26e1ad8b7401f6a718a9c57c96905483
commit-date: 2023-02-07
host: x86_64-unknown-linux-gnu
release: 1.67.1
LLVM version: 15.0.7
```