Skip to content

Use Cargo's links = "name" feature in singleton crates #467

Open
@jonas-schievink

Description

@jonas-schievink

We have several crates that only support linking a single version of them into a crate graph. These crates include:

  • All -rt crates, so cortex-m-rt, riscv-rt, msp430-rt, ...
    • These crates need to ensure this since they define the memory layout of the binary, and doing that multiple times would at best result in very confusing errors, and at worst produce a binary that doesn't work right.
  • All peripheral access crates like cortex-m and all svd2rust-generated PACs
    • These crates need to ensure this, as otherwise you could depend on 2 PACs and .take() their Peripherals twice.

Currently, these crates define a #[no_mangle] symbol to ensure that only a single version of them is linked to by an application, causing a linker error when more than one version is present.

Another way to do this is to use Cargo's links = "<name>" feature, which was designed to support precisely this use case (only for linking to C libraries that have global state). I think we should switch every crate to use the Cargo feature, since it was designed for this, gives a better error message, and cannot fail when no methods from one of the versions are called (a shortcoming of the current approach).

To do this, we need to migrate slowly, since removing the clashing symbol removes the protection with older versions of the library. That means, we start by adding a links key to all affected crates, and only after a couple of releases we remove the clashing symbol.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions