public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Linking un-referenced function
@ 2006-05-22 23:31 Jason Lam
  2006-05-23  0:49 ` John Carter
  2006-05-23  0:53 ` John Carter
  0 siblings, 2 replies; 7+ messages in thread
From: Jason Lam @ 2006-05-22 23:31 UTC (permalink / raw)
  To: gcc-help

Hi,

I looked through the documents and couldn't find an
option to not link an un-referenced function into the
final image.  Does GCC support it?  Could someone
please help?

For example, I have two functions in a file.  

example.c

void dummy_1 (void)
{
    printf("dummy 1\n");
}

void dummy_2 (void)
{
    printf("dummy 2\n");
}

dummy_1() is referenced by others in a different file
but not dummy_2().  However, dummy_2 is also got
linked to the final image.  It looks like the compiler
links functions by the entire object file but not by
each function.  Is there a way to exclude linking
dummy_2() to the final image?

Thank you very much for your help.

Regards,

Jason

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Linking un-referenced function
  2006-05-22 23:31 Linking un-referenced function Jason Lam
@ 2006-05-23  0:49 ` John Carter
  2006-05-23  0:53 ` John Carter
  1 sibling, 0 replies; 7+ messages in thread
From: John Carter @ 2006-05-23  0:49 UTC (permalink / raw)
  To: Jason Lam; +Cc: gcc-help

On Mon, 22 May 2006, Jason Lam wrote:

> I looked through the documents and couldn't find an
> option to not link an un-referenced function into the
> final image.  Does GCC support it?  Could someone
> please help?

Tends to occur at the linker script level...

From the ld info file...

Input Section and Garbage Collection
....................................

