Repository: rust
Issue: STATUS_HEAP_CORRUPTION in rust-lld with a custom target on x86_64-pc-windows-gnu
Labels: A-linkage,A-LLVM,O-windows-gnu,C-bug

The following example has recently started causing rust-lld to crash on x86_64-pc-windows-gnu:

```rust
#![no_main]
#![allow(internal_features)]
#![feature(no_core)]
#![feature(lang_items)]
#![no_core]

#[lang = "sized"]
pub trait Sized {
    // Empty.
}
#[lang = "copy"]
pub trait Copy {
    // Empty.
}
```

with a custom JSON target:

```javascript
{
    "llvm-target": "x86_64-unknown-none-gnu",
    "data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
    "arch": "x86_64",
    "target-endian": "little",
    "target-pointer-width": "64",
    "target-c-int-width": "32",
    "os": "none",
    "linker-flavor": "ld.lld",
    "linker": "rust-lld",
    "executables": true
}
```

Building with:

```
rustc --target custom-target.json main.rs
```

This causes rust-lld to fail about 50% of the time, with errors like:


```
error: linking with `rust-lld` failed: exit code: 0xc0000374
  |
  = note: "rust-lld" "-flavor" "gnu" "main.main.4fa2520d328b6ba3-cgu.0.rcgu.o" "--as-needed" "-L" "C:\\Users\\eric\\.rustup\\toolchains\\nightly-2023-09-01-x86_64-pc-windows-gnu\\lib\\rustlib\\custom-target\\lib" "-Bdynamic" "--eh-frame-hdr" "-z" "noexecstack" "-L" "C:\\Users\\eric\\.rustup\\toolchains\\nightly-2023-09-01-x86_64-pc-windows-gnu\\lib\\rustlib\\custom-target\\lib" "-o" "main" "--gc-sections"
  = note: rust-lld: warning: cannot find entry symbol _start; not setting start address

error: aborting due to previous error
```

or 

```
error: linking with `rust-lld` failed: exit code: 0xc0000005
  |
  = note: "rust-lld" "-flavor" "gnu" "main.main.4fa2520d328b6ba3-cgu.0.rcgu.o" "--as-needed" "-L" "C:\\Users\\eric\\.rustup\\toolchains\\nightly-2023-09-01-x86_64-pc-windows-gnu\\lib\\rustlib\\custom-target\\lib" "-Bdynamic" "--eh-frame-hdr" "-z" "noexecstack" "-L" "C:\\Users\\eric\\.rustup\\toolchains\\nightly-2023-09-01-x86_64-pc-windows-gnu\\lib\\rustlib\\custom-target\\lib" "-o" "main" "--gc-sections"
  = note: rust-lld: warning: cannot find entry symbol _start; not setting start address
          PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
          Stack dump:
          0.    Program arguments: rust-lld -flavor gnu main.main.4fa2520d328b6ba3-cgu.0.rcgu.o --as-needed -L C:\\Users\\eric\\.rustup\\toolchains\\nightly-2023-09-01-x86_64-pc-windows-gnu\\lib\\rustlib\\custom-target\\lib -Bdynamic --eh-frame-hdr -z noexecstack -L C:\\Users\\eric\\.rustup\\toolchains\\nightly-2023-09-01-x86_64-pc-windows-gnu\\lib\\rustlib\\custom-target\\lib -o main --gc-sections
          Exception Code: 0xC0000005
          0x00007FFDD755633C, C:\WINDOWS\SYSTEM32\ntdll.dll(0x00007FFDD7530000) + 0x2633C byte(s), RtlGetCurrentServiceSessionId() + 0x1AEC byte(s)
          0x00007FFDD7555B74, C:\WINDOWS\SYSTEM32\ntdll.dll(0x00007FFDD7530000) + 0x25B74 byte(s), RtlGetCurrentServiceSessionId() + 0x1324 byte(s)
          0x00007FFDD75547B1, C:\WINDOWS\SYSTEM32\ntdll.dll(0x00007FFDD7530000) + 0x247B1 byte(s), RtlFreeHeap() + 0x51 byte(s)
          0x00007FFDD5609C9C, C:\WINDOWS\System32\msvcrt.dll(0x00007FFDD55F0000) + 0x19C9C byte(s), free() + 0x1C byte(s)
          0x00007FFDD5609366, C:\WINDOWS\System32\msvcrt.dll(0x00007FFDD55F0000) + 0x19366 byte(s), _aligned_free() + 0x16 byte(s)
          0x00007FF74451890A, C:\Users\eric\.rustup\toolchains\nightly-2023-09-01-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\bin\rust-lld.exe(0x00007FF741000000) + 0x351890A byte(s)
          0x00007FF742807BF9, C:\Users\eric\.rustup\toolchains\nightly-2023-09-01-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\bin\rust-lld.exe(0x00007FF741000000) + 0x1807BF9 byte(s)
          0x00007FF7426D9634, C:\Users\eric\.rustup\toolchains\nightly-2023-09-01-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\bin\rust-lld.exe(0x00007FF741000000) + 0x16D9634 byte(s)
          0x00007FF742256947, C:\Users\eric\.rustup\toolchains\nightly-2023-09-01-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\bin\rust-lld.exe(0x00007FF741000000) + 0x1256947 byte(s)
          0x00007FF74208C943, C:\Users\eric\.rustup\toolchains\nightly-2023-09-01-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\bin\rust-lld.exe(0x00007FF741000000) + 0x108C943 byte(s)
          0x00007FF74106032E, C:\Users\eric\.rustup\toolchains\nightly-2023-09-01-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\bin\rust-lld.exe(0x00007FF741000000) + 0x6032E byte(s)
          0x00007FF74457EB45, C:\Users\eric\.rustup\toolchains\nightly-2023-09-01-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\bin\rust-lld.exe(0x00007FF741000000) + 0x357EB45 byte(s)
          0x00007FF741001395, C:\Users\eric\.rustup\toolchains\nightly-2023-09-01-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\bin\rust-lld.exe(0x00007FF741000000) + 0x1395 byte(s)
          0x00007FF7410014E6, C:\Users\eric\.rustup\toolchains\nightly-2023-09-01-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\bin\rust-lld.exe(0x00007FF741000000) + 0x14E6 byte(s)
          0x00007FFDD6CF7614, C:\WINDOWS\System32\KERNEL32.DLL(0x00007FFDD6CE0000) + 0x17614 byte(s), BaseThreadInitThunk() + 0x14 byte(s)
          0x00007FFDD75826B1, C:\WINDOWS\SYSTEM32\ntdll.dll(0x00007FFDD7530000) + 0x526B1 byte(s), RtlUserThreadStart() + 0x21 byte(s)
```

This is part of cargo's testsuite, which is causing issues in our CI.

I have bisected the regression to #114048 (starting nightly-2023-08-09). cc @nikic 

It doesn't seem to be happening on any other platforms.

### Meta

`rustc --version --verbose`:
```
rustc 1.74.0-nightly (203c57dbe 2023-09-17)
binary: rustc
commit-hash: 203c57dbe20aee67eaa8f7be45d1e4ef0b274109
commit-date: 2023-09-17
host: x86_64-pc-windows-gnu
release: 1.74.0-nightly
LLVM version: 17.0.0
```