removing unused functions during final link

Melvin Blades melvin.blades@gmail.com
Fri Sep 5 15:24:00 GMT 2014


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



More information about the Gcc-help mailing list