1

I am trying to remove unused functions (having no calls made to them) in my code which is built on x86 / Linux using gcc/g++. I use below options as I read from the gcc manual:

CCFLAGS = -ffunction-sections -fdata-sections -Wl,--gc-sections,--print-gc-sections

Make command output does show some unused sections removed messages as below:

/usr/bin/ld: Removing unused section '.text._ZN14myclass30myunusedfuncEPPNS_8device_sE' in file './myproject/x86-linux/release/file1.o'

But just to make sure, I tried to find if that symbol was present in the *.o object file, so when I do:

strings --all -f file1.o|grep myunusedfunc

I see output as below

myproject/x86-linux/debug/file1.o: [] myclass::myunusedfunc()
myproject/x86-linux/debug/file1.o: myunusedfunc
myproject/x86-linux/debug/file1.o: _ZN14myclass30myunusedfuncEPPNS_8device_sE
myproject/x86-linux/debug/file1.o: .rel.text._ZN14myclass30myunusedfuncEPPNS_8device_sE
myproject/x86-linux/debug/file1.o: _ZN14myclass30myunusedfuncEPPNS_8device_sE

What's going on?

Have the unused functions really been removed by these gcc/ld options:

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
goldenmean
  • 18,376
  • 54
  • 154
  • 211
  • 5
    Did you check with `nm` that these names are never referenced? And these function would be removed at link time, e.g. only removed from the produced (output) binary executable, not in the (input) object files `*.o` – Basile Starynkevitch Jan 07 '13 at 18:17
  • 1
    Or check the size of your binary with size. You also have another option to remove those and possibly improve binary. See -flto gcc option (possibly together with -Os). – dbrank0 Jan 07 '13 at 21:30
  • @Basile and dbrank0 : I checked the strings ... command on the generated linux executable as well. Same result. The functions did show up in the strings output as well. – goldenmean Jan 08 '13 at 14:12
  • @Basile - nm |grep myunusedfunc did not show any output for that function. So what you said makes sense. Thank you. So strings output is different than nm output. So seems like the symbols are present in the final executable as strings found it, but since nm did not find it, it means these symbols are present indeed in some section of the executable but that section would not be loaded when executed or in some kind of 'disabled code section' something of that effect. – goldenmean Jan 08 '13 at 14:15

1 Answers1

1

You're looking at the .o file, but that's the input to the linker, not the output. The .o file will contain all the functions, the linker should not emit unused ones into the linked output. It doesn't edit the .o files though.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • I checked the strings ... command on the generated linux executable as well. Same result. The functions did show up in the strings output as well. – goldenmean Jan 08 '13 at 14:12