Repository: rust
Issue: Invalid `aarch64-unknown-none` target produced if the `CC_aarch64_unknown_none` env var is not set
Labels: A-cross,T-bootstrap,C-bug,O-AArch64

If you try to compile the core library for the `aarch64-unknown-none` target, and the `CC_aarch64_unknown_none` environment variable is not set, you will end up with a broken target that can't link any basic program:

```
error: linking with `rust-lld` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="<redacted>" VSLANG="1033" "rust-lld" "-flavor" "gnu" "/tmp/rustcPX9tnr/symbols.o" "binary.binary.ec7d7d9e5618561c-cgu.0.rcgu.o" "--as-needed" "-L" "/home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib" "-Bstatic" "/home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/librustc_std_workspace_core-3304236ec67a17db.rlib" "/home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcore-27e4e5b40a434022.rlib" "/home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib" "-Bdynamic" "--eh-frame-hdr" "-z" "noexecstack" "-L" "/home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib" "-o" "binary" "--gc-sections"
  = note: rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas1_relax.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas1_acq.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas1_rel.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas1_acq_rel.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas2_relax.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas2_acq.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas2_rel.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas2_acq_rel.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas4_relax.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas4_acq.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas4_rel.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas4_acq_rel.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas8_relax.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas8_acq.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas8_rel.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas8_acq_rel.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas16_relax.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas16_acq.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas16_rel.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: /home/pietro/tmp/sysroot/lib/rustlib/aarch64-unknown-none/lib/libcompiler_builtins-5d101b3353699a4d.rlib(lse_cas16_acq_rel.o) is incompatible with /tmp/rustcPX9tnr/symbols.o
          rust-lld: error: too many errors emitted, stopping now (use --error-limit=0 to see all errors)
```

Inspecting the `libcompiler_builtins-5d101b3353699a4d.rlib` file, we can see that the codegen units produced by rustc are indeed aarch64, but the object files produced by the compiler_builtins's build script are x86_64:

```
$ file compiler_builtins-5d101b3353699a4d.compiler_builtins.aeeb364441e31d74-cgu.0.rcgu.o 
compiler_builtins-5d101b3353699a4d.compiler_builtins.aeeb364441e31d74-cgu.0.rcgu.o: ELF 64-bit LSB relocatable, ARM aarch64, version 1 (SYSV), not stripped
$ file lse_cas1_relax.o
lse_cas1_relax.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), stripped
```

Having a "polyglot" `libcompiler_builtins.rlib` makes the linker sad, and results in the confusing linker error messages above. Setting the `CC_aarch64_unknown_none` environment variable pointing to a valid compiler for the target fixes the problem.

CI-built tarballs are not affected by this (as it sets the `CC_aarch64_unknown_none` environment variable), but right now this is a huge footgun for anyone trying to build the target locally.

We should add some safeguards to prevent this from happening. Ideally we would change the build script of `compiler_builtins` not to YOLO and use the host compiler if it doesn't find the target compiler. If that doesn't work, bootstrap should abort the build if the `c` feature of `compiler_builtins` is enabled and the corresponding `CC_*` environment variable isn't set.