Repository: rust
Issue: ICE in LlvmArchiveBuilder when cross-compiling openssl for Android ARM64
Labels: A-codegen,I-ICE,O-android,P-medium,T-compiler,C-bug,O-AArch64

There is an ICE in nightly that I can trigger by adding a dependency on vendored openssl when cross-compiling for aarch64 android with the NDK. It's possible the problem here isn't really rustc's fault, but it does crash.

Test repo: https://github.com/thombles/rust-ice-openssl
(Requires Android NDK - tested on Darwin host)

This test case is a hello world app with a dependency that requires it to build and link openssl from source:
```
[dependencies]
openssl = { version = "0.10", features = ["vendored"] }
```

I run the following script to build it:
```
#!/bin/bash
set -eu

# Set this to location of Android NDK
NDK=/Users/tk/Library/Android/sdk/ndk-bundle/
# (Tested on NDK 19.2.5345600, installed via Android Studio SDK Manager)

# Set this to your platform - probably "darwin-x86_64", "windows-x86_64", "linux-x86_64"
HOST_PLATFORM=darwin-x86_64

echo "Using Android NDK at $NDK for $HOST_PLATFORM"

export AR="$NDK/toolchains/llvm/prebuilt/${HOST_PLATFORM}/bin/aarch64-linux-android-ar"
export CC="$NDK/toolchains/llvm/prebuilt/${HOST_PLATFORM}/bin/aarch64-linux-android21-clang"
export CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER="${CC}"
export RUST_BACKTRACE=1
cargo build --target aarch64-linux-android
```

This results in the following log and backtrace.

```
$ ./bug.sh
Using Android NDK at /Users/tk/Library/Android/sdk/ndk-bundle/ for darwin-x86_64
   Compiling cc v1.0.36
   Compiling libc v0.2.54
   Compiling pkg-config v0.3.14
   Compiling autocfg v0.1.2
   Compiling foreign-types-shared v0.1.1
   Compiling openssl v0.10.21
   Compiling bitflags v1.0.4
   Compiling cfg-if v0.1.7
   Compiling lazy_static v1.3.0
   Compiling foreign-types v0.3.2
   Compiling openssl-src v111.2.1+1.1.1b
   Compiling openssl-sys v0.9.45
thread 'rustc' panicked at 'called `Option::unwrap()` on a `None` value', src/libcore/option.rs:347:21
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
   1: std::sys_common::backtrace::_print
   2: std::panicking::default_hook::{{closure}}
   3: std::panicking::default_hook
   4: rustc::util::common::panic_hook
   5: std::panicking::rust_panic_with_hook
   6: std::panicking::continue_panic_fmt
   7: rust_begin_unwind
   8: core::panicking::panic_fmt
   9: core::panicking::panic
  10: <rustc_codegen_llvm::back::archive::LlvmArchiveBuilder as rustc_codegen_ssa::back::archive::ArchiveBuilder>::build
  11: rustc_codegen_ssa::back::link::link_binary
  12: rustc::util::common::time
  13: <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_utils::codegen_backend::CodegenBackend>::join_codegen_and_link
  14: rustc_interface::queries::Query<T>::compute
  15: rustc_interface::queries::<impl rustc_interface::interface::Compiler>::link
  16: rustc_interface::interface::run_compiler_in_existing_thread_pool
  17: std::thread::local::LocalKey<T>::with
  18: scoped_tls::ScopedKey<T>::set
query stack during panic:
end of query stack

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.36.0-nightly (cfdc84a00 2019-05-07) running on x86_64-apple-darwin

note: compiler flags: -C debuginfo=2 -C linker=/Users/tk/Library/Android/sdk/ndk-bundle//toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android21-clang --crate-type lib

note: some of the compiler flags provided by cargo are hidden

error: Could not compile `openssl-sys`.

To learn more, run the command again with --verbose.
```

```
$ rustc --version --verbose
rustc 1.36.0-nightly (cfdc84a00 2019-05-07)
binary: rustc
commit-hash: cfdc84a009020c59e53e4039beae22eb59e41685
commit-date: 2019-05-07
host: x86_64-apple-darwin
release: 1.36.0-nightly
LLVM version: 8.0
```