Optimization - removing unused code

Madani, Srikanth, VF-DE Srikanth.Madani@vodafone.com
Wed Jan 12 16:49:00 GMT 2005


Following up the thread titled "RE: ld warning (was Optimization, remove unused code from  Image)":

> Will retry on a Linux machine where everything is GNU, and see if I have
> better results there.

#Compilation : gcc version 3.3.1 (SuSE Linux)
$> gcc -c -ffunction-sections *.c

#Linking : GNU ld version 2.14.90.0.5 20030722 (SuSE Linux)
$> gcc -Wl,--gc-sections *.o

$> file a.out
a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked (uses shared libs), not stripped

a.out works fine, but contains the uncalled function func3() in the code section.

If I link it statically, however:
$> gcc -Wl,--gc-sections,-static *.o

$> file a.out
a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, not stripped


And a.out doesn't contain func3(). AND, it doesn't contain func2() either.

So, unused code has been removed - but only (as man ld stipulates) if you link statically.

Cheers,  
Srikanth Madani
Düsseldorf, NRW




________________________________________________________________
The source files are:
 
$> cat hello.c
int main(void) {
        printf("\nHello world!\n");
        func1();
        exit(0);
}
$>


$> cat fun1.c
void func1(void) {
        printf("\nIn function func1()\n");
}
void func2(void) {
        func3();
}
$>


$> cat fun2.c
void func3(void) {
        printf("\nIn function func3()\n");
}
$>



More information about the Gcc-help mailing list