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.
>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?
[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)
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.
(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.
(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).
(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 ~]$
(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.