Search code examples
gccclangstatic-librarieselfsections

Distinct static library compiled with -ffunction-sections flag


How to distinct static library compiled with -ffunction-sections compiler flag?

I want to determine is some particular .a library can benefit from -Wl,--gc-sections flag.

If there is way to list all the section names, then I can apply | wc -l to it and infer, that there is too many sections and the library is likely compiled with mentioned flags.

readelf -S just prints archived *.o filenames.


Solution

  • A simple take on this:

    # Collect function sections
    $ readelf -S tmp.o | sed -ne 's/.*\] \.text.\([a-zA-Z0-9_]\+\) .*/\1/p' | sort -u > fun_sec.lst
    
    # Collect function symbols
    $ nm tmp.o | grep ' T ' | awk '{print $3}' | sort -u > fun_sym.lst
    
    # Compare
    $ COMM=$(comm -12 fun_sym.lst fun_sec.lst | wc -l)
    $ UNIQ=$(comm -3 fun_sym.lst fun_sec.lst | wc -l)
    $ if test $COMM -gt $UNIQ; then echo "tmp.o was likely compiled with -ffunction-sections"; fi