Skip to content

Static linking without LTO on Linux does not remove unused code #39270

Closed
@sdroege

Description

@sdroege

When linking a minimal cdylib on Linux with Rust 1.14.0, lots of code from libstd is included that should not be. According to @sfackler, on MacOS all that code is not included.
Enabling LTO will get it removed on Linux, while it's not required on MacOS.

When compiling a mini-cdylib (see below), you can e.g. find functions for receiving packets from UDP sockets:

$ objdump -T ./target/release/libtest.so | grep net | grep recv
0000000000074710 g    DF .text	000000000000003f  Base        _ZN3std3sys3imp3ext3net12UnixDatagram4recv17hd6877157b1dad8adE
0000000000074210 g    DF .text	00000000000004fe  Base        _ZN3std3sys3imp3ext3net12UnixDatagram9recv_from17h5fb97d5dc256448cE
0000000000061bf0 g    DF .text	0000000000000177  Base        _ZN3std3net3udp9UdpSocket9recv_from17h69eac8c4da79232dE
0000000000062770 g    DF .text	000000000000003f  Base        _ZN3std3net3udp9UdpSocket4recv17hf94a0fd19de68323E

See following mini-cdylib for reproducing

src/lib.rs

#[no_mangle]                                                                                               
pub extern "C" fn foo(meh: &i32) {
    println!("foo {}", meh);
}

Cargo.toml

[package]
name = "test"
version = "0.1.0"
authors = ["Some One <[email protected]>"]

[profile.release]
#lto = true

[lib]
name = "test"
crate-type = ["cdylib"]
path = "src/lib.rs"

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