Skip to content

Linking to a static library does not always include exported symbols #99721

@mkroening

Description

@mkroening

Similarly to #47384, statics in a static library that are marked as used are not included in the resulting binary if a non-inlined function is used in the same module.

Example

This code can be found at mkroening/rust-issue-99721.

staticlib.rs:

mod note {
    #[used]
    #[link_section = ".note.my-note"]
    static MY_NOTE: u8 = 0;

    #[inline(always)]
    pub fn dummy() {}
}

#[no_mangle]
pub extern "C" fn staticlib() {
    note::dummy();
}

bin.rs:

fn main() {
    extern "C" {
        fn staticlib();
    }

    unsafe {
        staticlib();
    }
}

Build using:

rustc --crate-type staticlib staticlib.rs
rustc -L native=. -l static=staticlib bin.rs

Display static library notes:

$ readelf -Wn libstaticlib.a
[...]
File: libstaticlib.a(staticlib.staticlib.68b5aa53-cgu.0.rcgu.o)

Displaying notes found in: .note.my-note
  Owner                Data size 	Description
readelf: Warning: Corrupt note: only 1 byte remains, not enough for a full note
[...]

Display binary notes:

$ readelf -Wn bin
Displaying notes found in: .note.gnu.property
  Owner                Data size 	Description
  GNU                  0x00000010	NT_GNU_PROPERTY_TYPE_0	      Properties: x86 ISA needed: x86-64-baseline

Displaying notes found in: .note.gnu.build-id
  Owner                Data size 	Description
  GNU                  0x00000014	NT_GNU_BUILD_ID (unique build ID bitstring)	   Build ID: 258d83311faf1a32cb84dd090ccc57abdbb1cb38

Displaying notes found in: .note.ABI-tag
  Owner                Data size 	Description
  GNU                  0x00000010	NT_GNU_ABI_TAG (ABI version tag)	    OS: Linux, ABI: 4.4.0

If note::dummy is made inline(never), the symbol is included in the resulting binary:

$ readelf -Wn bin
Displaying notes found in: .note.gnu.property
  Owner                Data size 	Description
  GNU                  0x00000010	NT_GNU_PROPERTY_TYPE_0	      Properties: x86 ISA needed: x86-64-baseline

Displaying notes found in: .note.gnu.build-id
  Owner                Data size 	Description
  GNU                  0x00000014	NT_GNU_BUILD_ID (unique build ID bitstring)	   Build ID: 72d6efcf61e2b33563094c3f179ebcc511db7485

Displaying notes found in: .note.ABI-tag
  Owner                Data size 	Description
  GNU                  0x00000010	NT_GNU_ABI_TAG (ABI version tag)	    OS: Linux, ABI: 4.4.0

Displaying notes found in: .note.my-note
  Owner                Data size 	Description
readelf: Warning: Corrupt note: only 1 byte remains, not enough for a full note

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-linkageArea: linking into static, shared libraries and binariesC-discussionCategory: Discussion or questions that doesn't represent real issues.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions