This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Listing and removing unused objects
- From: Kazu Hirata <kazu at cs dot umass dot edu>
- To: AWLaFramboise at aol dot com
- Cc: gcc at gnu dot org
- Date: Sat, 25 Jan 2003 23:11:01 -0500 (EST)
- Subject: Re: Listing and removing unused objects
- References: <4D78BB07.4E21C91C.E8E247C1@aol.com>
Hi Aaron,
> 1) Is it possible
> to trivially use GCC to generate a list of symbols that are defined
> and used in a file, and imported (and presumably defined elsewhere)?
> I would like this so I can generate a list of symbols that are unused
> within an entire program build (unused functions, data).
>
> 2) Is there
> a way to cause GCC to not emit functions/data for a list of symbols?
> This would be useful in combination with the list of dead symbols from #1
> to eliminate these unused objects from the final executable.
>
> If GCC
> can not do this directly, is there some combination of shell scripting
> and usual UNIX development tools (binutils, etc) that could be used to
> accomplish it? If not, can someone advise me on how I might best modify
> GCC to facilitate this?
>
> Having found an unbeleivable amount of dead
> code in my executables, I've started taking considerable interest in
> trimming this fat. All compilers I have looked at do decidedly poorly in
> this department. I was actually suprised by how little interest there is
> in this, and in fact the only interest I have seen in the GNU development
> tools is the GNU ld --gc-sections option, which never works for this kind
> of thing in practical situations due to a combination of technicalities.
Do you mean that the combination of -Wl,-gc-sections,-static and
-ffunction-sections does not work for some reason? On my system
(RedHat 8.0 on AMD Athlon), the following works nicely.
unused.c
========
void
foo ()
{
}
int
main ()
{
return 0;
}
unused.sh
=========
#!/bin/sh
gcc -Wl,-gc-sections,-static -o unused unused.c
nm unused | sed -e "s|^...........||" > list1
gcc -ffunction-sections -Wl,-gc-sections,-static -o unused unused.c
nm unused | sed -e "s|^...........||" > list2
diff -u list1 list2 | grep ^- | grep -v -- --- | sed -e "s|^-||"
Kazu Hirata