EvilZone
Programming and Scripting => C - C++ => : techb August 01, 2012, 01:57:44 AM
-
So I was reading this (http://stackoverflow.com/questions/6215782/do-unused-functions-get-optimized-out) doing research on if the compiler or linker will include unused functions. It says for the most part it will not include unused functions..
So, if I include string.h and only use strcmp(), will it also include all the other functions? If so, is there a switch I can give gcc to exclude them? Extra unused functions just seems like bloat to me.
-
If you compile it with dinamyc linking (default) no external functions are included at all in the final executable. They will be loaded at run time from external libraries. You can see which symbols are included (and will be loaded) with
objdump -T file
Example:
#include <string.h>
#include <stdio.h>
int main (int argc, char *argv[])
{
if (argv[1])
if (!strcmp (argv[1], "foo"))
printf ("bar\n");
return 0;
}
[ca0s@st4ck-3rr0r Tests]$ objdump -T strcmp
strcmp: file format elf64-x86-64
DYNAMIC SYMBOL TABLE:
0000000000000000 DF *UND* 0000000000000000 GLIBC_2.2.5 puts
0000000000000000 DF *UND* 0000000000000000 GLIBC_2.2.5 __libc_start_main
0000000000000000 DF *UND* 0000000000000000 GLIBC_2.2.5 strcmp
0000000000000000 w D *UND* 0000000000000000 __gmon_start__
Linker will load the library in memory and resolve function's address.
If you link it statically, it seems every function of the library is linked:
[ca0s@st4ck-3rr0r Tests]$ gcc -static -o strstatic strcmp.c
[ca0s@st4ck-3rr0r Tests]$ objdump -t strstatic | grep strrchr #this one, for example
0000000000434a40 g F .text 000000000000009f strrchr
I have searched how to do it, but no method has worked.
-
I haven't really gotten into debugging yet, thanks for the explaintion. I only know the basics of compiling atm, learning as I go I guess.
-
If you're wanting to strip out extra symbols from a binary, use the 'strip' command. Typically, the '-s' option can be passed to remove all symbols.
man 1 strip