MarkP
MarkP

Reputation: 4278

Selectively linking symbols in archive

I have a project that is linking in libcrypto.a. But All I'm interested in is, as an example, the SHA functionality. Is there a way to specify that all other unused functions should be stripped from the resulting binary?

GCC 4.8 is part of the toolchain.

Upvotes: 1

Views: 301

Answers (1)

Tamás Zahola
Tamás Zahola

Reputation: 9321

By default, the GNU linker should pull in only the necessary object files, unless you specify the --whole_archive flag.

If you want dead-code elimination, follow use the following linker flags:

-Wl,-static

Link against static libraries. Required for dead-code elimination.

-fvtable-gc

C++ virtual method table instrumented with garbage collection information for the linker.

-fdata-sections

Keeps data in separate data sections, so they can be discarded if unused.

-ffunction-sections

Keeps functions in separate data sections, so they can be discarded if unused.

-Wl,--gc-sections

Tell the linker to garbage collect and discard unused sections.

-s

Strip the debug information, so as to make the code as small as possible. (I presume that you'd want to do this in a dead-code removal build.)

Source: https://gcc.gnu.org/ml/gcc-help/2003-08/msg00128.html

Upvotes: 2

Related Questions