Author Topic: [C]Linker stripping?  (Read 1513 times)

0 Members and 1 Guest are viewing this topic.

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
[C]Linker stripping?
« on: August 01, 2012, 01:57:44 am »
So I was reading this 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.
>>>import this
-----------------------------

Offline ca0s

  • VIP
  • Sir
  • *
  • Posts: 432
  • Cookies: 53
    • View Profile
    • ka0labs #
Re: [C]Linker stripping?
« Reply #1 on: August 01, 2012, 02:01:34 pm »
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
Code: [Select]
objdump -T file
Example:
Code: (C) [Select]
#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;
}
Code: [Select]
[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:
Code: [Select]
[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.

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: [C]Linker stripping?
« Reply #2 on: August 01, 2012, 02:16:14 pm »
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.
>>>import this
-----------------------------

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: [C]Linker stripping?
« Reply #3 on: September 06, 2012, 11:47:15 pm »
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
-Xires