Does gnu ld contain whole object files or just the necessary functions?

We have a library and an executable that should be statically linked to lib. We want to minimize the program space of the final executable.

According to avr-libc documentation:

linker links in the TOP OBJECT MODULE in which the function is located

On the other hand, my colleagues are unanimous that at some point the linker throws away any unused functions.

So who is right, or am I not understanding something? Is the answer consistent across gcc or are we only talking about avr port here?

+4
source share
1 answer

It does not delete dead code unless you say it. To do this, you need to compile everything with:

  -fdata-sections -ffunction-sections 

to mark all data and functions. And when you contact GCC, you need to pass:

  -Wl, - gc-sections 

so that garbage collects all unused partitions.

+7
source

Source: https://habr.com/ru/post/1447894/


All Articles