From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 22948 invoked by alias); 28 Jun 2006 20:17:19 -0000 Received: (qmail 22936 invoked by uid 22791); 28 Jun 2006 20:17:19 -0000 X-Spam-Check-By: sourceware.org Received: from shell4.bayarea.net (HELO shell4.bayarea.net) (209.128.82.1) by sourceware.org (qpsmtpd/0.31) with ESMTP; Wed, 28 Jun 2006 20:17:16 +0000 Received: from [192.168.20.7] (209-128-106-254.bayarea.net [209.128.106.254]) (authenticated bits=0) by shell4.bayarea.net (8.13.6/8.13.6) with ESMTP id k5SKHD8l021386; Wed, 28 Jun 2006 13:17:14 -0700 Message-ID: <44A2E3C9.90201@eagercon.com> Date: Wed, 28 Jun 2006 20:17:00 -0000 From: Michael Eager User-Agent: Thunderbird 1.5.0.4 (X11/20060614) MIME-Version: 1.0 To: Steve Kreyer CC: gcc-help@gcc.gnu.org Subject: Re: Symbols which were not used, still in binary References: <44A2DF65.8010704@web.de> In-Reply-To: <44A2DF65.8010704@web.de> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-IsSubscribed: yes Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2006-06/txt/msg00264.txt.bz2 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@eagercon.com 1960 Park Blvd., Palo Alto, CA 94306 650-325-8077