Repository: rust
Issue: Import order breaks compilation with undefined reference to `rust_begin_unwind`.
Labels: C-bug,S-needs-repro

<!--
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 (excerpt of a `no_std` binary crate's `main.rs`):

```rust
use freertos_rust::FreeRtosAllocator;
use panic_halt as _;

#[global_allocator]
static GLOBAL: FreeRtosAllocator = FreeRtosAllocator;
```

I expected to see this happen: 

Compilation to be successful.

Instead, this happened: 

Compilation failed with undefined reference to `rust_begin_unwind`.

Additional information:

This happened after switching from LLD to a GNU linker `arm-none-eabi-ld`.

As the title of the pull request suggests, the import order and type matters:

I found that moving the `freertos_rust` import after the `panic_halt` fixes the compilation, i.e.:

```rust
use panic_halt as _;
use freertos_rust::FreeRtosAllocator;

#[global_allocator]
static GLOBAL: FreeRtosAllocator = FreeRtosAllocator;
```

I also found that changing the `panic_halt` import to `extern crate panic_halt` fixes compilation, i.e. 

```rust
use freertos_rust::FreeRtosAllocator;
extern crate panic_halt;

#[global_allocator]
static GLOBAL: FreeRtosAllocator = FreeRtosAllocator;
```

### 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.61.0-nightly (0677edc86 2022-03-31)
binary: rustc
commit-hash: 0677edc86e342f333d4828b0ee1ef395a4e70fe5
commit-date: 2022-03-31
host: x86_64-apple-darwin
release: 1.61.0-nightly
LLVM version: 14.0.0
```

<!--
Include a backtrace in the code block by setting `RUST_BACKTRACE=1` in your
environment. E.g. `RUST_BACKTRACE=1 cargo build`.
-->
<details><summary>Backtrace</summary>
<p>

```
   Compiling cortex-m-quickstart v0.1.0 (${HOME}/Repos/cortex-m-quickstart)
error: linking with `arm-none-eabi-ld` failed: exit status: 1
  |
  = note: "arm-none-eabi-ld" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/cortex_m_quickstart-247e0e61977b1202.13szx06iad2gp43b.rcgu.o" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/cortex_m_quickstart-247e0e61977b1202.3nke8ncxu5ll24hb.rcgu.o" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/cortex_m_quickstart-247e0e61977b1202.4vbaf45z3drful2p.rcgu.o" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/cortex_m_quickstart-247e0e61977b1202.ctr8sio8s0tj90j.rcgu.o" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/cortex_m_quickstart-247e0e61977b1202.3cv7nchiggoab8b0.rcgu.o" "--as-needed" "-L" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps" "-L" "${HOME}/Repos/cortex-m-quickstart/target/debug/deps" "-L" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/build/cortex-m-quickstart-9138f89003d7324d/out" "-L" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/build/cortex-m-quickstart-9138f89003d7324d/out" "-L" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/build/cortex-m-98c5d38ec100d940/out" "-L" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/build/cortex-m-rt-d1f2d9903a07f144/out" "-L" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/build/stm32f4-e13a2e788a0ac9ed/out" "-L" "${HOME}/.config/rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/thumbv7em-none-eabihf/lib" "-Bstatic" "-lfreertos" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/libpanic_halt-014fffc5e9a0b89a.rlib" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/libfreertos_rust-008b4b372f7db2f8.rlib" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/liballoc-c9c427728f324bc2.rlib" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/libstm32f4xx_hal-6e6e918dc0e63f4c.rlib" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/libfugit_timer-de781b10f82aafc5.rlib" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/libbitflags-abeba381ea6e78c0.rlib" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/libtime-964ed4e2cbd1ae3b.rlib" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/libembedded_storage-4ec653b2cba68cb6.rlib" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/libembedded_dma-769c57ba16ce7ceb.rlib" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/libstable_deref_trait-c244db15baafc1e2.rlib" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/libfugit-908b99816a1b314d.rlib" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/libgcd-cf907cfea85545a7.rlib" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/libembedded_hal-edafe61436d40ca5.rlib" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/libstm32f4-2ad0f6735608353e.rlib" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/libcortex_m-955a650231f80a07.rlib" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/libvolatile_register-defcfb2b931328f5.rlib" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/libvcell-48994f19c9c20ae3.rlib" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/libbare_metal-0ee91f29f47b21f1.rlib" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/libembedded_hal-3ce29f8aa4ff80c6.rlib" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/libvoid-e2cf52f9ae4cc8c4.rlib" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/libnb-475ccb394689ef83.rlib" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/libnb-2004031aa15456e0.rlib" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/libcortex_m_rt-0b4ba077ef625bae.rlib" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/librustc_std_workspace_core-a439db3c1068b07a.rlib" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/libcore-8e05f43722693083.rlib" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/libcompiler_builtins-34317003d7c23af4.rlib" "-Bdynamic" "--eh-frame-hdr" "-znoexecstack" "-L" "${HOME}/.config/rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/thumbv7em-none-eabihf/lib" "-o" "${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/cortex_m_quickstart-247e0e61977b1202" "--gc-sections" "--nmagic" "-Tlink.x"
  = note: arm-none-eabi-ld: ${HOME}/Repos/cortex-m-quickstart/target/thumbv7em-none-eabihf/debug/deps/libcore-8e05f43722693083.rlib(core-8e05f43722693083.core.a7996396-cgu.2.rcgu.o): in function `core::panicking::panic_fmt':
          ${HOME}/.config/rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/panicking.rs:143: undefined reference to `rust_begin_unwind'
          
  = help: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

error: could not compile `cortex-m-quickstart` due to previous error
```

</p>
</details>