How to use "--gc-sections" link option to reduce target binary size?

Just got some hints from blogs saying that using -ffunction-sections -fdata-sections -Wl,--gc-sections can even further reduce binary target file size.

Is that true? And how to use these link options?


  • "-Cllvm-args=-ffunction-sections"
  • "-Cllvm-args=-fdata-sections"
  • "-Clink-arg=-Wl,--gc-sections"

in config.toml
rustflags settings, but got unknown link options...

I think those are GCC specific options?

I'm not sure these are GCC specific, you mean LLVM backend doesn't support these options?

I don't know whether LLVM supports them.

Rustc already tells LLVM to use what amounts to -ffunction-sections -fdata-sections and passes -Wl,--gc-sections to the linker. You don't need to pass them yourself.

You may want to follow some of the suggestions in to reduce the binary size.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.