knurling-rs / flip-link

Adds zero-cost stack overflow protection to your embedded programs
Apache License 2.0
270 stars 6 forks source link

flip-link error #73

Closed adminSxs closed 1 year ago

adminSxs commented 2 years ago

windows os: 11 I use https://github.com/knurling-rs/app-template generate project add

cortex-m-rtic = { version = "1.1.3" }
error: linking with `flip-link` failed: exit code: 1
  |
  = note: "flip-link" "-flavor" "gnu" "C:\\Users\\zhaobo\\AppData\\Local\\Temp\\rustck3K05Z\\symbols.o" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\main-11049d013bb1904c.main.2fc7ea03-cgu.0.rcgu.o" "--as-needed" "-L" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps" "-L" "D:\\projects\\embeed\\app\\target\\debug\\deps" "-L" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\build\\cortex-m-5817308009678109\\out" "-L" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\build\\cortex-m-rt-4199a1caeb388d95\\out" "-L" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\build\\defmt-d86b0063a8a3b56a\\out" "-L" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\build\\stm32f1-14b8b2830e44275e\\out" "-L" "D:\\software\\rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\thumbv7m-none-eabi\\lib" "-Bstatic" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\libstm32f1xx_hal-8051c863c3ad98b5.rlib" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\libfugit_timer-fe7d59127748e96a.rlib" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\libbxcan-e9e7ad35b16a23e4.rlib" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\libstm32_usbd-ed785f6b86adbff3.rlib" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\libusb_device-a233a87681d50602.rlib" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\libfugit-6c4bc25732b1db27.rlib" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\libgcd-4197544a75179d12.rlib" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\libembedded_dma-d8fa02c1119216ed.rlib" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\libstm32f1-b31ddd66260981cf.rlib" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\libcortex_m_rt-c1a6a9ab4f0cfe1e.rlib" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\librtic-34882a4391d40282.rlib" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\libheapless-3a2e6cf7174401b1.rlib" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\libstable_deref_trait-b94e8061351ccd46.rlib" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\libhash32-2ef9212d208b60d7.rlib" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\libbyteorder-90345a976a4d83d8.rlib" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\libbare_metal-7b4e9559ea5371a8.rlib" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\librtic_monotonic-8232f0df63c185a6.rlib" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\librtic_core-fbd27b968ff823cf.rlib" "--start-group" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\libpanic_probe-a9b08e11c9fb609a.rlib" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\libdefmt-d4cf8a2ca51cf9ce.rlib" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\libbitflags-5351597746fc1950.rlib" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\libcortex_m-f44fc0a28245896b.rlib" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\libembedded_hal-0f940bf2e1d93e1f.rlib" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\libvoid-b7e1daec3a29df70.rlib" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\libnb-9a0f1077e9087fcc.rlib" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\libnb-89e12803df9a6252.rlib" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\libvolatile_register-1ef2fbeaac4c48e3.rlib" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\libvcell-894f713cc598d2e9.rlib" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\libbare_metal-392c59e661be3df0.rlib" "D:\\software\\rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\thumbv7m-none-eabi\\lib\\librustc_std_workspace_core-d708ca894357bb88.rlib" "D:\\software\\rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\thumbv7m-none-eabi\\lib\\libcore-7279b5ba9f24caff.rlib" "--end-group" "D:\\software\\rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\thumbv7m-none-eabi\\lib\\libcompiler_builtins-25380e8375a00138.rlib" "-Bdynamic" "--eh-frame-hdr" "-znoexecstack" "-L" "D:\\software\\rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\thumbv7m-none-eabi\\lib" "-o" "D:\\projects\\embeed\\app\\target\\thumbv7m-none-eabi\\debug\\deps\\main-11049d013bb1904c" "--gc-sections" "-O1" "-Tlink.x" "-Tdefmt.x" "--nmagic"
  = note: rust-lld: error: undefined symbol: _defmt_acquire
          >>> referenced by mod.rs:55 (D:\software\cargo\registry\src\rsproxy.cn-8f6827c7555bfaf8\defmt-0.3.2\src\export\mod.rs:55)
          >>>               panic_probe-a9b08e11c9fb609a.panic_probe.02df55b0-cgu.0.rcgu.o:(rust_begin_unwind) in archive D:\projects\embeed\app\target\thumbv7m-none-eabi\debug\deps\libpanic_probe-a9b08e11c9fb609a.rlib

          rust-lld: error: undefined symbol: _defmt_release
          >>> referenced by mod.rs:71 (D:\software\cargo\registry\src\rsproxy.cn-8f6827c7555bfaf8\defmt-0.3.2\src\export\mod.rs:71)
          >>>               panic_probe-a9b08e11c9fb609a.panic_probe.02df55b0-cgu.0.rcgu.o:(rust_begin_unwind) in archive D:\projects\embeed\app\target\thumbv7m-none-eabi\debug\deps\libpanic_probe-a9b08e11c9fb609a.rlib

          rust-lld: error: undefined symbol: _defmt_write
          >>> referenced by mod.rs:85 (src\export\mod.rs:85)
          >>>               defmt-d4cf8a2ca51cf9ce.defmt.d14e61bb-cgu.0.rcgu.o:(_$LT$$RF$mut$u20$W$u20$as$u20$core..fmt..Write$GT$::write_char::hbf1e7cda46151703) in archive D:\projects\embeed\app\target\thumbv7m-none-eabi\debug\deps\libdefmt-d4cf8a2ca51cf9ce.rlib
          >>> referenced by mod.rs:85 (src\export\mod.rs:85)
          >>>               defmt-d4cf8a2ca51cf9ce.defmt.d14e61bb-cgu.0.rcgu.o:(_$LT$$RF$mut$u20$W$u20$as$u20$core..fmt..Write$GT$::write_str::h9be493d4997977cf) in archive D:\projects\embeed\app\target\thumbv7m-none-eabi\debug\deps\libdefmt-d4cf8a2ca51cf9ce.rlib
          >>> referenced by mod.rs:85 (src\export\mod.rs:85)
          >>>               defmt-d4cf8a2ca51cf9ce.defmt.d14e61bb-cgu.0.rcgu.o:(defmt::export::istr::hf51bfc040acfc767) in archive D:\projects\embeed\app\target\thumbv7m-none-eabi\debug\deps\libdefmt-d4cf8a2ca51cf9ce.rlib
          >>> referenced 2 more times

          flip-link: the native linker failed to link the program normally; please check your project configuration and linker scripts

