Blog coding article

defmt-rtt linker error

Johann
Article

defmt-rtt linker error

Published on 3 min read
Knurling icon
Knurling
A tool set to develop embedded applications faster.
❤️ Sponsor

    TL;DR

    Compiling a Rust project with defmt-rtt v0.3 fails with a linker error if an application author (this is you!) does not select a critical section implementation.

    To fix this do the following (taken from the critical-section docs):

    Add a dependency on a crate providing a critical section implementation. Enable the critical-section-* Cargo feature if required by the crate.

    Implementations are typically provided by either architecture-support crates (cortex-m, riscv, etc), or HAL crates.

    For example, for single-core Cortex-M targets, you can use:

    [dependencies]
    cortex-m = { version = "0.7.6", features = ["critical-section-single-core"]}
    

    Additionally you should update from defmt-rtt v0.3 to defmt-rtt v0.4:

    [dependencies]
    defmt-rtt = "0.4"
    

    See the knurling-rs/app-template as an example how the final list of dependencies may look like. See the critical-section documentation for more details on critical-section.

    Why did this happen?

    On 29.11.2022, our dependency critical-section released a patch version v0.2.8, which breaks compilation. It requires an application author to select a critical section implementation manually. See above how to do that.

    While this breaks the semver guarantees, which promise that a patch version should not break compilation, this case is a bit special. It turned out that critical-section had an unsoundness bug up to version v0.2.7, which was fixed in version v1.0.0. To push the ecosystem to stop using the unsound version, they came up with a plan consisting of two steps. Step one is to publish version v0.2.8, which internally uses version v1.1.1. Step two is to yank the unsound versions.

    Understanding the error

    The error message you get when you hit this bug is pretty long and looks pretty scary. You can scroll over it, below we will explain what it (tries to) tell you.

    $ cargo build --bin hello
       Compiling krate v0.1.0 (/home/urhengulas/Documents/github.com/knurling-rs/krate)
    error: linking with `flip-link` failed: exit status: 1
      |
      = note: "flip-link" "-flavor" "gnu" "/tmp/rustcF1Rfjq/symbols.o" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/hello-96594b1e3d051856.hello.0cea9aae-cgu.0.rcgu.o" "--as-needed" "-L" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps" "-L" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/debug/deps" "-L" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/build/cortex-m-5a8a3e5efd3f5d38/out" "-L" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/build/cortex-m-rt-e6802dc0ad56dc15/out" "-L" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/build/defmt-7b3da69143a3fac1/out" "-L" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/build/nrf52840-hal-ae6ccd2495f5889b/out" "-L" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/build/nrf52840-pac-9f2223e6db3a89fb/out" "-L" "/home/urhengulas/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/thumbv7em-none-eabihf/lib" "-Bstatic" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libkrate-632bf0d5d85b05ec.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libpanic_probe-bbf27cb19b769f0b.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libnrf52840_hal-0f08953c5a7d6ac4.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libnrf_hal_common-d29db42856341194.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libnrf_usbd-32dafb8dd9b8a23a.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libusb_device-2e0163d30d4ccb12.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libfixed-4972feb924614e14.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libtypenum-0b8c24fb75b0c009.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libbytemuck-7e6eea92c66259ec.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libhalf-79af8e34ec0bd222.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libaz-9c67ac6ff0bb89dc.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/librand_core-004168d1f2ffedf7.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libcfg_if-8f12ed11b8f02f37.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libembedded_storage-84da9091ff941886.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libembedded_dma-9fc8147d33a51156.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libstable_deref_trait-a5791fe8e36b0422.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libcast-3172d1b238b655f4.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libnrf52840_pac-a28a1bf3b2f474ca.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libcortex_m_rt-5dea9281ad1b0dd3.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libcortex_m-d061ffb1de059027.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libvolatile_register-3a728ae2a87918b8.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libvcell-cfa26c19c038e9fa.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libbare_metal-ff78ee2f5eb37d65.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libembedded_hal-5e1488e628b72261.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libvoid-0e3f58d3ce897e05.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libnb-5b75d58cbaa0844a.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libnb-cfcd1ce941857ce1.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libdefmt_rtt-8f91bdadd9f22437.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libcritical_section-9515ac48372a7ebf.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libcritical_section-31aa59cd3086ef05.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libbare_metal-21044f4c1d6fa4c3.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libdefmt-5c2d88b647047019.rlib" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libbitflags-f5043d0c96fb2c0f.rlib" "/home/urhengulas/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/thumbv7em-none-eabihf/lib/librustc_std_workspace_core-8f64384142cbfee6.rlib" "/home/urhengulas/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/thumbv7em-none-eabihf/lib/libcore-3ad9929a938dd9d2.rlib" "/home/urhengulas/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/thumbv7em-none-eabihf/lib/libcompiler_builtins-1f7e7d1541f43b6f.rlib" "-Bdynamic" "--eh-frame-hdr" "-znoexecstack" "-L" "/home/urhengulas/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/thumbv7em-none-eabihf/lib" "-o" "/home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/hello-96594b1e3d051856" "--gc-sections" "-Tlink.x" "-Tdefmt.x" "--nmagic"
      = note: rust-lld: warning: section type mismatch for .uninit.defmt-rtt.BUFFER
              >>> /home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libdefmt_rtt-8f91bdadd9f22437.rlib(defmt_rtt-8f91bdadd9f22437.defmt_rtt.cf21a68e-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
              >>> referenced by lib.rs:29 (/home/urhengulas/.cargo/registry/src/github.com-1ecc6299db9ec823/critical-section-0.2.8/src/lib.rs:29)
              >>>               defmt_rtt-8f91bdadd9f22437.defmt_rtt.cf21a68e-cgu.0.rcgu.o:(_$LT$defmt_rtt..Logger$u20$as$u20$defmt..traits..Logger$GT$::acquire::h9d3858e61ffb8d8d) in archive /home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libdefmt_rtt-8f91bdadd9f22437.rlib
              
              rust-lld: error: undefined symbol: _critical_section_1_0_release
              >>> referenced by lib.rs:45 (/home/urhengulas/.cargo/registry/src/github.com-1ecc6299db9ec823/critical-section-0.2.8/src/lib.rs:45)
              >>>               defmt_rtt-8f91bdadd9f22437.defmt_rtt.cf21a68e-cgu.0.rcgu.o:(_$LT$defmt_rtt..Logger$u20$as$u20$defmt..traits..Logger$GT$::release::h9526e652a9425b09) in archive /home/urhengulas/Documents/github.com/knurling-rs/krate/target/thumbv7em-none-eabihf/debug/deps/libdefmt_rtt-8f91bdadd9f22437.rlib
              
              flip-link: the native linker failed to link the program normally; please check your project configuration and linker scripts
              
    
    error: could not compile `krate` due to previous error
    

    The error tells us that "linking […] failed", which is the part of the build process that happens after each crate has been compiled into object code. Although the error message mentions flip-link, the error is related to the underlying linker that flip-link calls, as is stated by "the native linker failed to link the program normally".

    The important parts of the error are "undefined symbol: _critical_section_1_0_acquire" and "undefined symbol: _critical_section_1_0_release". critical-section works using #[no_mangle] functions, to avoid every user of a critical section (e.g. a driver) having to depend on your specific implementation (e.g. your HAL). Instead, the user (the driver) just has to depend on the critical-section crate, which assumes the existence of those two symbols, and elsewhere your dependency tree, something (typically your HAL) has to supply them.

    In version 0.2.7 of critical-section these symbols had a default implementation which just disabled interrupts, but this is not appropriate in many cases (e.g. you use the nRF Soft Device), and is unsound for multicore systems (e.g. the Raspberry Pi Silicon RP2040). Now another crate needs to implement these symbols for your system as there is no longer that unsound default implementation. The 'unsound' implementation still exists in the critical-section crate, but it's now behind a feature flag called critical-section-single-core.