When link-time garbage collection is in use (`--gc-sections'), it is
often useful to mark sections that should not be eliminated.  This is
accomplished by surrounding an input section's wildcard entry with
`KEEP()', as in `KEEP(*(.init))' or `KEEP(SORT_BY_NAME(*)(.ctors))'.





John Carter                             Phone : (64)(3) 358 6639
Tait Electronics                        Fax   : (64)(3) 359 4632
PO Box 1645 Christchurch                Email : john.carter@tait.co.nz
New Zealand

Carter's Clarification of Murphy's Law.

"Things only ever go right so that they may go more spectacularly wrong later."

From this principle, all of life and physics may be deduced.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Linking un-referenced function
  2006-05-22 23:31 Linking un-referenced function Jason Lam
  2006-05-23  0:49 ` John Carter
@ 2006-05-23  0:53 ` John Carter
  2006-05-23  8:58   ` Shriramana Sharma
  2006-05-23 19:35   ` Jason Lam
  1 sibling, 2 replies; 7+ messages in thread
From: John Carter @ 2006-05-23  0:53 UTC (permalink / raw)
  To: Jason Lam; +Cc: gcc-help

On Mon, 22 May 2006, Jason Lam wrote:

> I looked through the documents and couldn't find an
> option to not link an un-referenced function into the
> final image.  Does GCC support it?  Could someone
> please help?

Sorry, I got confuddled...

Use
   `--gc-sections'
option on linker and...


`-ffunction-sections'
`-fdata-sections'
      Place each function or data item into its own section in the output
      file if the target supports arbitrary sections.  The name of the
      function or the name of the data item determines the section's name
      in the output file.

      Use these options on systems where the linker can perform
      optimizations to improve locality of reference in the instruction
      space.  Most systems using the ELF object format and SPARC
      processors running Solaris 2 have linkers with such optimizations.
      AIX may have these optimizations in the future.

      Only use these options when there are significant benefits from
      doing so.  When you specify these options, the assembler and
      linker will create larger object and executable files and will
      also be slower.  You will not be able to use `gprof' on all
      systems if you specify this option and you may have problems with
      debugging if you specify both this option and `-g'.


John Carter                             Phone : (64)(3) 358 6639
Tait Electronics                        Fax   : (64)(3) 359 4632
PO Box 1645 Christchurch                Email : john.carter@tait.co.nz
New Zealand

Carter's Clarification of Murphy's Law.

"Things only ever go right so that they may go more spectacularly wrong later."

From this principle, all of life and physics may be deduced.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Linking un-referenced function
  2006-05-23  0:53 ` John Carter
@ 2006-05-23  8:58   ` Shriramana Sharma
  2006-05-23 19:35   ` Jason Lam
  1 sibling, 0 replies; 7+ messages in thread
From: Shriramana Sharma @ 2006-05-23  8:58 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 609 bytes --]

Tuesday 23 May 2006 06:23 samaye, John Carter alekhiit:
> Use
>    `--gc-sections'
> option on linker and...
> `-ffunction-sections'
> `-fdata-sections'

Thanks. I had been searching for this for quite some time. I just added the 
options:

 -fdata-sections -ffunction-sections -Wl,--gc-sections

to my gcc command and it worked! (The "-Wl," part [including the comma] is 
used to denote that the option is for the linker but it is passed via the 
compiler.)

-- 

Tux #395953 resides at http://samvit.org
playing with KDE 3.51 on SUSE Linux 10.1
$ date [] CCE +2006-05-23 W21-2 UTC+0530

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Linking un-referenced function
  2006-05-23  0:53 ` John Carter
  2006-05-23  8:58   ` Shriramana Sharma
@ 2006-05-23 19:35   ` Jason Lam
  1 sibling, 0 replies; 7+ messages in thread
From: Jason Lam @ 2006-05-23 19:35 UTC (permalink / raw)
  To: John Carter; +Cc: gcc-help

Thanks for the quick response, John.

It looks like these options will arrange the functions
to different sections in the final image but it still
doesn't exclude them.  Am I missing something?

Thanks,

Jason

--- John Carter <john.carter@tait.co.nz> wrote:

> On Mon, 22 May 2006, Jason Lam wrote:
> 
> > I looked through the documents and couldn't find
> an
> > option to not link an un-referenced function into
> the
> > final image.  Does GCC support it?  Could someone
> > please help?
> 
> Sorry, I got confuddled...
> 
> Use
>    `--gc-sections'
> option on linker and...
> 
> 
> `-ffunction-sections'
> `-fdata-sections'
>       Place each function or data item into its own
> section in the output
>       file if the target supports arbitrary
> sections.  The name of the
>       function or the name of the data item
> determines the section's name
>       in the output file.
> 
>       Use these options on systems where the linker
> can perform
>       optimizations to improve locality of reference
> in the instruction
>       space.  Most systems using the ELF object
> format and SPARC
>       processors running Solaris 2 have linkers with
> such optimizations.
>       AIX may have these optimizations in the
> future.
> 
>       Only use these options when there are
> significant benefits from
>       doing so.  When you specify these options, the
> assembler and
>       linker will create larger object and
> executable files and will
>       also be slower.  You will not be able to use
> `gprof' on all
>       systems if you specify this option and you may
> have problems with
>       debugging if you specify both this option and
> `-g'.
> 
> 
> John Carter                             Phone :
> (64)(3) 358 6639
> Tait Electronics                        Fax   :
> (64)(3) 359 4632
> PO Box 1645 Christchurch                Email :
> john.carter@tait.co.nz
> New Zealand
> 
> Carter's Clarification of Murphy's Law.
> 
> "Things only ever go right so that they may go more
> spectacularly wrong later."
> 
> From this principle, all of life and physics may be
> deduced.
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: Linking un-referenced function
  2006-05-23 21:11 Meissner, Michael
@ 2006-05-23 23:29 ` Jason Lam
  0 siblings, 0 replies; 7+ messages in thread
From: Jason Lam @ 2006-05-23 23:29 UTC (permalink / raw)
  To: Meissner, Michael, John Carter; +Cc: gcc-help

This does the trick!  Thank you very much for your
help , Michael and John.

Jason

--- "Meissner, Michael" <michael.meissner@amd.com>
wrote:

> The linker option --gc-sections (ie, from the GCC
> command line:
> 	-ffunction-sections -fdata-sections
> -Wl,--gc-sections
> 
> Will do the collection.  You need the two -f options
> so that the
> compiler puts each function/data item into a
> separate section, and then
> the --gc-sections option will enable the linker to
> remove any section
> that is not referenced.
> 
> --
> Michael Meissner
> AMD, MS 83-29
> 90 Central Street
> Boxborough, MA 01719
> 
> -----Original Message-----
> From: gcc-help-owner@gcc.gnu.org
> [mailto:gcc-help-owner@gcc.gnu.org] On
> Behalf Of Jason Lam
> Sent: Tuesday, May 23, 2006 3:35 PM
> To: John Carter
> Cc: gcc-help@gcc.gnu.org
> Subject: Re: Linking un-referenced function
> 
> Thanks for the quick response, John.
> 
> It looks like these options will arrange the
> functions
> to different sections in the final image but it
> still
> doesn't exclude them.  Am I missing something?
> 
> Thanks,
> 
> Jason
> 
> --- John Carter <john.carter@tait.co.nz> wrote:
> 
> > On Mon, 22 May 2006, Jason Lam wrote:
> > 
> > > I looked through the documents and couldn't find
> > an
> > > option to not link an un-referenced function
> into
> > the
> > > final image.  Does GCC support it?  Could
> someone
> > > please help?
> > 
> > Sorry, I got confuddled...
> > 
> > Use
> >    `--gc-sections'
> > option on linker and...
> > 
> > 
> > `-ffunction-sections'
> > `-fdata-sections'
> >       Place each function or data item into its
> own
> > section in the output
> >       file if the target supports arbitrary
> > sections.  The name of the
> >       function or the name of the data item
> > determines the section's name
> >       in the output file.
> > 
> >       Use these options on systems where the
> linker
> > can perform
> >       optimizations to improve locality of
> reference
> > in the instruction
> >       space.  Most systems using the ELF object
> > format and SPARC
> >       processors running Solaris 2 have linkers
> with
> > such optimizations.
> >       AIX may have these optimizations in the
> > future.
> > 
> >       Only use these options when there are
> > significant benefits from
> >       doing so.  When you specify these options,
> the
> > assembler and
> >       linker will create larger object and
> > executable files and will
> >       also be slower.  You will not be able to use
> > `gprof' on all
> >       systems if you specify this option and you
> may
> > have problems with
> >       debugging if you specify both this option
> and
> > `-g'.
> > 
> > 
> > John Carter                             Phone :
> > (64)(3) 358 6639
> > Tait Electronics                        Fax   :
> > (64)(3) 359 4632
> > PO Box 1645 Christchurch                Email :
> > john.carter@tait.co.nz
> > New Zealand
> > 
> > Carter's Clarification of Murphy's Law.
> > 
> > "Things only ever go right so that they may go
> more
> > spectacularly wrong later."
> > 
> > From this principle, all of life and physics may
> be
> > deduced.
> > 
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam
> protection around 
> http://mail.yahoo.com 
> 
> 
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: Linking un-referenced function
@ 2006-05-23 21:11 Meissner, Michael
  2006-05-23 23:29 ` Jason Lam
  0 siblings, 1 reply; 7+ messages in thread
From: Meissner, Michael @ 2006-05-23 21:11 UTC (permalink / raw)
  To: Jason Lam, John Carter; +Cc: gcc-help

The linker option --gc-sections (ie, from the GCC command line:
	-ffunction-sections -fdata-sections -Wl,--gc-sections

Will do the collection.  You need the two -f options so that the
compiler puts each function/data item into a separate section, and then
the --gc-sections option will enable the linker to remove any section
that is not referenced.

--
Michael Meissner
AMD, MS 83-29
90 Central Street
Boxborough, MA 01719

-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
Behalf Of Jason Lam
Sent: Tuesday, May 23, 2006 3:35 PM
To: John Carter
Cc: gcc-help@gcc.gnu.org
Subject: Re: Linking un-referenced function

Thanks for the quick response, John.

It looks like these options will arrange the functions
to different sections in the final image but it still
doesn't exclude them.  Am I missing something?

Thanks,

Jason

--- John Carter <john.carter@tait.co.nz> wrote:

> On Mon, 22 May 2006, Jason Lam wrote:
> 
> > I looked through the documents and couldn't find
> an
> > option to not link an un-referenced function into
> the
> > final image.  Does GCC support it?  Could someone
> > please help?
> 
> Sorry, I got confuddled...
> 
> Use
>    `--gc-sections'
> option on linker and...
> 
> 
> `-ffunction-sections'
> `-fdata-sections'
>       Place each function or data item into its own
> section in the output
>       file if the target supports arbitrary
> sections.  The name of the
>       function or the name of the data item
> determines the section's name
>       in the output file.
> 
>       Use these options on systems where the linker
> can perform
>       optimizations to improve locality of reference
> in the instruction
>       space.  Most systems using the ELF object
> format and SPARC
>       processors running Solaris 2 have linkers with
> such optimizations.
>       AIX may have these optimizations in the
> future.
> 
>       Only use these options when there are
> significant benefits from
>       doing so.  When you specify these options, the
> assembler and
>       linker will create larger object and
> executable files and will
>       also be slower.  You will not be able to use
> `gprof' on all
>       systems if you specify this option and you may
> have problems with
>       debugging if you specify both this option and
> `-g'.
> 
> 
> John Carter                             Phone :
> (64)(3) 358 6639
> Tait Electronics                        Fax   :
> (64)(3) 359 4632
> PO Box 1645 Christchurch                Email :
> john.carter@tait.co.nz
> New Zealand
> 
> Carter's Clarification of Murphy's Law.
> 
> "Things only ever go right so that they may go more
> spectacularly wrong later."
> 
> From this principle, all of life and physics may be
> deduced.
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2006-05-23 23:29 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-05-22 23:31 Linking un-referenced function Jason Lam
2006-05-23  0:49 ` John Carter
2006-05-23  0:53 ` John Carter
2006-05-23  8:58   ` Shriramana Sharma
2006-05-23 19:35   ` Jason Lam
2006-05-23 21:11 Meissner, Michael
2006-05-23 23:29 ` Jason Lam

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).