Skip to content

Linking a staticlib into shared C library or executable with gcc causes considerbly larger binary than linking into a cdylib or executable with rustc #111593

Closed
rust-lang/reference
#1361
@sdroege

Description

@sdroege

See https://github.com/sdroege/rust-staticlib-bloat-test for the code. This doesn't use cargo to make the actual compiler invocations clear (the same behaviour can be observed with cargo).

With Rust 1.69 and gcc 13.1.1 on Linux x86-64 the following can be observed:

unstripped stripped (--strip-all)
Rust executable (my_test) 4.1MB 315kB
Rust cdylib (libstaticlib.so) 4.0MB 290kB
gcc executable (my_staticlib_test) 5.2MB (+ 27%) 1.1MB (+ 355%)
gcc shared library (libmy_staticlib_test_dylib.so) 5.6MB (+ 40%) 1.5MB (+ 517%)

The actual size comes from the enabled overflow checks (-C debug-assertions=on) , and thus all the panic and string formatting machinery that is included. However, this should be approximately the same code in either case so that doesn't explain the difference. (Disabling the overflow checks gives a more expected result because the function from the library compiles down to a few instructions) Also note that this compiles with -C panic=abort so having the assertion in an extern "C" function is not UB.

The question here now is what exactly the difference between both cases is, and how the staticlib case can be improved. Maybe this requires changes in the compiler, maybe this just requires invoking the linker differently (in which case this would be useful to add to the documentation).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-linkageArea: linking into static, shared libraries and binariesC-bugCategory: This is a bug.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