Re: Symbols which were not used, still in binary

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Steve Kreyer wrote:
Hi,

I have a question about an optimization issue.

I have compiled the follwing code with the command ''gcc file.c''

---------------------------------------------------------------
int add(int a, int b){
   return a + b;
}

int main(){
}
--------------------------------------------------------------

My question is:
Why does the symbol "add" appear in the outcoming binary file?
nm gives me:
--------------------------------------------------------------
redwing@euklid:~ $ nm a.out | grep add
08048324 T add
--------------------------------------------------------------

Neither is it used in this piece of code, nor is it compiled into an object file,
so in my opinion it should be optimized away by gcc.
So perhaps anybody can tell me whats wrong with my thought...

add is a global symbol.  GCC has no way of knowing whether or not
this object file will be linked with another file which contains
a reference to the function.  Arguably, the linker should know
that add is not referenced and could remove it, but linkers are not
usually able to slice and dice object files.

[If you declare add to be static, then GCC should be able
to eliminate it.  But it doesn't.]

Programs often contain functions which are designed
to be called by someone using a debugger but are otherwise
not referenced.

--
Michael Eager	 eager@xxxxxxxxxxxx
1960 Park Blvd., Palo Alto, CA 94306  650-325-8077

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux