Bug 49258 - -ffunction-sections and --gc-sectinons have no effect at gcj
Summary: -ffunction-sections and --gc-sectinons have no effect at gcj
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: java (show other bugs)
Version: 4.4.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-06-01 15:55 UTC by licheng.1212@gmail.com
Modified: 2012-04-09 19:40 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2011-12-31 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description licheng.1212@gmail.com 2011-06-01 15:55:53 UTC
there is many dead code at java source,and the gcj can't remove it.
I hava been add the function-sections and gc-sections to the compiler flags.
Comment 1 Andrew Pinski 2011-12-31 00:05:14 UTC
>there is many dead code at java source,and the gcj can't remove it.

But Java is a dynamic language so the vtable will reference most of it.

Can you provide an example of where you think a dead function is not being removed?
Comment 2 licheng.1212@gmail.com 2012-04-07 07:53:34 UTC
[licheng@rda ~]$ cat hello.java 
class hello{
	public void notused()
	{
		System.out.println("not used");
	}
	public static void main(String argv[])
	{
		System.out.println("Hello");	
	}
}
[licheng@rda ~]$ gcj --main=hello  hello.java -o hello -ffunction-sections -Wl,--gc-sections,--print-gc-sections
/usr/bin/ld: Removing unused section '.rodata' in file '/usr/lib/gcc/i686-redhat-linux/4.6.3/../../../crt1.o'
/usr/bin/ld: Removing unused section '.rodata' in file '/usr/lib/gcc/i686-redhat-linux/4.6.3/crtbegin.o'
[licheng@rda ~]$ ./hello 
Hello
[licheng@rda ~]$ objdump -d hello > hello.asm
[licheng@rda ~]$ cat hello.asm | grep notused -C 5
 8048866:	e8 a5 fe ff ff       	call   8048710 <_ZN4java4lang6ObjectC1Ev@plt>
 804886b:	c9                   	leave  
 804886c:	c3                   	ret    
 804886d:	90                   	nop

0804886e <_ZN5hello7notusedEJvv>:
 804886e:	55                   	push   %ebp
 804886f:	89 e5                	mov    %esp,%ebp
 8048871:	53                   	push   %ebx
 8048872:	83 ec 24             	sub    $0x24,%esp
 8048875:	bb 00 00 00 00       	mov    $0x0,%ebx
 804887a:	84 db                	test   %bl,%bl
 804887c:	75 0c                	jne    804888a <_ZN5hello7notusedEJvv+0x1c>
 804887e:	c7 04 24 c0 9e 04 08 	movl   $0x8049ec0,(%esp)
 8048885:	e8 46 fe ff ff       	call   80486d0 <_Jv_InitClass@plt>
 804888a:	a1 a0 9e 04 08       	mov    0x8049ea0,%eax
 804888f:	89 45 f4             	mov    %eax,-0xc(%ebp)
 8048892:	8b 45 f4             	mov    -0xc(%ebp),%eax
[licheng@rda ~]$ 

my gcj version is gcj (GCC) 4.6.3 20120306 (Red Hat 4.6.3-2)
Comment 3 Andrew Pinski 2012-04-07 08:50:03 UTC
There is nothing to be done here since notused is used from the vtable for the class.

"objdump -d hello" does not show the vtables.
Comment 4 licheng.1212@gmail.com 2012-04-07 09:02:38 UTC
(In reply to comment #3)
> There is nothing to be done here since notused is used from the vtable for the
> class.
> 
> "objdump -d hello" does not show the vtables.

Yes,but why we can't remove the function notused() form vtable, since this function will never used.
Comment 5 Andrew Pinski 2012-04-07 18:08:21 UTC
(In reply to comment #4)
> Yes,but why we can't remove the function notused() form vtable, since this
> function will never used.

How can you tell?  Without the full exectuable and knowing the entry point, there is no way (-fwhole-program can help but it becomes a turning machine at that point).
Comment 6 licheng.1212@gmail.com 2012-04-09 11:14:56 UTC
(In reply to comment #5)
> (In reply to comment #4)
> > Yes,but why we can't remove the function notused() form vtable, since this
> > function will never used.
> 
> How can you tell?  Without the full exectuable and knowing the entry point,
> there is no way (-fwhole-program can help but it becomes a turning machine at
> that point).

class hello{
    public void notused()
    {
        System.out.println("not used");
    }
    public static void main(String argv[])
    {
        System.out.println("Hello");    
    }
}

this is a full execlutable program,and it hava only one entry point "main"!

and i test a c++ file,it will remove the unused funcitons "notused":
[licheng@Soft04 ~]$ cat hello.cpp 
#include <iostream>

class hello
{
public:
        hello()
        {
        }
        void notused()
        {
                std::cout << "not used";
        }
        void used()
        {
                std::cout << "used";
        }
        ~hello()
        {
        }
}hi;

int main()
{       hello t;
        t.used();
        return 0;
}
[licheng@Soft04 ~]$
Comment 7 Andrew Pinski 2012-04-09 19:40:41 UTC
(In reply to comment #6)
> and i test a c++ file,it will remove the unused funcitons "notused":

But that is not the same as there is no vtable for hello.
Try:
#include <iostream>

class hello
{
public:
        hello()
        {
        }
        virtual void notused()
        {
                std::cout << "not used";
        }
        virtual void used()
        {
                std::cout << "used";
        }
        virtual ~hello()
        {
        }
}hi;

int main()
{       hello t;
        t.used();
        return 0;
}

And see the result.