Skip to main content

embed

Macro embed 

Source
macro_rules! embed {
    () => { ... };
}
Expand description

Force the module_info rlib to be linked into the consuming binary so the .note.package section is emitted with ELF type SHT_NOTE.

§Why this is needed

The note data is produced by the linker script that build.rs generates. GNU ld assigns SHT_NOTE to the output .note.package only when an input object file contributes a same-named input section already typed SHT_NOTE; this crate provides exactly that input section through the #[link_section = ".note.package"] static PACKAGE_NOTE_SECTION. Without a source-level reference to this crate, cargo/rustc drops the module_info rlib from the final link, no SHT_NOTE input section is present, and ld synthesizes the output section from the script’s BYTE(...) directives alone, which yields SHT_PROGBITS. The bytes are present, but tools like readelf -n and systemd-coredump filter by section type and ignore it.

Invoking module_info::embed!() at the crate root creates a #[used] reference to [PACKAGE_NOTE_SECTION], which forces the rlib to link and restores the correct section type.

§When to use it

Use embed!() when the consuming crate does not call get_module_info! or reference any other module_info item at runtime (pure build-time embedding). When the consuming crate already calls module_info::get_module_info!(...) or imports any item from the crate, this macro is unnecessary; the rlib is already linked.

§Example

// Top of src/main.rs or src/lib.rs:
module_info::embed!();

fn main() {
    // No other module_info references needed for the .note.package
    // section to end up in the binary with SHT_NOTE type.
}