Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
L

linktime

> 编程语言
Open source

Link-time initialization, destruction, scattered data collection macros for Rust

1.1K stars0 likes1 views
WebsiteGitHub

About

Link-time initialization, destruction, scattered data collection macros for Rust

linktime

Cross-platform libraries for link-time initialization, finalization and collection in Rust.

crate
linktime
Convenience crate for ctor, dtor and link-section
ctor
Module initialization functions before main
dtor
Module shutdown functions before main
link-section
Linker-managed typed (slices) and untyped sections
scattered-collect
Linker-managed collections: slices, sorted slices, maps

Crates

The linktime project comprises three crates, and the top-level linktime crate aggregates them all.

Pick-and-choose, or import the top-level crate to get all three.

ctor

Module initialization functions for Rust (like __attribute__((constructor)) in C/C++).

Run code before main to initialize data, external resources, or other state.

toml
[dependencies]
linktime = { version = "...", features = ["ctor"] }  # note: already enabled by default
# or
ctor = "..."
rust
use linktime::ctor; // or ctor::ctor
use libc_print::*;

#[ctor(unsafe)]
fn foo() {
    libc_println!("Life before main!");
}

dtor

Module shutdown functions for Rust (like __attribute__((destructor))).

Run code after main to clean up resources, or perform other final operations.

toml
[dependencies]
linktime = { version = "...", features = ["dtor"] }  # note: already enabled by default
# or
dtor = "..."
rust
use linktime::dtor; // or dtor::dtor
use libc_print::*;

#[dtor(unsafe)]
fn foo() {
    libc_println!("Life after main!");
}

link-section

Typed and untyped link section support for Rust.

Collect related items from an entire linked binary into a single link section.

toml
[dependencies]
linktime = { version = "...", features = ["link-section"] }  # note: already enabled by default
# or
link-section = "..."
rust
use linktime::link_section::{section, in_section, TypedSection};
use linktime::ctor;
use libc_print::*;

#[section(typed)]
static FOO: TypedSection;

#[in_section(FOO)]
fn foo() {
    libc_println!("Hello, world!");
}

#[ctor(unsafe)]
fn print_numbers() {
    for f in FOO {
        f();
    }
}

scattered-collect

A crate for defining zero-allocation,linker-managed scattered collections in Rust.

  • ScatteredIterable: A collection of items that are available only via
  • ScatteredSlice: A collection of sized items that collected into a slice in an arbitrary order.
  • ScatteredSortedSlice: A collection of items that are available via slice, in sorted order.
  • ScatteredReferencedSlice: A collection of items collected into a slice (link order).
  • ScatteredSortedReferencedSlice: A collection of sized items that are available both via sorted slice and via reference at the declaration site.
  • ScatteredMap: A collection of key-value pairs that are available via slice, as well as indexed by key.
use scattered_collect::{gather, scatter, slice::ScatteredSlice};

#[gather]
static SLICE_PLUGINS: ScatteredSlice;

#[scatter(SLICE_PLUGINS)]
const _: &'static str = "json";

#[scatter(SLICE_PLUGINS)]
const _: &'static str = "yaml";

fn main() {
    assert_eq!(SLICE_PLUGINS.len(), 2);
    assert!(SLICE_PLUGINS.contains(&"json"));
}

UEFI

UEFI is fully supported (both with std and #![no_std]), but ctors and dtors must be manually run via run_constructors and run_destructors, respectively.

#[export_name = "efi_main"]
extern "efiapi" fn efi_main(_handle: *mut c_void, _st: *mut c_void) -> usize {
    unsafe { linktime::run_constructors() };

    // ... application ...

    unsafe { linktime::run_destructors() };
    0
}

Contributing

Contributions are welcome!

License

These projects are dual-licensed under the Apache License, Version 2.0 and the MIT License.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rustconstructorctordtordylib

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言