public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* removing unused functions during final link
@ 2014-08-27 21:02 Melvin Blades
  2014-08-27 21:22 ` Ian Lance Taylor
  0 siblings, 1 reply; 7+ messages in thread
From: Melvin Blades @ 2014-08-27 21:02 UTC (permalink / raw)
  To: gcc-help

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: removing unused functions during final link
  2014-08-27 21:02 removing unused functions during final link Melvin Blades
@ 2014-08-27 21:22 ` Ian Lance Taylor
  2014-09-03  3:20   ` Melvin Blades
  0 siblings, 1 reply; 7+ messages in thread
From: Ian Lance Taylor @ 2014-08-27 21:22 UTC (permalink / raw)
  To: Melvin Blades; +Cc: gcc-help

On Wed, Aug 27, 2014 at 2:02 PM, Melvin Blades <melvin.blades@gmail.com> wrote:
>
> 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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: removing unused functions during final link
  2014-08-27 21:22 ` Ian Lance Taylor
@ 2014-09-03  3:20   ` Melvin Blades
  2014-09-03 10:21     ` Andrew Haley
  0 siblings, 1 reply; 7+ messages in thread
From: Melvin Blades @ 2014-09-03  3:20 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: gcc-help

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

On Wed, Aug 27, 2014 at 2:22 PM, Ian Lance Taylor <iant@google.com> wrote:
> On Wed, Aug 27, 2014 at 2:02 PM, Melvin Blades <melvin.blades@gmail.com> wrote:
>>
>> 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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: removing unused functions during final link
  2014-09-03  3:20   ` Melvin Blades
@ 2014-09-03 10:21     ` Andrew Haley
  2014-09-03 15:09       ` Melvin Blades
  0 siblings, 1 reply; 7+ messages in thread
From: Andrew Haley @ 2014-09-03 10:21 UTC (permalink / raw)
  To: Melvin Blades, Ian Lance Taylor; +Cc: gcc-help

On 09/03/2014 04:20 AM, Melvin Blades wrote:
> 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.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: removing unused functions during final link
  2014-09-03 10:21     ` Andrew Haley
@ 2014-09-03 15:09       ` Melvin Blades
  2014-09-03 15:13         ` Andrew Haley
  0 siblings, 1 reply; 7+ messages in thread
From: Melvin Blades @ 2014-09-03 15:09 UTC (permalink / raw)
  To: Andrew Haley; +Cc: Ian Lance Taylor, gcc-help

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

~

On Wed, Sep 3, 2014 at 3:21 AM, Andrew Haley <aph@redhat.com> wrote:
> On 09/03/2014 04:20 AM, Melvin Blades wrote:
>> 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.
>
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: removing unused functions during final link
  2014-09-03 15:09       ` Melvin Blades
@ 2014-09-03 15:13         ` Andrew Haley
  2014-09-05 15:24           ` Melvin Blades
  0 siblings, 1 reply; 7+ messages in thread
From: Andrew Haley @ 2014-09-03 15:13 UTC (permalink / raw)
  To: Melvin Blades; +Cc: Ian Lance Taylor, gcc-help

On 09/03/2014 04:09 PM, Melvin Blades wrote:
> 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.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: removing unused functions during final link
  2014-09-03 15:13         ` Andrew Haley
@ 2014-09-05 15:24           ` Melvin Blades
  0 siblings, 0 replies; 7+ messages in thread
From: Melvin Blades @ 2014-09-05 15:24 UTC (permalink / raw)
  To: Andrew Haley; +Cc: Ian Lance Taylor, gcc-help

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
============ ==================================

On Wed, Sep 3, 2014 at 8:13 AM, Andrew Haley <aph@redhat.com> wrote:
> On 09/03/2014 04:09 PM, Melvin Blades wrote:
>> 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.
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2014-09-05 15:24 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-27 21:02 removing unused functions during final link Melvin Blades
2014-08-27 21:22 ` Ian Lance Taylor
2014-09-03  3:20   ` Melvin Blades
2014-09-03 10:21     ` Andrew Haley
2014-09-03 15:09       ` Melvin Blades
2014-09-03 15:13         ` Andrew Haley
2014-09-05 15:24           ` Melvin Blades

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).