C compilation optimization: deleted unedited parts on the fly

We are faced with an interesting topic. Suppose we have a special-functions.c file, basically a library. We need to optimize the code by getting rid of all unused / unaccounted functions during the build process on the fly. I'm not looking for unused (dead) code at all: some parts will be β€œdead” if compiled to one of the architectures, but they will be used in another architecture assembly.

Does anyone know flags, tools, methods, and tricks? The compiler is a standard gcc with the code ansi 99 C.

EDIT I ​​know this is basically part of the linker, but using gcc the process is not split into two parts.

+4
source share
3 answers

From http://embeddedfreak.wordpress.com/2009/02/10/removing-unused-functionsdead-codes-with-gccgnu-ld/ :

  • Compile with -fdata-sections to save data in separate section data and -ffunction-sections to save functions in separate sections, so they (data and functions) can be discarded if they are not used.
  • Link to --gc-sections to delete unused partitions.

For instance:

 gcc -Os -fdata-sections -ffunction-sections test.c -o test -Wl,--gc-sections 
+2
source

I think the recent GCC (i.e. 4.6 ) should do this if you compile and link the -flto flag (link time optimization). I would suggest that having hidden or internal visibility should be relevant (at least for non-static functions).

+2
source

As far as I know, GNU binary utils (ld, in this case) already remove unusesd links by static link

0
source

Source: https://habr.com/ru/post/1384068/


All Articles