If I statically link a C library, will the unused functions be optimized out?
Clash Royale CLAN TAG#URR8PPP
If I statically link a C library, will the unused functions be optimized out?
My feeling is that essentially 100% of the time this is what you would want to happen, but I suspect that there might be some theoretical caveats, for example:
Say I statically link the standard library and I use printf
but not sprintf
. Further suppose that I know that &sprintf == &printf + SPRINTF_OFFSET
. How could the compiler know I'm never accessing sprintf
like this? Does the standard prohibit it somehow?
printf
sprintf
&sprintf == &printf + SPRINTF_OFFSET
sprintf
uintptr_t
sprintf()
How could you know the offset, if one of the two functions is not linked?
– Yunnosch
Aug 5 at 17:26
@Yunnosch good point.
– Benjamin Lindqvist
Aug 5 at 17:27
Is there a version of your question (which I find somewhat interesting but cannot give a substantial "not possible" answer to), without that logical obstacle? Try to rephrase around it, otherwise I fear the question might be closed as "cannot be reproduced".
– Yunnosch
Aug 5 at 17:31
On the other hand, if you consider my nitpicking to be a negative but otherwise helpful answer, let me know, then I will of course turn my comment into an official answer.
– Yunnosch
Aug 5 at 17:34
1 Answer
1
If I statically link a C library, will the unused functions be optimized out?
Yes, provided they are not part of an object that is pulled into the link via some other symbol.
To understand how the linker works, read this or this.
How could the compiler know I'm never accessing sprintf like this?
The C
language standard prohibits computing a pointer that doesn't point to a valid object, or just beyond the last element of an array. Your example is ill-formed.
C
I'd accept this if you posted a link explaining your second paragraph. The term "invalid object" seems very imprecise to me.
– Benjamin Lindqvist
Aug 6 at 5:06
@BenjaminLindqvist There is no "invalid object" in my answer. If you want the precise chapter and verse, start with "6.2.4 Storage durations of objects" of the C standard, which defines how objects become valid (start their lifetime) and how the object lifetime ends (at which point the object is no longer valid).
– Employed Russian
Aug 6 at 5:16
Yes there is, you wrote that the standard prohibits computing a pointer that doesn't point to a "valid object". Besides, I fail to see how "storage duration" applies to a statically linked function. Please quote the relevant parts of the standard.
– Benjamin Lindqvist
Aug 6 at 17:05
Prob better to not bother because the original question is slightly too low quality anyway.
– Benjamin Lindqvist
Aug 6 at 17:12
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You can't do computations on function addresses as shown (you'd need casts to
uintptr_t
or similar, and then cast back to a suitable function pointer, at minimum — andsprintf()
won't be at a fixed offset). The linker links object files from the static library that satisfy unsatisfied references in the previous object code. It won't pull in other uncalled functions unless they're part of the same object file as the one that is called. It iterates until there are no more references that can be satisfied by the static library; then it moves onto the next library in the linker command line.– Jonathan Leffler
Aug 5 at 17:25