4

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

According to avr-libc's documentation:

the linker links in THE ENTIRE OBJECT MODULE in which the function is located

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

So who is correct or am I misunderstanding something? Is the answer consistent throughout gcc or are we talking only the avr port here?

4
  • the linker cannot know if a function will be called, for instance by a function pointer. Commented Nov 23, 2012 at 9:16
  • I guess if a function name is mentioned in the sources, the function is considered "used", so must be linked in. Commented Nov 23, 2012 at 9:21
  • usually it's compiler task, as Nikos C. highlights Commented Nov 23, 2012 at 9:56
  • A "how to do it version": stackoverflow.com/questions/1033898/… Commented May 15, 2015 at 20:02

1 Answer 1

10

It doesn't perform dead code stripping unless you tell it to. In order to do that, you need to compile everything with:

-fdata-sections -ffunction-sections

in order to mark all data and functions. And when linking with GCC you need to pass:

-Wl,--gc-sections

in order to garbage-collect all unused sections.

Sign up to request clarification or add additional context in comments.

1 Comment

So both of my sources were correct and I had not understood everything correct. Just as expected. Thank you for the prompt answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.