warning: `app` (bin "main") generated 3 warnings
error: could not compile `app` due to previous error; 3 warnings emitted
Urhengulas commented 2 years ago

I am not able to reproduce the error. I added cortex-m-rtic to a the app-template, but the bins still compiles just fine for me.

The error says error: undefined symbol: _defmt_release and the same for _defmt_release and _defmt_write. Do you have defmt-rtt in your list of dependencies? (It should be there by default).

What happens if you remove "-C", "linker=flip-link", from the rustflags in .cargo/config.toml? This will skip flip-link and just invoke the native linker.

rustflags = [
-  "-C", "linker=flip-link",
  "-C", "link-arg=-Tlink.x",
  "-C", "link-arg=-Tdefmt.x",
  # This is needed if your flash or ram addresses are not aligned to 0x10000 in memory.x
  # See https://github.com/rust-embedded/cortex-m-quickstart/pull/95
  "-C", "link-arg=--nmagic",
]
Ragarnoy commented 1 year ago

I'm running into the same issue, however when I remove the line in config.toml I get this:

error: linking with `rust-lld` failed: exit code: 1
  |
  = note: "rust-lld" "-flavor" "gnu" "C:\\Users\\Ragarnoy\\AppData\\Local\\Temp\\rustc5b0jk9\\symbols.o" "C:\\Users\\Ragarnoy\\embedded-trainings-2020\\beginner\\apps\\target\\thumbv7em-none-eabihf\\debug\\deps\\hello-8229623d76740f1f.hello.f932f36f-cgu.0.rcgu.o" "--as-needed" "-L" "C:\\Users\\Ragarnoy\\embedded-trainings-2020\\beginner\\apps\\target\\thumbv7em-none-eabihf\\debug\\deps" "-L" "C:\\Users\\Ragarnoy\\embedded-trainings-2020\\beginner\\apps\\target\\debug\\deps" "-L" "C:\\Users\\Ragarnoy\\embedded-trainings-2020\\beginner\\apps\\target\\thumbv7em-none-eabihf\\debug\\build\\cortex-m-c452bec137310b55\\out" "-L" "C:\\Users\\Ragarnoy\\embedded-trainings-2020\\beginner\\apps\\target\\thumbv7em-none-eabihf\\debug\\build\\cortex-m-rt-be4f982373b21b56\\out" "-L" "C:\\Users\\Ragarnoy\\embedded-trainings-2020\\beginner\\apps\\target\\thumbv7em-none-eabihf\\debug\\build\\defmt-7fb01f6acebd88d4\\out" "-L" "C:\\Users\\Ragarnoy\\embedded-trainings-2020\\beginner\\apps\\target\\thumbv7em-none-eabihf\\debug\\build\\dk-54fd7126418f8b29\\out" "-L" "C:\\Users\\Ragarnoy\\embedded-trainings-2020\\beginner\\apps\\target\\thumbv7em-none-eabihf\\debug\\build\\nrf52840-hal-19cebceb665df9d9\\out" "-L" "C:\\Users\\Ragarnoy\\embedded-trainings-2020\\beginner\\apps\\target\\thumbv7em-none-eabihf\\debug\\build\\nrf52840-pac-e7d0e23c12afa921\\out" "-L" "C:\\Users\\Ragarnoy\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\thumbv7em-none-eabihf\\lib" "-Bstatic" "C:\\Users\\Ragarnoy\\AppData\\Local\\Temp\\rustc5b0jk9\\libcortex_m-3b1a265280aea223.rlib" "C:\\Users\\Ragarnoy\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\thumbv7em-none-eabihf\\lib\\libcompiler_builtins-1f7e7d1541f43b6f.rlib" "-Bdynamic" "--eh-frame-hdr" "-znoexecstack" "-L" "C:\\Users\\Ragarnoy\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\thumbv7em-none-eabihf\\lib" "-o" "C:\\Users\\Ragarnoy\\embedded-trainings-2020\\beginner\\apps\\target\\thumbv7em-none-eabihf\\debug\\deps\\hello-8229623d76740f1f" "--gc-sections" "-Tlink.x" "-Tdefmt.x"
  = note: rust-lld: warning: section type mismatch for .uninit.defmt-rtt.BUFFER
          >>> C:\Users\Ragarnoy\embedded-trainings-2020\beginner\apps\target\thumbv7em-none-eabihf\debug\deps\hello-8229623d76740f1f.hello.f932f36f-cgu.0.rcgu.o:(.uninit.defmt-rtt.BUFFER): SHT_PROGBITS
          >>> output section .uninit: SHT_NOBITS

          rust-lld: warning: section type mismatch for .got
          >>> <internal>:(.got): SHT_PROGBITS
          >>> output section .got: SHT_NOBITS

          rust-lld: warning: section type mismatch for .got.plt
          >>> <internal>:(.got.plt): SHT_PROGBITS
          >>> output section .got: SHT_NOBITS

          rust-lld: warning: section type mismatch for .got
          >>> <internal>:(.got): SHT_PROGBITS
          >>> output section .got: SHT_NOBITS

          rust-lld: error: undefined symbol: _critical_section_1_0_acquire
          >>>               C:\Users\Ragarnoy\embedded-trainings-2020\beginner\apps\target\thumbv7em-none-eabihf\debug\deps\hello-8229623d76740f1f.hello.f932f36f-cgu.0.rcgu.o:(_defmt_acquire)

          rust-lld: error: undefined symbol: _critical_section_1_0_release
          >>> referenced by lib.rs:45 (C:\Users\Ragarnoy\.cargo\registry\src\github.com-1ecc6299db9ec823\critical-section-0.2.8\src\lib.rs:45)
          >>>               C:\Users\Ragarnoy\embedded-trainings-2020\beginner\apps\target\thumbv7em-none-eabihf\debug\deps\hello-8229623d76740f1f.hello.f932f36f-cgu.0.rcgu.o:(_defmt_release)

error: could not compile `apps` due to previous error
jonathanpallant commented 1 year ago

That's caused by an update to critical-section and isn't a flip-link bug (although perhaps we need to bump some dependencies in our example project). The update resolves an unsoundness bug, but requires you to opt-in to a trait implementation from somewhere (or write your own) - if you don't, you are missing those two symbols.

See https://docs.rs/critical-section/latest/critical_section/#usage-in-no-std-binaries.

Ragarnoy commented 1 year ago

That's caused by an update to critical-section and isn't a flip-link bug (although perhaps we need to bump some dependencies in our example project). The update resolves an unsoundness bug, but requires you to opt-in to a trait implementation from somewhere (or write your own) - if you don't, you are missing those two symbols.

See https://docs.rs/critical-section/latest/critical_section/#usage-in-no-std-binaries.

tyvm!

Urhengulas commented 1 year ago

Please see our blog post on the issue for how to fix it and how it happened: https://ferrous-systems.com/blog/defmt-rtt-linker-error/