Discussion:
removing unused functions during final link
Melvin Blades
2014-08-27 21:02:39 UTC
Permalink
I've inherited a pile of code and need to cross compile it for an
embedded MIPs processor
I'm creating a single app that will be run under Linux.
My app code links to another larger pile of third-party software (utilities)
My app uses only a part of the these utilities, but the final
executable is very large and includes utilities functions that are
never called.
My app also will dynamically link with uclibc

My compilation/linking results in an app so large that I can't load it
on my eval board.

What command line options can I use to get the linker to strip unused
functions from the final executable?

I've googled and came up with some proposed solutions

This one
https://gcc.gnu.org/ml/gcc-help/2003-08/msg00128.html
results in an error message .. -f may not be used without -shared

Other places say that the dead-strip option is architecture dependent.
http://embeddedfreak.wordpress.com/2009/02/10/removing-unused-functionsdead-codes-with-gccgnu-ld/
Where can I find out if the MIPS compiler supports this?
Or whether I just haven't used the correct command line options for
compiling and linking
Ian Lance Taylor
2014-08-27 21:22:03 UTC
Permalink
Post by Melvin Blades
What command line options can I use to get the linker to strip unused
functions from the final executable?
Compile with -ffunction-sections -fdata-sections.

Link with -Wl,--gc-sections.

Ian
Melvin Blades
2014-09-03 03:20:01 UTC
Permalink
Using gcc instead of ld, I was able to get it to strip unused
functions if all the functions in a file are not called.
But I could not get it to strip unused functions if they are in the
same source file as used functions .

Is there a way to get it strip ALL the unused functions?

Thanks for any hints, I'm running out of things to try
Post by Ian Lance Taylor
Post by Melvin Blades
What command line options can I use to get the linker to strip unused
functions from the final executable?
Compile with -ffunction-sections -fdata-sections.
Link with -Wl,--gc-sections.
Ian
Andrew Haley
2014-09-03 10:21:41 UTC
Permalink
Post by Melvin Blades
Using gcc instead of ld, I was able to get it to strip unused
functions if all the functions in a file are not called.
But I could not get it to strip unused functions if they are in the
same source file as used functions .
Is there a way to get it strip ALL the unused functions?
That should have worked. Please tell us the exact command you used.

Andrew.
Melvin Blades
2014-09-03 15:09:25 UTC
Permalink
For a simple example I created 3 files.
root.c -- has main() which calls one function in show.c
show.c - has two functions, one is called by main, one is not
called. ( both use puts to print a message.)
show2.c -- has two functions. Neither are called

--------Makefile--------

OBJS := root.o show.o show2.o
SOURCE := root.c show.c show2.c

all: $(SOURCE)
${CC} -mips32 -fdata-sections -ffunction-sections -c $(SOURCE)
${CC} $(OBJS) -Wl,--gc-sections -o root.exe
-------End --------

The resulting executable contains the texts strings and function names
from both functions in show.c and neither function of show2.c My
usage of -fdata-section -ffunction-section and -gc-sections prevents
the functions in show2.c from being linked in , but not the unused
functions in show.c

$home/dev1/>file root.exe
root.exe: ELF 32-bit LSB executable, MIPS, MIPS32 version 1 (SYSV),
dynamically linked (uses shared libs), not stripped

~
Post by Andrew Haley
Post by Melvin Blades
Using gcc instead of ld, I was able to get it to strip unused
functions if all the functions in a file are not called.
But I could not get it to strip unused functions if they are in the
same source file as used functions .
Is there a way to get it strip ALL the unused functions?
That should have worked. Please tell us the exact command you used.
Andrew.
Andrew Haley
2014-09-03 15:13:44 UTC
Permalink
Post by Melvin Blades
The resulting executable contains the texts strings and function names
from both functions in show.c and neither function of show2.c My
usage of -fdata-section -ffunction-section and -gc-sections prevents
the functions in show2.c from being linked in , but not the unused
functions in show.c
Something must be referring to them; they must be reachable.
That or there's a bug.
There's no way for anyone to know without seeing code.

Andrew.
Melvin Blades
2014-09-05 15:24:00 UTC
Permalink
As far as I can tell -- for my tiny example -- with the proper
compiler and linker options, the unused FUNCTIONS do not appear in the
final executable, but the unused string-data does appear in the
executable.
Is that normal?


For example ... in this function

void not_called_function(){
puts("!!not_called\n");
}
the string "!!not_called!!" shows up in the executable, although I
think the function it self is not included in the build.
( previously I thought the function was present since the string had
the same name)

Here are my 3 source files
=======root.c ====================================
#include <stdio.h>
extern void show_the_message(void);

int main(void)
{
puts("hello world");
show_the_message();
return 1;
}
=======show.c ====================================
#include <stdio.h>
void show_the_message(void)
{ puts("!!!Here is the message !!!");
}
void not_called_function(void)
{ puts("!!! not_called !!!");// This string appears in the executable
}
=======show2.c ====================================
#include <stdio.h>
// Neither of these functions are called, so they are not linked in

void show_the_last_message(void)
{ puts("!!!Here is the last one !!!");// this string does not
appear in the executable
}
void show_the_first_one(void)
{ puts("!!!Here is the first one !!!"); // This string does not
appear in the executable
}
=======Makefile ==================================
OBJS := root.o show.o show2.o
SOURCE := root.c show.c show2.c
# CC is defined as mipsel-linux-uclibc-gcc
all: show.c show2.c root.c
${CC} -mips32 -fdata-sections -ffunction-sections -c $(SOURCE)
${CC} $(OBJS) -Wl,--gc-sections -o root.exe
============ ==================================
Post by Andrew Haley
Post by Melvin Blades
The resulting executable contains the texts strings and function names
from both functions in show.c and neither function of show2.c My
usage of -fdata-section -ffunction-section and -gc-sections prevents
the functions in show2.c from being linked in , but not the unused
functions in show.c
Something must be referring to them; they must be reachable.
That or there's a bug.
There's no way for anyone to know without seeing code.
Andrew.
Continue reading on narkive:
Loading...