Skip to content
This repository was archived by the owner on Jan 24, 2022. It is now read-only.
This repository was archived by the owner on Jan 24, 2022. It is now read-only.

[RFC] make sections .bss and .data opt in #32

Closed
@japaric

Description

@japaric

The zero_bss and init_data routines will run before main even if the
application makes use of neither or only one of these sections. This may not
matter much if your device has plenty of Flash memory / ROM but if you are
targeting really small devices with e.g. only 512 bytes of ROM then every byte
you can save counts.

The only safe way to opt out of these routines is to forbid the existence of the
.bss and / or .data sections. That can be easily done in this crate because
it controls the linker script of the application.

Design

We add two opt-in Cargo features: "bss" and "data". If neither is enabled then
static variables can't be used at all (unless they end up in .rodata) and
neither, zero_bss or init_data, will run before "main".

If only "bss" is enabled then zero_bss will run before main but only static
variables that end up in .bss are allowed in the program. For instance this
would compile:

static mut STATE: bool = false;

but this would not:

static mut STATE: bool = true;

Something similar would happen with the "data" feature.

Enabling both features gives you today's behavior.

Bonus

For the really brave we could allow this pattern when both "bss" and "data" are
disabled:

#[link_section = ".uninit"]
static mut STATE: bool = false;

STATE is uninitialized because neither zero_bss and init_data runs before
main. Reading this variable before assigning it any value at runtime will return
junk and could even cause undefined behavior, for example if the variable is an
enum.

Drawbacks

This is a breaking change and it's also kind of annoying for people that usually
use both .bss and .data because they'll now have to enable both features or
their program won't compile.

cc @cr1901 @therealprof

Metadata

Metadata

Assignees

No one assigned

    Labels

    RFCThis issue needs your input!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions