public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* PATCH: makes --gc-sections compatible with -r/-q
@ 2008-01-09  0:03 Tristan Gingold
  2008-01-09  5:38 ` Daniel Jacobowitz
  2008-01-10  8:04 ` Alan Modra
  0 siblings, 2 replies; 27+ messages in thread
From: Tristan Gingold @ 2008-01-09  0:03 UTC (permalink / raw)
  To: binutils

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

Hi,

this simple patch removes the incompatibility between --gc-sections  
and -r or -q.
This feature is useful for VxWorks (whose loadable binaries are  
compiled with -r or -q) as well as
for EFI modules.

We (= AdaCore) are using it for a long time on VxWorks as we are  
compiling the Gnat run time with
--gc-sections.

This patch was test only for ppc-vxworks and i586-linux targets (on a  
i586-linux host).

I don't know if other targets will support it out of the box.  But to  
avoid spurious failures the two
tests are enabled only on a few targets (ppc-vxworks, x86-vxworks and  
x86-linux).

Tristan.

[-- Attachment #2: ld-gc.diffs --]
[-- Type: application/octet-stream, Size: 7043 bytes --]

bfd:
2008-01-08  Eric Botcazou  <ebotcazou@adacore.com>

	* elf32-ppc.c (ppc_elf_gc_sweep_hook): Add missing guard.
	* elflink.c (bfd_elf_gc_sections): Do not punt on relocatable output
	or executable output with relocations.

ld:
2008-01-08  Eric Botcazou  <ebotcazou@adacore.com>

	* ld.texinfo (Options): Document that --gc-sections is compatible
	with -r and -q.
	* ldmain.c (main): Do not error out if -r and --gc-sections.

ld/testsuite:
2008-01-08  Tristan Gingold  <gingold@adacore.com>

	* lib/ld-lib.exp (check_gc_sections_available): Now available on
	VxWorks.
	* ld-gc: New directory for testing --gc-sections.
	* ld-gc/gc.c: New file.
	* ld-gc/gc.exp: New file.


*** bfd/elf32-ppc.c	8 Nov 2007 13:51:06 -0000	1.224
--- bfd/elf32-ppc.c	8 Jan 2008 13:53:51 -0000
*************** ppc_elf_gc_sweep_hook (bfd *abfd,
*** 4017,4023 ****
  	    {
  	      bfd_vma addend = r_type == R_PPC_PLTREL24 ? rel->r_addend : 0;
  	      struct plt_entry *ent = find_plt_ent (h, got2, addend);
! 	      if (ent->plt.refcount > 0)
  		ent->plt.refcount -= 1;
  	    }
  	  break;
--- 4017,4023 ----
  	    {
  	      bfd_vma addend = r_type == R_PPC_PLTREL24 ? rel->r_addend : 0;
  	      struct plt_entry *ent = find_plt_ent (h, got2, addend);
! 	      if (ent && ent->plt.refcount > 0)
  		ent->plt.refcount -= 1;
  	    }
  	  break;
*** bfd/elflink.c	7 Jan 2008 16:45:35 -0000	1.291
--- bfd/elflink.c	8 Jan 2008 13:53:57 -0000
*************** bfd_elf_gc_sections (bfd *abfd, struct b
*** 11474,11481 ****
    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  
    if (!bed->can_gc_sections
-       || info->relocatable
-       || info->emitrelocations
        || !is_elf_hash_table (info->hash))
      {
        (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
--- 11474,11479 ----
*** ld/ld.texinfo	1 Oct 2007 09:54:57 -0000	1.203
--- ld/ld.texinfo	8 Jan 2008 13:54:04 -0000
*************** it ends in a @code{.exe} suffix.
*** 1280,1287 ****
  @item --gc-sections
  @itemx --no-gc-sections
  Enable garbage collection of unused input sections.  It is ignored on
! targets that do not support this option.  This option is not compatible
! with @samp{-r} or @samp{--emit-relocs}. The default behaviour (of not
  performing this garbage collection) can be restored by specifying
  @samp{--no-gc-sections} on the command line.
  
--- 1280,1286 ----
  @item --gc-sections
  @itemx --no-gc-sections
  Enable garbage collection of unused input sections.  It is ignored on
! targets that do not support this option.  The default behaviour (of not
  performing this garbage collection) can be restored by specifying
  @samp{--no-gc-sections} on the command line.
  
*** ld/ldmain.c	6 Jul 2007 14:09:41 -0000	1.125
--- ld/ldmain.c	8 Jan 2008 13:54:05 -0000
*************** main (int argc, char **argv)
*** 292,300 ****
  
    if (link_info.relocatable)
      {
!       if (link_info.gc_sections)
! 	einfo ("%P%F: --gc-sections and -r may not be used together\n");
!       else if (command_line.relax)
  	einfo (_("%P%F: --relax and -r may not be used together\n"));
        if (link_info.shared)
  	einfo (_("%P%F: -r and -shared may not be used together\n"));
--- 292,298 ----
  
    if (link_info.relocatable)
      {
!       if (command_line.relax)
  	einfo (_("%P%F: --relax and -r may not be used together\n"));
        if (link_info.shared)
  	einfo (_("%P%F: -r and -shared may not be used together\n"));
*** ld/testsuite/lib/ld-lib.exp	20 Nov 2007 15:08:23 -0000	1.52
--- ld/testsuite/lib/ld-lib.exp	8 Jan 2008 13:54:06 -0000
*************** proc check_gc_sections_available { } {
*** 1559,1572 ****
  	    return 0
  	}
  
- 	# VxWorks kernel modules are relocatable objects linked with -r,
- 	# while RTP executables are linked with -q (--emit-relocs).
- 	# Both of these options are incompatible with --gc-sections.
- 	if { [istarget *-*-vxworks*] } {
- 	    set gc_sections_available_saved 0
- 	    return 0
- 	}
- 
  	# Check if the ld used by gcc supports --gc-sections.
  	set ld_output [remote_exec host $ld "--help"]
  	if { [ string first "--gc-sections" $ld_output ] >= 0 } {
--- 1559,1564 ----
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- ld/testsuite/ld-gc/gc.c	8 Jan 2008 13:55:04 -0000
***************
*** 0 ****
--- 1,20 ----
+ int unused_var = 7;
+ int used_var = 7;
+ 
+ int
+ unused_func (int v)
+ {
+   return 3 * unused_var;
+ }
+ 
+ int
+ used_func (int v)
+ {
+   return 2 * used_var;
+ }
+ 
+ int
+ main (void)
+ {
+   return used_func (5);
+ }
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- ld/testsuite/ld-gc/gc.exp	8 Jan 2008 13:55:04 -0000
***************
*** 0 ****
--- 1,77 ----
+ # Expect script for ld-gc tests
+ #   Copyright 2008
+ #   Free Software Foundation, Inc.
+ #
+ # This file is part of the GNU Binutils.
+ #
+ # This program is free software; you can redistribute it and/or modify
+ # it under the terms of the GNU General Public License as published by
+ # the Free Software Foundation; either version 3 of the License, or
+ # (at your option) any later version.
+ #
+ # This program is distributed in the hope that it will be useful,
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ # GNU General Public License for more details.
+ #
+ # You should have received a copy of the GNU General Public License
+ # along with this program; if not, write to the Free Software
+ # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+ # MA 02110-1301, USA.
+ 
+ # These tests require --gc-sections
+ if ![check_gc_sections_available] {
+     return
+ }
+ 
+ set cflags "-ffunction-sections -fdata-sections"
+ set objfile "tmpdir/gc.o"
+ 
+ ld_compile "$CC -c $CFLAGS $cflags" $srcdir/$subdir/gc.c $objfile
+ 
+ proc test_gc { testname filename linker ldflags} {
+     global nm
+     global srcdir
+     global subdir
+     global nm_output
+     global objfile
+ 
+     set outfile "tmpdir/$filename"
+ 
+     if ![ld_simple_link $linker $outfile "-L$srcdir/$subdir $ldflags $objfile"] {
+ 	    fail $testname
+ 	return
+     }
+     if ![ld_nm $nm "" $outfile] {
+ 	unresolved $testname
+ 	return
+     }
+     if {![info exists nm_output(used_func)] \
+ 	    || ![info exists nm_output(used_var)]} {
+ 	send_log "used sections do not exist\n"
+ 	verbose "used sections do not exist"
+ 	fail $testname
+ 	return
+     }
+     if {[info exists nm_output(unused_func)] \
+ 	    || [info exists nm_output(unused_var)]} {
+ 	send_log "unused section still here\n"
+ 	verbose "unused section still here"
+ 	fail $testname
+ 	return
+     }
+     pass $testname
+ }
+ 
+ test_gc "Check --gc-section" "gcexe" $ld "--gc-sections -e main"
+ 
+ # Only a few platform have been tested for the -q/-r tests.
+ if { ![istarget i?86-*-vxworks4*] \
+ 	 && ![istarget powerpc*-*-vxworks*] \
+ 	 && ![istarget i?86-*-linux*] } {
+     return
+ }
+ 
+ test_gc "Check --gc-section/-q" "gcrexe" $ld "--gc-sections -q -e main"
+ test_gc "Check --gc-section/-r" "gcrel" $ld "-r --gc-sections -e main"
+ 

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

* Re: PATCH: makes --gc-sections compatible with -r/-q
  2008-01-09  0:03 PATCH: makes --gc-sections compatible with -r/-q Tristan Gingold
@ 2008-01-09  5:38 ` Daniel Jacobowitz
  2008-01-09  6:09   ` Tristan Gingold
  2008-01-10  8:04 ` Alan Modra
  1 sibling, 1 reply; 27+ messages in thread
From: Daniel Jacobowitz @ 2008-01-09  5:38 UTC (permalink / raw)
  To: Tristan Gingold; +Cc: binutils

On Tue, Jan 08, 2008 at 03:21:28PM +0100, Tristan Gingold wrote:
> Hi,
>
> this simple patch removes the incompatibility between --gc-sections and -r 
> or -q.
> This feature is useful for VxWorks (whose loadable binaries are compiled 
> with -r or -q) as well as
> for EFI modules.
>
> We (= AdaCore) are using it for a long time on VxWorks as we are  
> compiling the Gnat run time with
> --gc-sections.

What does it keep?  In a linked object it's obvious which symbols are
externally visible.

-- 
Daniel Jacobowitz
CodeSourcery

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

* Re: PATCH: makes --gc-sections compatible with -r/-q
  2008-01-09  5:38 ` Daniel Jacobowitz
@ 2008-01-09  6:09   ` Tristan Gingold
  2008-01-09 15:44     ` Thiemo Seufer
  2008-01-09 15:58     ` Daniel Jacobowitz
  0 siblings, 2 replies; 27+ messages in thread
From: Tristan Gingold @ 2008-01-09  6:09 UTC (permalink / raw)
  To: Daniel Jacobowitz; +Cc: binutils


On Jan 8, 2008, at 3:24 PM, Daniel Jacobowitz wrote:

> What does it keep?  In a linked object it's obvious which symbols are
> externally visible.

For partial link (-r), you should specify the root of symbols kept  
with the -e option.
Sorry, I forgot to mention that.

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

* Re: PATCH: makes --gc-sections compatible with -r/-q
  2008-01-09  6:09   ` Tristan Gingold
@ 2008-01-09 15:44     ` Thiemo Seufer
  2008-01-09 15:58     ` Daniel Jacobowitz
  1 sibling, 0 replies; 27+ messages in thread
From: Thiemo Seufer @ 2008-01-09 15:44 UTC (permalink / raw)
  To: Tristan Gingold; +Cc: Daniel Jacobowitz, binutils

Tristan Gingold wrote:
>
> On Jan 8, 2008, at 3:24 PM, Daniel Jacobowitz wrote:
>
>> What does it keep?  In a linked object it's obvious which symbols are
>> externally visible.
>
> For partial link (-r), you should specify the root of symbols kept with the 
> -e option.
> Sorry, I forgot to mention that.

Please add that to the documentation in you patch.


Thiemo

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

* Re: PATCH: makes --gc-sections compatible with -r/-q
  2008-01-09  6:09   ` Tristan Gingold
  2008-01-09 15:44     ` Thiemo Seufer
@ 2008-01-09 15:58     ` Daniel Jacobowitz
  2008-01-09 16:26       ` Tristan Gingold
  1 sibling, 1 reply; 27+ messages in thread
From: Daniel Jacobowitz @ 2008-01-09 15:58 UTC (permalink / raw)
  To: Tristan Gingold; +Cc: binutils

On Tue, Jan 08, 2008 at 03:40:46PM +0100, Tristan Gingold wrote:
>
> On Jan 8, 2008, at 3:24 PM, Daniel Jacobowitz wrote:
>
>> What does it keep?  In a linked object it's obvious which symbols are
>> externally visible.
>
> For partial link (-r), you should specify the root of symbols kept with 
> the -e option.
> Sorry, I forgot to mention that.

Patch is definitely not OK without that in the manual.

-- 
Daniel Jacobowitz
CodeSourcery

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

* Re: PATCH: makes --gc-sections compatible with -r/-q
  2008-01-09 15:58     ` Daniel Jacobowitz
@ 2008-01-09 16:26       ` Tristan Gingold
  2008-01-09 16:32         ` H.J. Lu
  0 siblings, 1 reply; 27+ messages in thread
From: Tristan Gingold @ 2008-01-09 16:26 UTC (permalink / raw)
  To: Daniel Jacobowitz; +Cc: binutils


On Jan 8, 2008, at 4:01 PM, Daniel Jacobowitz wrote:

> Patch is definitely not OK without that in the manual.

Sure.

I propose this entry.  Tell me if the english is not good enough.

@item --gc-sections
@itemx --no-gc-sections
Enable garbage collection of unused input sections.  It is ignored on
targets that do not support this option.  The default behaviour (of not
performing this garbage collection) can be restored by specifying
@samp{--no-gc-sections} on the command line.

This option can be set when doing a partial link (enabled with option
@samp{-r}).  In this case the root of symbols kept must be specified  
with
the option @samp{-e}.

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

* Re: PATCH: makes --gc-sections compatible with -r/-q
  2008-01-09 16:26       ` Tristan Gingold
@ 2008-01-09 16:32         ` H.J. Lu
  2008-01-09 16:44           ` Tristan Gingold
  0 siblings, 1 reply; 27+ messages in thread
From: H.J. Lu @ 2008-01-09 16:32 UTC (permalink / raw)
  To: Tristan Gingold; +Cc: Daniel Jacobowitz, binutils

On Tue, Jan 08, 2008 at 04:15:28PM +0100, Tristan Gingold wrote:
> This option can be set when doing a partial link (enabled with option
> @samp{-r}).  In this case the root of symbols kept must be specified with
> the option @samp{-e}.

Did you enforce it in linker?


H.J.

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

* Re: PATCH: makes --gc-sections compatible with -r/-q
  2008-01-09 16:32         ` H.J. Lu
@ 2008-01-09 16:44           ` Tristan Gingold
  2008-01-10  0:45             ` H.J. Lu
  0 siblings, 1 reply; 27+ messages in thread
From: Tristan Gingold @ 2008-01-09 16:44 UTC (permalink / raw)
  To: H.J. Lu; +Cc: Daniel Jacobowitz, binutils


On Jan 8, 2008, at 4:34 PM, H.J. Lu wrote:

> On Tue, Jan 08, 2008 at 04:15:28PM +0100, Tristan Gingold wrote:
>> This option can be set when doing a partial link (enabled with option
>> @samp{-r}).  In this case the root of symbols kept must be  
>> specified with
>> the option @samp{-e}.
>
> Did you enforce it in linker?

No, there is always a default entry set by the script (usually  
start).  I think it's simpler to have the
same semantic as normal link.

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

* Re: PATCH: makes --gc-sections compatible with -r/-q
  2008-01-09 16:44           ` Tristan Gingold
@ 2008-01-10  0:45             ` H.J. Lu
  2008-01-10  1:12               ` Tristan Gingold
  0 siblings, 1 reply; 27+ messages in thread
From: H.J. Lu @ 2008-01-10  0:45 UTC (permalink / raw)
  To: Tristan Gingold; +Cc: Daniel Jacobowitz, binutils

On Tue, Jan 08, 2008 at 04:58:01PM +0100, Tristan Gingold wrote:
>
> On Jan 8, 2008, at 4:34 PM, H.J. Lu wrote:
>
>> On Tue, Jan 08, 2008 at 04:15:28PM +0100, Tristan Gingold wrote:
>>> This option can be set when doing a partial link (enabled with option
>>> @samp{-r}).  In this case the root of symbols kept must be specified with
>>> the option @samp{-e}.
>>
>> Did you enforce it in linker?
>
> No, there is always a default entry set by the script (usually start).  I 
> think it's simpler to have the
> same semantic as normal link.

The current linker does

[hjl@gnu-6 tmp]$ ld -r --gc-sections y.o
ld: --gc-sections and -r may not be used together
[hjl@gnu-6 tmp]$ 

So are you proposing linker will now generate incorrect output
siliently instead? I don't like it.


H.J.

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

* Re: PATCH: makes --gc-sections compatible with -r/-q
  2008-01-10  0:45             ` H.J. Lu
@ 2008-01-10  1:12               ` Tristan Gingold
  0 siblings, 0 replies; 27+ messages in thread
From: Tristan Gingold @ 2008-01-10  1:12 UTC (permalink / raw)
  To: H.J. Lu; +Cc: Daniel Jacobowitz, binutils


On Jan 8, 2008, at 5:45 PM, H.J. Lu wrote:

> The current linker does
>
> [hjl@gnu-6 tmp]$ ld -r --gc-sections y.o
> ld: --gc-sections and -r may not be used together
> [hjl@gnu-6 tmp]$
>
> So are you proposing linker will now generate incorrect output
> siliently instead? I don't like it.

I understand your point.

Do you want --gc-sections -r requires a -e ?
As a consequence, ENTRY in linker script won't be allowed.

As I don't last the consequence, we may also remove the ENTRY command  
in .xr scripts.

Thoughts ?

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

* Re: PATCH: makes --gc-sections compatible with -r/-q
  2008-01-09  0:03 PATCH: makes --gc-sections compatible with -r/-q Tristan Gingold
  2008-01-09  5:38 ` Daniel Jacobowitz
@ 2008-01-10  8:04 ` Alan Modra
  2008-01-10 14:50   ` PATCH [v2]: " Tristan Gingold
  1 sibling, 1 reply; 27+ messages in thread
From: Alan Modra @ 2008-01-10  8:04 UTC (permalink / raw)
  To: Tristan Gingold; +Cc: binutils

On Tue, Jan 08, 2008 at 03:21:28PM +0100, Tristan Gingold wrote:
> this simple patch removes the incompatibility between --gc-sections  
> and -r or -q.
> This feature is useful for VxWorks (whose loadable binaries are  
> compiled with -r or -q) as well as
> for EFI modules.
> 
> We (= AdaCore) are using it for a long time on VxWorks as we are  
> compiling the Gnat run time with
> --gc-sections.
> 
> This patch was test only for ppc-vxworks and i586-linux targets (on a  
> i586-linux host).

Not just only tested on ppc and i586, but only written for ppc and
i586.  Not OK without some more work.

You need to modify gc_sweep_hook to cope with -r for all targets.
Also, Eric's change, "Add missing guard", is not the proper fix for
ppc_elf_gc_sweep_hook.  You should instead exit ppc_elf_gc_sweep_hook
immediately if -r, because that is what ppc_elf_check_relocs does.

-- 
Alan Modra
Australia Development Lab, IBM

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

* PATCH [v2]: makes --gc-sections compatible with -r/-q
  2008-01-10  8:04 ` Alan Modra
@ 2008-01-10 14:50   ` Tristan Gingold
  2008-01-11  1:16     ` Alan Modra
  0 siblings, 1 reply; 27+ messages in thread
From: Tristan Gingold @ 2008-01-10 14:50 UTC (permalink / raw)
  To: binutils; +Cc: Alan Modra

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

> You need to modify gc_sweep_hook to cope with -r for all targets.
> Also, Eric's change, "Add missing guard", is not the proper fix for
> ppc_elf_gc_sweep_hook.  You should instead exit ppc_elf_gc_sweep_hook
> immediately if -r, because that is what ppc_elf_check_relocs does.

Ok, here is the new version of the patch.

* makes all gc_sweep_hook coherent with check_relocs (ie exit  
immediately if -r)
* --gc-section -r requires a non-default entry point
* document in --gc-section the use of -e

Tristan.

[-- Attachment #2: ld-gc.diffs2 --]
[-- Type: application/octet-stream, Size: 20546 bytes --]

bfd:
2008-01-09  Tristan Gingold  <gingold@adacore.com>
	    Eric Botcazou  <ebotcazou@adacore.com>

	* elf32-ppc.c (ppc_elf_gc_sweep_hook): Exit early if generating a
	relocatable.
	* elf32-arm.c (elf32_arm_gc_sweep_hook): Likewise.
	* elf32-cris.c (cris_elf_gc_sweep_hook): Likewise.
	* elf32-hppa.c (elf32_hppa_gc_sweep_hook): Likewise.
	* elf32-i386.c (elf_i386_gc_sweep_hook): Likewise.
	* elf32-m32r.c (m32r_elf_gc_sweep_hook): Likewise.
	* elf32-m68k.c (elf_m68k_gc_sweep_hook): Likewise.
	* elf32-s390.c (elf_s390_gc_sweep_hook): Likewise.
	* elf32-sh.c (sh_elf_gc_sweep_hook): Likewise.
	* elf32-vax.c (elf_vax_gc_sweep_hook): Likewise.
	* elf32-xtensa.c (elf_xtensa_gc_sweep_hook): Likewise.
	* elf64-x86-64.c (elf64_x86_64_gc_sweep_hook): Likewise.
	* elf64-s390.c (elf_s390_gc_sweep_hook): Likewise.
	* elf64-ppc.c (ppc64_elf_gc_sweep_hook): Likewise.
	* elfxx-mips.c (_bfd_mips_elf_gc_sweep_hook): Likewise.
	* elfxx-sparc.c (_bfd_sparc_elf_gc_sweep_hook): Likewise.
	* elflink.c (bfd_elf_gc_sections): Do not punt on relocatable output
	or executable output with relocations.

ld:
2008-01-09  Tristan Gingold  <gingold@adacore.com>
	    Eric Botcazou  <ebotcazou@adacore.com>

	* ldlang.c (lang_end): Warns if the entry point is not found when
	--gc-sections.
	Emit an error if no entry point is specified when --gc-sections -r.
	* ld.texinfo (Options): Document that --gc-sections is compatible
	with -r and -q.
	* ldmain.c (main): Do not error out if -r and --gc-sections.
	* scripttempl/elf.sc: Emit ENTRY command only if relocating.

ld/testsuite:
2008-01-09  Tristan Gingold  <gingold@adacore.com>

	* lib/ld-lib.exp (check_gc_sections_available): Now available on
	VxWorks.
	* ld-gc: New directory for testing --gc-sections.
	* ld-gc/gc.c: New file.
	* ld-gc/gc.exp: New file.
	* ld-gc/noent.s: New file.
	* ld-gc/noent.d: New file.


Index: bfd/elf32-arm.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-arm.c,v
retrieving revision 1.131
diff -c -r1.131 elf32-arm.c
*** bfd/elf32-arm.c	29 Nov 2007 08:14:22 -0000	1.131
--- bfd/elf32-arm.c	9 Jan 2008 14:58:29 -0000
***************
*** 7351,7356 ****
--- 7351,7359 ----
    const Elf_Internal_Rela *rel, *relend;
    struct elf32_arm_link_hash_table * globals;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    globals = elf32_arm_hash_table (info);
  
    elf_section_data (sec)->local_dynrel = NULL;
Index: bfd/elf32-cris.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-cris.c,v
retrieving revision 1.84
diff -c -r1.84 elf32-cris.c
*** bfd/elf32-cris.c	28 Sep 2007 08:43:45 -0000	1.84
--- bfd/elf32-cris.c	9 Jan 2008 14:58:30 -0000
***************
*** 1986,1991 ****
--- 1986,1994 ----
    asection *sgot;
    asection *srelgot;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    dynobj = elf_hash_table (info)->dynobj;
    if (dynobj == NULL)
      return TRUE;
Index: bfd/elf32-hppa.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-hppa.c,v
retrieving revision 1.160
diff -c -r1.160 elf32-hppa.c
*** bfd/elf32-hppa.c	31 Dec 2007 11:01:54 -0000	1.160
--- bfd/elf32-hppa.c	9 Jan 2008 14:58:32 -0000
***************
*** 1622,1627 ****
--- 1622,1630 ----
    bfd_signed_vma *local_plt_refcounts;
    const Elf_Internal_Rela *rela, *relend;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    elf_section_data (sec)->local_dynrel = NULL;
  
    symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
Index: bfd/elf32-i386.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-i386.c,v
retrieving revision 1.183
diff -c -r1.183 elf32-i386.c
*** bfd/elf32-i386.c	8 Nov 2007 13:51:06 -0000	1.183
--- bfd/elf32-i386.c	9 Jan 2008 14:58:33 -0000
***************
*** 1599,1604 ****
--- 1599,1607 ----
    bfd_signed_vma *local_got_refcounts;
    const Elf_Internal_Rela *rel, *relend;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    elf_section_data (sec)->local_dynrel = NULL;
  
    symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
Index: bfd/elf32-m32r.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-m32r.c,v
retrieving revision 1.90
diff -c -r1.90 elf32-m32r.c
*** bfd/elf32-m32r.c	28 Sep 2007 08:43:45 -0000	1.90
--- bfd/elf32-m32r.c	9 Jan 2008 14:58:34 -0000
***************
*** 3654,3659 ****
--- 3654,3662 ----
    bfd_signed_vma *local_got_refcounts;
    const Elf_Internal_Rela *rel, *relend;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    elf_section_data (sec)->local_dynrel = NULL;
  
    symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
Index: bfd/elf32-m68k.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-m68k.c,v
retrieving revision 1.105
diff -c -r1.105 elf32-m68k.c
*** bfd/elf32-m68k.c	26 Sep 2007 13:45:32 -0000	1.105
--- bfd/elf32-m68k.c	9 Jan 2008 14:58:35 -0000
***************
*** 1093,1098 ****
--- 1093,1101 ----
    asection *sgot;
    asection *srelgot;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    dynobj = elf_hash_table (info)->dynobj;
    if (dynobj == NULL)
      return TRUE;
Index: bfd/elf32-ppc.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-ppc.c,v
retrieving revision 1.224
diff -c -r1.224 elf32-ppc.c
*** bfd/elf32-ppc.c	8 Nov 2007 13:51:06 -0000	1.224
--- bfd/elf32-ppc.c	9 Jan 2008 14:58:38 -0000
***************
*** 3910,3915 ****
--- 3910,3918 ----
    const Elf_Internal_Rela *rel, *relend;
    asection *got2;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    if ((sec->flags & SEC_ALLOC) == 0)
      return TRUE;
  
Index: bfd/elf32-s390.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-s390.c,v
retrieving revision 1.95
diff -c -r1.95 elf32-s390.c
*** bfd/elf32-s390.c	26 Sep 2007 13:45:32 -0000	1.95
--- bfd/elf32-s390.c	9 Jan 2008 14:58:39 -0000
***************
*** 1424,1429 ****
--- 1424,1432 ----
    bfd_signed_vma *local_got_refcounts;
    const Elf_Internal_Rela *rel, *relend;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    elf_section_data (sec)->local_dynrel = NULL;
  
    symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
Index: bfd/elf32-sh.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-sh.c,v
retrieving revision 1.154
diff -c -r1.154 elf32-sh.c
*** bfd/elf32-sh.c	8 Nov 2007 13:51:06 -0000	1.154
--- bfd/elf32-sh.c	9 Jan 2008 14:58:42 -0000
***************
*** 4564,4569 ****
--- 4564,4572 ----
    bfd_signed_vma *local_got_refcounts;
    const Elf_Internal_Rela *rel, *relend;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    elf_section_data (sec)->local_dynrel = NULL;
  
    symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
Index: bfd/elf32-vax.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-vax.c,v
retrieving revision 1.46
diff -c -r1.46 elf32-vax.c
*** bfd/elf32-vax.c	26 Sep 2007 13:45:32 -0000	1.46
--- bfd/elf32-vax.c	9 Jan 2008 14:58:43 -0000
***************
*** 867,872 ****
--- 867,875 ----
    const Elf_Internal_Rela *rel, *relend;
    bfd *dynobj;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    dynobj = elf_hash_table (info)->dynobj;
    if (dynobj == NULL)
      return TRUE;
Index: bfd/elf32-xtensa.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-xtensa.c,v
retrieving revision 1.105
diff -c -r1.105 elf32-xtensa.c
*** bfd/elf32-xtensa.c	21 Dec 2007 23:11:28 -0000	1.105
--- bfd/elf32-xtensa.c	9 Jan 2008 14:58:46 -0000
***************
*** 1012,1017 ****
--- 1012,1020 ----
    bfd_signed_vma *local_got_refcounts;
    const Elf_Internal_Rela *rel, *relend;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    if ((sec->flags & SEC_ALLOC) == 0)
      return TRUE;
  
Index: bfd/elf64-ppc.c
===================================================================
RCS file: /cvs/src/src/bfd/elf64-ppc.c,v
retrieving revision 1.271
diff -c -r1.271 elf64-ppc.c
*** bfd/elf64-ppc.c	15 Nov 2007 23:47:58 -0000	1.271
--- bfd/elf64-ppc.c	9 Jan 2008 14:58:52 -0000
***************
*** 5265,5270 ****
--- 5265,5273 ----
    struct got_entry **local_got_ents;
    const Elf_Internal_Rela *rel, *relend;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    if ((sec->flags & SEC_ALLOC) == 0)
      return TRUE;
  
Index: bfd/elf64-s390.c
===================================================================
RCS file: /cvs/src/src/bfd/elf64-s390.c,v
retrieving revision 1.96
diff -c -r1.96 elf64-s390.c
*** bfd/elf64-s390.c	26 Sep 2007 13:45:32 -0000	1.96
--- bfd/elf64-s390.c	9 Jan 2008 14:58:54 -0000
***************
*** 1392,1397 ****
--- 1392,1400 ----
    bfd_signed_vma *local_got_refcounts;
    const Elf_Internal_Rela *rel, *relend;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    elf_section_data (sec)->local_dynrel = NULL;
  
    symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
Index: bfd/elf64-x86-64.c
===================================================================
RCS file: /cvs/src/src/bfd/elf64-x86-64.c,v
retrieving revision 1.144
diff -c -r1.144 elf64-x86-64.c
*** bfd/elf64-x86-64.c	18 Oct 2007 09:13:51 -0000	1.144
--- bfd/elf64-x86-64.c	9 Jan 2008 14:58:56 -0000
***************
*** 1425,1430 ****
--- 1425,1433 ----
    bfd_signed_vma *local_got_refcounts;
    const Elf_Internal_Rela *rel, *relend;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    elf_section_data (sec)->local_dynrel = NULL;
  
    symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
Index: bfd/elflink.c
===================================================================
RCS file: /cvs/src/src/bfd/elflink.c,v
retrieving revision 1.291
diff -c -r1.291 elflink.c
*** bfd/elflink.c	7 Jan 2008 16:45:35 -0000	1.291
--- bfd/elflink.c	9 Jan 2008 14:59:00 -0000
***************
*** 11474,11481 ****
    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  
    if (!bed->can_gc_sections
-       || info->relocatable
-       || info->emitrelocations
        || !is_elf_hash_table (info->hash))
      {
        (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
--- 11474,11479 ----
Index: bfd/elfxx-mips.c
===================================================================
RCS file: /cvs/src/src/bfd/elfxx-mips.c,v
retrieving revision 1.223
diff -c -r1.223 elfxx-mips.c
*** bfd/elfxx-mips.c	29 Nov 2007 12:23:43 -0000	1.223
--- bfd/elfxx-mips.c	9 Jan 2008 14:59:03 -0000
***************
*** 9917,9922 ****
--- 9917,9925 ----
    unsigned long r_symndx;
    struct elf_link_hash_entry *h;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
    sym_hashes = elf_sym_hashes (abfd);
    local_got_refcounts = elf_local_got_refcounts (abfd);
Index: bfd/elfxx-sparc.c
===================================================================
RCS file: /cvs/src/src/bfd/elfxx-sparc.c,v
retrieving revision 1.36
diff -c -r1.36 elfxx-sparc.c
*** bfd/elfxx-sparc.c	8 Nov 2007 13:51:06 -0000	1.36
--- bfd/elfxx-sparc.c	9 Jan 2008 14:59:06 -0000
***************
*** 1563,1568 ****
--- 1563,1571 ----
    bfd_signed_vma *local_got_refcounts;
    const Elf_Internal_Rela *rel, *relend;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    elf_section_data (sec)->local_dynrel = NULL;
  
    htab = _bfd_sparc_elf_hash_table (info);
Index: ld/ld.texinfo
===================================================================
RCS file: /cvs/src/src/ld/ld.texinfo,v
retrieving revision 1.203
diff -c -r1.203 ld.texinfo
*** ld/ld.texinfo	1 Oct 2007 09:54:57 -0000	1.203
--- ld/ld.texinfo	9 Jan 2008 14:59:15 -0000
***************
*** 1280,1290 ****
  @item --gc-sections
  @itemx --no-gc-sections
  Enable garbage collection of unused input sections.  It is ignored on
! targets that do not support this option.  This option is not compatible
! with @samp{-r} or @samp{--emit-relocs}. The default behaviour (of not
  performing this garbage collection) can be restored by specifying
  @samp{--no-gc-sections} on the command line.
  
  @kindex --print-gc-sections
  @kindex --no-print-gc-sections
  @cindex garbage collection
--- 1280,1294 ----
  @item --gc-sections
  @itemx --no-gc-sections
  Enable garbage collection of unused input sections.  It is ignored on
! targets that do not support this option.  The default behaviour (of not
  performing this garbage collection) can be restored by specifying
  @samp{--no-gc-sections} on the command line.
  
+ This option can be set when doing a partial link (enabled with option
+ @samp{-r}).  In this case the root of symbols kept is the entry point and must
+ be specified with the option @samp{-e} or with @code{ENTRY} command of the
+ script.
+ 
  @kindex --print-gc-sections
  @kindex --no-print-gc-sections
  @cindex garbage collection
Index: ld/ldlang.c
===================================================================
RCS file: /cvs/src/src/ld/ldlang.c,v
retrieving revision 1.273
diff -c -r1.273 ldlang.c
*** ld/ldlang.c	17 Oct 2007 19:26:10 -0000	1.273
--- ld/ldlang.c	9 Jan 2008 14:59:16 -0000
***************
*** 5077,5089 ****
    struct bfd_link_hash_entry *h;
    bfd_boolean warn;
  
!   if (link_info.relocatable || link_info.shared)
      warn = FALSE;
    else
      warn = TRUE;
  
    if (entry_symbol.name == NULL)
      {
        /* No entry has been specified.  Look for the default entry, but
  	 don't warn if we don't find it.  */
        entry_symbol.name = entry_symbol_default;
--- 5077,5093 ----
    struct bfd_link_hash_entry *h;
    bfd_boolean warn;
  
!   if ((link_info.relocatable && !link_info.gc_sections)
!       || link_info.shared)
      warn = FALSE;
    else
      warn = TRUE;
  
    if (entry_symbol.name == NULL)
      {
+       if (link_info.gc_sections && link_info.relocatable)
+ 	einfo (_("%P%F: entry symbol required when gc-sections\n"));
+ 
        /* No entry has been specified.  Look for the default entry, but
  	 don't warn if we don't find it.  */
        entry_symbol.name = entry_symbol_default;
Index: ld/ldmain.c
===================================================================
RCS file: /cvs/src/src/ld/ldmain.c,v
retrieving revision 1.125
diff -c -r1.125 ldmain.c
*** ld/ldmain.c	6 Jul 2007 14:09:41 -0000	1.125
--- ld/ldmain.c	9 Jan 2008 14:59:17 -0000
***************
*** 292,300 ****
  
    if (link_info.relocatable)
      {
!       if (link_info.gc_sections)
! 	einfo ("%P%F: --gc-sections and -r may not be used together\n");
!       else if (command_line.relax)
  	einfo (_("%P%F: --relax and -r may not be used together\n"));
        if (link_info.shared)
  	einfo (_("%P%F: -r and -shared may not be used together\n"));
--- 292,298 ----
  
    if (link_info.relocatable)
      {
!       if (command_line.relax)
  	einfo (_("%P%F: --relax and -r may not be used together\n"));
        if (link_info.shared)
  	einfo (_("%P%F: -r and -shared may not be used together\n"));
Index: ld/scripttempl/elf.sc
===================================================================
RCS file: /cvs/src/src/ld/scripttempl/elf.sc,v
retrieving revision 1.84
diff -c -r1.84 elf.sc
*** ld/scripttempl/elf.sc	15 Dec 2007 09:43:40 -0000	1.84
--- ld/scripttempl/elf.sc	9 Jan 2008 14:59:17 -0000
***************
*** 250,256 ****
  OUTPUT_FORMAT("${OUTPUT_FORMAT}", "${BIG_OUTPUT_FORMAT}",
  	      "${LITTLE_OUTPUT_FORMAT}")
  OUTPUT_ARCH(${OUTPUT_ARCH})
! ENTRY(${ENTRY})
  
  ${RELOCATING+${LIB_SEARCH_DIRS}}
  ${RELOCATING+${EXECUTABLE_SYMBOLS}}
--- 250,256 ----
  OUTPUT_FORMAT("${OUTPUT_FORMAT}", "${BIG_OUTPUT_FORMAT}",
  	      "${LITTLE_OUTPUT_FORMAT}")
  OUTPUT_ARCH(${OUTPUT_ARCH})
! ${RELOCATING+ENTRY(${ENTRY})}
  
  ${RELOCATING+${LIB_SEARCH_DIRS}}
  ${RELOCATING+${EXECUTABLE_SYMBOLS}}
Index: ld/testsuite/lib/ld-lib.exp
===================================================================
RCS file: /cvs/src/src/ld/testsuite/lib/ld-lib.exp,v
retrieving revision 1.52
diff -c -r1.52 ld-lib.exp
*** ld/testsuite/lib/ld-lib.exp	20 Nov 2007 15:08:23 -0000	1.52
--- ld/testsuite/lib/ld-lib.exp	9 Jan 2008 14:59:22 -0000
***************
*** 1559,1572 ****
  	    return 0
  	}
  
- 	# VxWorks kernel modules are relocatable objects linked with -r,
- 	# while RTP executables are linked with -q (--emit-relocs).
- 	# Both of these options are incompatible with --gc-sections.
- 	if { [istarget *-*-vxworks*] } {
- 	    set gc_sections_available_saved 0
- 	    return 0
- 	}
- 
  	# Check if the ld used by gcc supports --gc-sections.
  	set ld_output [remote_exec host $ld "--help"]
  	if { [ string first "--gc-sections" $ld_output ] >= 0 } {
--- 1559,1564 ----
Index: ld/testsuite/ld-gc/gc.c
===================================================================
RCS file: ld/testsuite/ld-gc/gc.c
diff -N ld/testsuite/ld-gc/gc.c
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- ld/testsuite/ld-gc/gc.c	9 Jan 2008 15:36:50 -0000
***************
*** 0 ****
--- 1,20 ----
+ int unused_var = 7;
+ int used_var = 7;
+ 
+ int
+ unused_func (int v)
+ {
+   return 3 * unused_var;
+ }
+ 
+ int
+ used_func (int v)
+ {
+   return 2 * used_var;
+ }
+ 
+ int
+ main (void)
+ {
+   return used_func (5);
+ }
Index: ld/testsuite/ld-gc/gc.exp
===================================================================
RCS file: ld/testsuite/ld-gc/gc.exp
diff -N ld/testsuite/ld-gc/gc.exp
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- ld/testsuite/ld-gc/gc.exp	9 Jan 2008 15:36:50 -0000
***************
*** 0 ****
--- 1,70 ----
+ # Expect script for ld-gc tests
+ #   Copyright 2008
+ #   Free Software Foundation, Inc.
+ #
+ # This file is part of the GNU Binutils.
+ #
+ # This program is free software; you can redistribute it and/or modify
+ # it under the terms of the GNU General Public License as published by
+ # the Free Software Foundation; either version 3 of the License, or
+ # (at your option) any later version.
+ #
+ # This program is distributed in the hope that it will be useful,
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ # GNU General Public License for more details.
+ #
+ # You should have received a copy of the GNU General Public License
+ # along with this program; if not, write to the Free Software
+ # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+ # MA 02110-1301, USA.
+ 
+ # These tests require --gc-sections
+ if ![check_gc_sections_available] {
+     return
+ }
+ 
+ set cflags "-ffunction-sections -fdata-sections"
+ set objfile "tmpdir/gc.o"
+ 
+ ld_compile "$CC -c $CFLAGS $cflags" $srcdir/$subdir/gc.c $objfile
+ 
+ proc test_gc { testname filename linker ldflags} {
+     global nm
+     global srcdir
+     global subdir
+     global nm_output
+     global objfile
+ 
+     set outfile "tmpdir/$filename"
+ 
+     if ![ld_simple_link $linker $outfile "-L$srcdir/$subdir $ldflags $objfile"] {
+ 	    fail $testname
+ 	return
+     }
+     if ![ld_nm $nm "" $outfile] {
+ 	unresolved $testname
+ 	return
+     }
+     if {![info exists nm_output(used_func)] \
+ 	    || ![info exists nm_output(used_var)]} {
+ 	send_log "used sections do not exist\n"
+ 	verbose "used sections do not exist"
+ 	fail $testname
+ 	return
+     }
+     if {[info exists nm_output(unused_func)] \
+ 	    || [info exists nm_output(unused_var)]} {
+ 	send_log "unused section still here\n"
+ 	verbose "unused section still here"
+ 	fail $testname
+ 	return
+     }
+     pass $testname
+ }
+ 
+ test_gc "Check --gc-section" "gcexe" $ld "--gc-sections -e main"
+ test_gc "Check --gc-section/-q" "gcrexe" $ld "--gc-sections -q -e main"
+ test_gc "Check --gc-section/-r" "gcrel" $ld "-r --gc-sections -e main"
+ 
+ run_dump_test "noent"
Index: ld/testsuite/ld-gc/noent.d
===================================================================
RCS file: ld/testsuite/ld-gc/noent.d
diff -N ld/testsuite/ld-gc/noent.d
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- ld/testsuite/ld-gc/noent.d	9 Jan 2008 15:36:50 -0000
***************
*** 0 ****
--- 1,3 ----
+ # name: --gc-sections -r without -e
+ # ld: --gc-sections -r
+ # error: entry symbol required when gc-sections
Index: ld/testsuite/ld-gc/noent.s
===================================================================
RCS file: ld/testsuite/ld-gc/noent.s
diff -N ld/testsuite/ld-gc/noent.s
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- ld/testsuite/ld-gc/noent.s	9 Jan 2008 15:36:50 -0000
***************
*** 0 ****
--- 1,4 ----
+ 	.text
+ 	.globl entry
+ entry:
+ 	.long 0

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

* Re: PATCH [v2]: makes --gc-sections compatible with -r/-q
  2008-01-10 14:50   ` PATCH [v2]: " Tristan Gingold
@ 2008-01-11  1:16     ` Alan Modra
  2008-01-11 10:50       ` Tristan Gingold
  2008-01-11 14:56       ` PATCH [v3]: " Tristan Gingold
  0 siblings, 2 replies; 27+ messages in thread
From: Alan Modra @ 2008-01-11  1:16 UTC (permalink / raw)
  To: Tristan Gingold; +Cc: binutils

On Wed, Jan 09, 2008 at 04:48:58PM +0100, Tristan Gingold wrote:
> >You need to modify gc_sweep_hook to cope with -r for all targets.
> >Also, Eric's change, "Add missing guard", is not the proper fix for
> >ppc_elf_gc_sweep_hook.  You should instead exit ppc_elf_gc_sweep_hook
> >immediately if -r, because that is what ppc_elf_check_relocs does.
> 
> Ok, here is the new version of the patch.
> 
> * makes all gc_sweep_hook coherent with check_relocs (ie exit  
> immediately if -r)

That looks better, but please explain why you omitted elf32-bfin.c and
elf64-mmix.c.  I'm not saying you are wrong in this (bfin is fairly
obviously ok), but I'd like confirmation that you at least looked at
these targets..

> * --gc-section -r requires a non-default entry point
> * document in --gc-section the use of -e

Does forcing the use of -e really make sense?  What if I'm using
-r --gc-sections on an object file and wish to keep two functions and
everything they depend on?  Wouldn't -u be a more natural option to
use in that case?  Hmm, I guess a warning or error would be useful if
neither -e or -u is specified.

I'm committing the following --gc-sections overview.

	* ld.texinfo (--gc-sections): Describe linker behaviour.

Index: ld/ld.texinfo
===================================================================
RCS file: /cvs/src/src/ld/ld.texinfo,v
retrieving revision 1.203
diff -u -p -r1.203 ld.texinfo
--- ld/ld.texinfo	1 Oct 2007 09:54:57 -0000	1.203
+++ ld/ld.texinfo	10 Jan 2008 01:06:17 -0000
@@ -1285,6 +1285,16 @@ with @samp{-r} or @samp{--emit-relocs}. 
 performing this garbage collection) can be restored by specifying
 @samp{--no-gc-sections} on the command line.
 
+@samp{--gc-sections} decides which input sections are used by
+examining symbols and relocations.  The section containing the entry
+symbol and all sections containing symbols undefined on the
+command-line will be kept, as will sections containing symbols
+referenced by dynamic objects.  Note that when building shared
+libraries, the linker must assume that any visible symbol is
+referenced.  Once this initial set of sections has been determined,
+the linker recursively marks as used any section referenced by their
+relocations.  See @samp{--entry} and @samp{--undefined}.
+
 @kindex --print-gc-sections
 @kindex --no-print-gc-sections
 @cindex garbage collection

-- 
Alan Modra
Australia Development Lab, IBM

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

* Re: PATCH [v2]: makes --gc-sections compatible with -r/-q
  2008-01-11  1:16     ` Alan Modra
@ 2008-01-11 10:50       ` Tristan Gingold
  2008-01-11 14:56       ` PATCH [v3]: " Tristan Gingold
  1 sibling, 0 replies; 27+ messages in thread
From: Tristan Gingold @ 2008-01-11 10:50 UTC (permalink / raw)
  To: Alan Modra; +Cc: binutils


On Jan 10, 2008, at 2:12 AM, Alan Modra wrote:

> On Wed, Jan 09, 2008 at 04:48:58PM +0100, Tristan Gingold wrote:
>> * makes all gc_sweep_hook coherent with check_relocs (ie exit
>> immediately if -r)
>
> That looks better, but please explain why you omitted elf32-bfin.c and
> elf64-mmix.c.  I'm not saying you are wrong in this (bfin is fairly
> obviously ok), but I'd like confirmation that you at least looked at
> these targets..

Yes, I looked at these two targets too.  But their current behavior  
seems OK.

>> * --gc-section -r requires a non-default entry point
>> * document in --gc-section the use of -e
>
> Does forcing the use of -e really make sense?  What if I'm using
> -r --gc-sections on an object file and wish to keep two functions and
> everything they depend on?  Wouldn't -u be a more natural option to
> use in that case?  Hmm, I guess a warning or error would be useful if
> neither -e or -u is specified.

Good point.  Requiring -e, -u or ENTRY when --gc-sections -r seems to  
be a better
policy.  I will update my patch.

Thank you for your comments and for improving the documentation.

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

* PATCH [v3]: makes --gc-sections compatible with -r/-q
  2008-01-11  1:16     ` Alan Modra
  2008-01-11 10:50       ` Tristan Gingold
@ 2008-01-11 14:56       ` Tristan Gingold
  2008-01-11 16:48         ` Alan Modra
  2008-01-24  1:12         ` Andreas Schwab
  1 sibling, 2 replies; 27+ messages in thread
From: Tristan Gingold @ 2008-01-11 14:56 UTC (permalink / raw)
  To: binutils; +Cc: Alan Modra

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

> Hmm, I guess a warning or error would be useful if
> neither -e or -u is specified.

I have updated the patch.  An error is now emitted if neither -e nor - 
u is specified
when --gc-sections -r.

Tristan.

[-- Attachment #2: ld-gc.diffs3 --]
[-- Type: application/octet-stream, Size: 21715 bytes --]

bfd:
2008-01-10  Tristan Gingold  <gingold@adacore.com>
	    Eric Botcazou  <ebotcazou@adacore.com>

	* elf32-ppc.c (ppc_elf_gc_sweep_hook): Exit early if generating a
	relocatable.
	* elf32-arm.c (elf32_arm_gc_sweep_hook): Likewise.
	* elf32-cris.c (cris_elf_gc_sweep_hook): Likewise.
	* elf32-hppa.c (elf32_hppa_gc_sweep_hook): Likewise.
	* elf32-i386.c (elf_i386_gc_sweep_hook): Likewise.
	* elf32-m32r.c (m32r_elf_gc_sweep_hook): Likewise.
	* elf32-m68k.c (elf_m68k_gc_sweep_hook): Likewise.
	* elf32-s390.c (elf_s390_gc_sweep_hook): Likewise.
	* elf32-sh.c (sh_elf_gc_sweep_hook): Likewise.
	* elf32-vax.c (elf_vax_gc_sweep_hook): Likewise.
	* elf32-xtensa.c (elf_xtensa_gc_sweep_hook): Likewise.
	* elf64-x86-64.c (elf64_x86_64_gc_sweep_hook): Likewise.
	* elf64-s390.c (elf_s390_gc_sweep_hook): Likewise.
	* elf64-ppc.c (ppc64_elf_gc_sweep_hook): Likewise.
	* elfxx-mips.c (_bfd_mips_elf_gc_sweep_hook): Likewise.
	* elfxx-sparc.c (_bfd_sparc_elf_gc_sweep_hook): Likewise.
	* elflink.c (bfd_elf_gc_sections): Do not punt on relocatable output
	or executable output with relocations.

ld:
2008-01-10  Tristan Gingold  <gingold@adacore.com>
	    Eric Botcazou  <ebotcazou@adacore.com>

	* ldlang.c (lang_end): Warns if the entry point is not found when
	--gc-sections.
	Emit an error if no root is specified when --gc-sections -r.
	* ld.texinfo (Options): Document that --gc-sections is compatible
	with -r and -q.
	* ldmain.c (main): Do not error out if -r and --gc-sections.
	* scripttempl/elf.sc: Emit ENTRY command only if relocating.

ld/testsuite:
2008-01-10  Tristan Gingold  <gingold@adacore.com>

	* lib/ld-lib.exp (check_gc_sections_available): Now available on
	VxWorks.
	* ld-gc: New directory for testing --gc-sections.
	* ld-gc/gc.c: New file.
	* ld-gc/gc.exp: New file.
	* ld-gc/noent.s: New file.
	* ld-gc/noent.d: New file.

Index: bfd/elf32-arm.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-arm.c,v
retrieving revision 1.131
diff -c -p -r1.131 elf32-arm.c
*** bfd/elf32-arm.c	29 Nov 2007 08:14:22 -0000	1.131
--- bfd/elf32-arm.c	10 Jan 2008 09:42:29 -0000
*************** elf32_arm_gc_sweep_hook (bfd *          
*** 7351,7356 ****
--- 7351,7359 ----
    const Elf_Internal_Rela *rel, *relend;
    struct elf32_arm_link_hash_table * globals;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    globals = elf32_arm_hash_table (info);
  
    elf_section_data (sec)->local_dynrel = NULL;
Index: bfd/elf32-cris.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-cris.c,v
retrieving revision 1.84
diff -c -p -r1.84 elf32-cris.c
*** bfd/elf32-cris.c	28 Sep 2007 08:43:45 -0000	1.84
--- bfd/elf32-cris.c	10 Jan 2008 09:42:31 -0000
*************** cris_elf_gc_sweep_hook (bfd *abfd,
*** 1986,1991 ****
--- 1986,1994 ----
    asection *sgot;
    asection *srelgot;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    dynobj = elf_hash_table (info)->dynobj;
    if (dynobj == NULL)
      return TRUE;
Index: bfd/elf32-hppa.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-hppa.c,v
retrieving revision 1.160
diff -c -p -r1.160 elf32-hppa.c
*** bfd/elf32-hppa.c	31 Dec 2007 11:01:54 -0000	1.160
--- bfd/elf32-hppa.c	10 Jan 2008 09:42:33 -0000
*************** elf32_hppa_gc_sweep_hook (bfd *abfd,
*** 1622,1627 ****
--- 1622,1630 ----
    bfd_signed_vma *local_plt_refcounts;
    const Elf_Internal_Rela *rela, *relend;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    elf_section_data (sec)->local_dynrel = NULL;
  
    symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
Index: bfd/elf32-i386.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-i386.c,v
retrieving revision 1.183
diff -c -p -r1.183 elf32-i386.c
*** bfd/elf32-i386.c	8 Nov 2007 13:51:06 -0000	1.183
--- bfd/elf32-i386.c	10 Jan 2008 09:42:35 -0000
*************** elf_i386_gc_sweep_hook (bfd *abfd,
*** 1599,1604 ****
--- 1599,1607 ----
    bfd_signed_vma *local_got_refcounts;
    const Elf_Internal_Rela *rel, *relend;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    elf_section_data (sec)->local_dynrel = NULL;
  
    symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
Index: bfd/elf32-m32r.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-m32r.c,v
retrieving revision 1.90
diff -c -p -r1.90 elf32-m32r.c
*** bfd/elf32-m32r.c	28 Sep 2007 08:43:45 -0000	1.90
--- bfd/elf32-m32r.c	10 Jan 2008 09:42:37 -0000
*************** m32r_elf_gc_sweep_hook (bfd *abfd ATTRIB
*** 3654,3659 ****
--- 3654,3662 ----
    bfd_signed_vma *local_got_refcounts;
    const Elf_Internal_Rela *rel, *relend;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    elf_section_data (sec)->local_dynrel = NULL;
  
    symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
Index: bfd/elf32-m68k.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-m68k.c,v
retrieving revision 1.105
diff -c -p -r1.105 elf32-m68k.c
*** bfd/elf32-m68k.c	26 Sep 2007 13:45:32 -0000	1.105
--- bfd/elf32-m68k.c	10 Jan 2008 09:42:38 -0000
*************** elf_m68k_gc_sweep_hook (bfd *abfd,
*** 1093,1098 ****
--- 1093,1101 ----
    asection *sgot;
    asection *srelgot;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    dynobj = elf_hash_table (info)->dynobj;
    if (dynobj == NULL)
      return TRUE;
Index: bfd/elf32-ppc.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-ppc.c,v
retrieving revision 1.224
diff -c -p -r1.224 elf32-ppc.c
*** bfd/elf32-ppc.c	8 Nov 2007 13:51:06 -0000	1.224
--- bfd/elf32-ppc.c	10 Jan 2008 09:42:42 -0000
*************** ppc_elf_gc_sweep_hook (bfd *abfd,
*** 3910,3915 ****
--- 3910,3918 ----
    const Elf_Internal_Rela *rel, *relend;
    asection *got2;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    if ((sec->flags & SEC_ALLOC) == 0)
      return TRUE;
  
Index: bfd/elf32-s390.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-s390.c,v
retrieving revision 1.95
diff -c -p -r1.95 elf32-s390.c
*** bfd/elf32-s390.c	26 Sep 2007 13:45:32 -0000	1.95
--- bfd/elf32-s390.c	10 Jan 2008 09:42:44 -0000
*************** elf_s390_gc_sweep_hook (bfd *abfd,
*** 1424,1429 ****
--- 1424,1432 ----
    bfd_signed_vma *local_got_refcounts;
    const Elf_Internal_Rela *rel, *relend;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    elf_section_data (sec)->local_dynrel = NULL;
  
    symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
Index: bfd/elf32-sh.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-sh.c,v
retrieving revision 1.154
diff -c -p -r1.154 elf32-sh.c
*** bfd/elf32-sh.c	8 Nov 2007 13:51:06 -0000	1.154
--- bfd/elf32-sh.c	10 Jan 2008 09:42:48 -0000
*************** sh_elf_gc_sweep_hook (bfd *abfd, struct 
*** 4564,4569 ****
--- 4564,4572 ----
    bfd_signed_vma *local_got_refcounts;
    const Elf_Internal_Rela *rel, *relend;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    elf_section_data (sec)->local_dynrel = NULL;
  
    symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
Index: bfd/elf32-vax.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-vax.c,v
retrieving revision 1.46
diff -c -p -r1.46 elf32-vax.c
*** bfd/elf32-vax.c	26 Sep 2007 13:45:32 -0000	1.46
--- bfd/elf32-vax.c	10 Jan 2008 09:42:49 -0000
*************** elf_vax_gc_sweep_hook (bfd *abfd, struct
*** 867,872 ****
--- 867,875 ----
    const Elf_Internal_Rela *rel, *relend;
    bfd *dynobj;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    dynobj = elf_hash_table (info)->dynobj;
    if (dynobj == NULL)
      return TRUE;
Index: bfd/elf32-xtensa.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-xtensa.c,v
retrieving revision 1.105
diff -c -p -r1.105 elf32-xtensa.c
*** bfd/elf32-xtensa.c	21 Dec 2007 23:11:28 -0000	1.105
--- bfd/elf32-xtensa.c	10 Jan 2008 09:42:56 -0000
*************** elf_xtensa_gc_sweep_hook (bfd *abfd,
*** 1012,1017 ****
--- 1012,1020 ----
    bfd_signed_vma *local_got_refcounts;
    const Elf_Internal_Rela *rel, *relend;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    if ((sec->flags & SEC_ALLOC) == 0)
      return TRUE;
  
Index: bfd/elf64-ppc.c
===================================================================
RCS file: /cvs/src/src/bfd/elf64-ppc.c,v
retrieving revision 1.271
diff -c -p -r1.271 elf64-ppc.c
*** bfd/elf64-ppc.c	15 Nov 2007 23:47:58 -0000	1.271
--- bfd/elf64-ppc.c	10 Jan 2008 09:43:02 -0000
*************** ppc64_elf_gc_sweep_hook (bfd *abfd, stru
*** 5265,5270 ****
--- 5265,5273 ----
    struct got_entry **local_got_ents;
    const Elf_Internal_Rela *rel, *relend;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    if ((sec->flags & SEC_ALLOC) == 0)
      return TRUE;
  
Index: bfd/elf64-s390.c
===================================================================
RCS file: /cvs/src/src/bfd/elf64-s390.c,v
retrieving revision 1.96
diff -c -p -r1.96 elf64-s390.c
*** bfd/elf64-s390.c	26 Sep 2007 13:45:32 -0000	1.96
--- bfd/elf64-s390.c	10 Jan 2008 09:43:06 -0000
*************** elf_s390_gc_sweep_hook (bfd *abfd,
*** 1392,1397 ****
--- 1392,1400 ----
    bfd_signed_vma *local_got_refcounts;
    const Elf_Internal_Rela *rel, *relend;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    elf_section_data (sec)->local_dynrel = NULL;
  
    symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
Index: bfd/elf64-x86-64.c
===================================================================
RCS file: /cvs/src/src/bfd/elf64-x86-64.c,v
retrieving revision 1.144
diff -c -p -r1.144 elf64-x86-64.c
*** bfd/elf64-x86-64.c	18 Oct 2007 09:13:51 -0000	1.144
--- bfd/elf64-x86-64.c	10 Jan 2008 09:43:08 -0000
*************** elf64_x86_64_gc_sweep_hook (bfd *abfd, s
*** 1425,1430 ****
--- 1425,1433 ----
    bfd_signed_vma *local_got_refcounts;
    const Elf_Internal_Rela *rel, *relend;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    elf_section_data (sec)->local_dynrel = NULL;
  
    symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
Index: bfd/elflink.c
===================================================================
RCS file: /cvs/src/src/bfd/elflink.c,v
retrieving revision 1.291
diff -c -p -r1.291 elflink.c
*** bfd/elflink.c	7 Jan 2008 16:45:35 -0000	1.291
--- bfd/elflink.c	10 Jan 2008 09:43:14 -0000
*************** bfd_elf_gc_sections (bfd *abfd, struct b
*** 11474,11481 ****
    const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  
    if (!bed->can_gc_sections
-       || info->relocatable
-       || info->emitrelocations
        || !is_elf_hash_table (info->hash))
      {
        (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
--- 11474,11479 ----
Index: bfd/elfxx-mips.c
===================================================================
RCS file: /cvs/src/src/bfd/elfxx-mips.c,v
retrieving revision 1.223
diff -c -p -r1.223 elfxx-mips.c
*** bfd/elfxx-mips.c	29 Nov 2007 12:23:43 -0000	1.223
--- bfd/elfxx-mips.c	10 Jan 2008 09:43:21 -0000
*************** _bfd_mips_elf_gc_sweep_hook (bfd *abfd A
*** 9917,9922 ****
--- 9917,9925 ----
    unsigned long r_symndx;
    struct elf_link_hash_entry *h;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
    sym_hashes = elf_sym_hashes (abfd);
    local_got_refcounts = elf_local_got_refcounts (abfd);
Index: bfd/elfxx-sparc.c
===================================================================
RCS file: /cvs/src/src/bfd/elfxx-sparc.c,v
retrieving revision 1.36
diff -c -p -r1.36 elfxx-sparc.c
*** bfd/elfxx-sparc.c	8 Nov 2007 13:51:06 -0000	1.36
--- bfd/elfxx-sparc.c	10 Jan 2008 09:43:25 -0000
*************** _bfd_sparc_elf_gc_sweep_hook (bfd *abfd,
*** 1563,1568 ****
--- 1563,1571 ----
    bfd_signed_vma *local_got_refcounts;
    const Elf_Internal_Rela *rel, *relend;
  
+   if (info->relocatable)
+     return TRUE;
+ 
    elf_section_data (sec)->local_dynrel = NULL;
  
    htab = _bfd_sparc_elf_hash_table (info);
Index: ld/ld.texinfo
===================================================================
RCS file: /cvs/src/src/ld/ld.texinfo,v
retrieving revision 1.204
diff -c -p -r1.204 ld.texinfo
*** ld/ld.texinfo	10 Jan 2008 01:12:56 -0000	1.204
--- ld/ld.texinfo	10 Jan 2008 09:43:36 -0000
*************** it ends in a @code{.exe} suffix.
*** 1280,1287 ****
  @item --gc-sections
  @itemx --no-gc-sections
  Enable garbage collection of unused input sections.  It is ignored on
! targets that do not support this option.  This option is not compatible
! with @samp{-r} or @samp{--emit-relocs}. The default behaviour (of not
  performing this garbage collection) can be restored by specifying
  @samp{--no-gc-sections} on the command line.
  
--- 1280,1286 ----
  @item --gc-sections
  @itemx --no-gc-sections
  Enable garbage collection of unused input sections.  It is ignored on
! targets that do not support this option.  The default behaviour (of not
  performing this garbage collection) can be restored by specifying
  @samp{--no-gc-sections} on the command line.
  
*************** referenced.  Once this initial set of se
*** 1295,1300 ****
--- 1294,1304 ----
  the linker recursively marks as used any section referenced by their
  relocations.  See @samp{--entry} and @samp{--undefined}.
  
+ This option can be set when doing a partial link (enabled with option
+ @samp{-r}).  In this case the root of symbols kept must be explicitely 
+ specified either by an @samp{--entry} or @samp{--undefined} option or by
+ a @code{ENTRY} command in the linker script.
+ 
  @kindex --print-gc-sections
  @kindex --no-print-gc-sections
  @cindex garbage collection
Index: ld/ldlang.c
===================================================================
RCS file: /cvs/src/src/ld/ldlang.c,v
retrieving revision 1.273
diff -c -p -r1.273 ldlang.c
*** ld/ldlang.c	17 Oct 2007 19:26:10 -0000	1.273
--- ld/ldlang.c	10 Jan 2008 09:43:38 -0000
*************** lang_end (void)
*** 5077,5087 ****
    struct bfd_link_hash_entry *h;
    bfd_boolean warn;
  
!   if (link_info.relocatable || link_info.shared)
      warn = FALSE;
    else
      warn = TRUE;
  
    if (entry_symbol.name == NULL)
      {
        /* No entry has been specified.  Look for the default entry, but
--- 5077,5096 ----
    struct bfd_link_hash_entry *h;
    bfd_boolean warn;
  
!   if ((link_info.relocatable && !link_info.gc_sections)
!       || link_info.shared)
      warn = FALSE;
    else
      warn = TRUE;
  
+   /* Force the user to specify a root when generating a relocatable with
+      --gc-sections.  */
+   if (link_info.gc_sections && link_info.relocatable
+       && (entry_symbol.name == NULL
+ 	  && ldlang_undef_chain_list_head == NULL))
+     einfo (_("%P%F: gc-sections requires either an entry or "
+ 	     "an undefined symbol\n"));
+ 
    if (entry_symbol.name == NULL)
      {
        /* No entry has been specified.  Look for the default entry, but
Index: ld/ldmain.c
===================================================================
RCS file: /cvs/src/src/ld/ldmain.c,v
retrieving revision 1.125
diff -c -p -r1.125 ldmain.c
*** ld/ldmain.c	6 Jul 2007 14:09:41 -0000	1.125
--- ld/ldmain.c	10 Jan 2008 09:43:39 -0000
*************** main (int argc, char **argv)
*** 292,300 ****
  
    if (link_info.relocatable)
      {
!       if (link_info.gc_sections)
! 	einfo ("%P%F: --gc-sections and -r may not be used together\n");
!       else if (command_line.relax)
  	einfo (_("%P%F: --relax and -r may not be used together\n"));
        if (link_info.shared)
  	einfo (_("%P%F: -r and -shared may not be used together\n"));
--- 292,298 ----
  
    if (link_info.relocatable)
      {
!       if (command_line.relax)
  	einfo (_("%P%F: --relax and -r may not be used together\n"));
        if (link_info.shared)
  	einfo (_("%P%F: -r and -shared may not be used together\n"));
Index: ld/scripttempl/elf.sc
===================================================================
RCS file: /cvs/src/src/ld/scripttempl/elf.sc,v
retrieving revision 1.84
diff -c -p -r1.84 elf.sc
*** ld/scripttempl/elf.sc	15 Dec 2007 09:43:40 -0000	1.84
--- ld/scripttempl/elf.sc	10 Jan 2008 09:43:39 -0000
*************** cat <<EOF
*** 250,256 ****
  OUTPUT_FORMAT("${OUTPUT_FORMAT}", "${BIG_OUTPUT_FORMAT}",
  	      "${LITTLE_OUTPUT_FORMAT}")
  OUTPUT_ARCH(${OUTPUT_ARCH})
! ENTRY(${ENTRY})
  
  ${RELOCATING+${LIB_SEARCH_DIRS}}
  ${RELOCATING+${EXECUTABLE_SYMBOLS}}
--- 250,256 ----
  OUTPUT_FORMAT("${OUTPUT_FORMAT}", "${BIG_OUTPUT_FORMAT}",
  	      "${LITTLE_OUTPUT_FORMAT}")
  OUTPUT_ARCH(${OUTPUT_ARCH})
! ${RELOCATING+ENTRY(${ENTRY})}
  
  ${RELOCATING+${LIB_SEARCH_DIRS}}
  ${RELOCATING+${EXECUTABLE_SYMBOLS}}
Index: ld/testsuite/ld-gc/gc.c
===================================================================
RCS file: ld/testsuite/ld-gc/gc.c
diff -N ld/testsuite/ld-gc/gc.c
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- ld/testsuite/ld-gc/gc.c	10 Jan 2008 09:43:40 -0000
***************
*** 0 ****
--- 1,20 ----
+ int unused_var = 7;
+ int used_var = 7;
+ 
+ int
+ unused_func (int v)
+ {
+   return 3 * unused_var;
+ }
+ 
+ int
+ used_func (int v)
+ {
+   return 2 * used_var;
+ }
+ 
+ int
+ main (void)
+ {
+   return used_func (5);
+ }
Index: ld/testsuite/ld-gc/gc.exp
===================================================================
RCS file: ld/testsuite/ld-gc/gc.exp
diff -N ld/testsuite/ld-gc/gc.exp
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- ld/testsuite/ld-gc/gc.exp	10 Jan 2008 09:43:40 -0000
***************
*** 0 ****
--- 1,71 ----
+ # Expect script for ld-gc tests
+ #   Copyright 2008
+ #   Free Software Foundation, Inc.
+ #
+ # This file is part of the GNU Binutils.
+ #
+ # This program is free software; you can redistribute it and/or modify
+ # it under the terms of the GNU General Public License as published by
+ # the Free Software Foundation; either version 3 of the License, or
+ # (at your option) any later version.
+ #
+ # This program is distributed in the hope that it will be useful,
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ # GNU General Public License for more details.
+ #
+ # You should have received a copy of the GNU General Public License
+ # along with this program; if not, write to the Free Software
+ # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+ # MA 02110-1301, USA.
+ 
+ # These tests require --gc-sections
+ if ![check_gc_sections_available] {
+     return
+ }
+ 
+ set cflags "-ffunction-sections -fdata-sections"
+ set objfile "tmpdir/gc.o"
+ 
+ ld_compile "$CC -c $CFLAGS $cflags" $srcdir/$subdir/gc.c $objfile
+ 
+ proc test_gc { testname filename linker ldflags} {
+     global nm
+     global srcdir
+     global subdir
+     global nm_output
+     global objfile
+ 
+     set outfile "tmpdir/$filename"
+ 
+     if ![ld_simple_link $linker $outfile "-L$srcdir/$subdir $ldflags $objfile"] {
+ 	    fail $testname
+ 	return
+     }
+     if ![ld_nm $nm "" $outfile] {
+ 	unresolved $testname
+ 	return
+     }
+     if {![info exists nm_output(used_func)] \
+ 	    || ![info exists nm_output(used_var)]} {
+ 	send_log "used sections do not exist\n"
+ 	verbose "used sections do not exist"
+ 	fail $testname
+ 	return
+     }
+     if {[info exists nm_output(unused_func)] \
+ 	    || [info exists nm_output(unused_var)]} {
+ 	send_log "unused section still here\n"
+ 	verbose "unused section still here"
+ 	fail $testname
+ 	return
+     }
+     pass $testname
+ }
+ 
+ test_gc "Check --gc-section" "gcexe" $ld "--gc-sections -e main"
+ test_gc "Check --gc-section/-q" "gcrexe" $ld "--gc-sections -q -e main"
+ test_gc "Check --gc-section/-r/-e" "gcrel" $ld "-r --gc-sections -e main"
+ test_gc "Check --gc-section/-r/-u" "gcrel" $ld "-r --gc-sections -u used_func"
+ 
+ run_dump_test "noent"
Index: ld/testsuite/ld-gc/noent.d
===================================================================
RCS file: ld/testsuite/ld-gc/noent.d
diff -N ld/testsuite/ld-gc/noent.d
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- ld/testsuite/ld-gc/noent.d	10 Jan 2008 09:43:40 -0000
***************
*** 0 ****
--- 1,3 ----
+ # name: --gc-sections -r without -e
+ # ld: --gc-sections -r
+ # error: gc-sections requires either an entry or an undefined symbol
Index: ld/testsuite/ld-gc/noent.s
===================================================================
RCS file: ld/testsuite/ld-gc/noent.s
diff -N ld/testsuite/ld-gc/noent.s
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- ld/testsuite/ld-gc/noent.s	10 Jan 2008 09:43:40 -0000
***************
*** 0 ****
--- 1,4 ----
+ 	.text
+ 	.globl entry
+ entry:
+ 	.long 0
Index: ld/testsuite/lib/ld-lib.exp
===================================================================
RCS file: /cvs/src/src/ld/testsuite/lib/ld-lib.exp,v
retrieving revision 1.52
diff -c -p -r1.52 ld-lib.exp
*** ld/testsuite/lib/ld-lib.exp	20 Nov 2007 15:08:23 -0000	1.52
--- ld/testsuite/lib/ld-lib.exp	10 Jan 2008 09:43:42 -0000
*************** proc check_gc_sections_available { } {
*** 1559,1572 ****
  	    return 0
  	}
  
- 	# VxWorks kernel modules are relocatable objects linked with -r,
- 	# while RTP executables are linked with -q (--emit-relocs).
- 	# Both of these options are incompatible with --gc-sections.
- 	if { [istarget *-*-vxworks*] } {
- 	    set gc_sections_available_saved 0
- 	    return 0
- 	}
- 
  	# Check if the ld used by gcc supports --gc-sections.
  	set ld_output [remote_exec host $ld "--help"]
  	if { [ string first "--gc-sections" $ld_output ] >= 0 } {
--- 1559,1564 ----

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

* Re: PATCH [v3]: makes --gc-sections compatible with -r/-q
  2008-01-11 14:56       ` PATCH [v3]: " Tristan Gingold
@ 2008-01-11 16:48         ` Alan Modra
  2008-01-11 16:56           ` Tristan Gingold
  2008-01-24  1:12         ` Andreas Schwab
  1 sibling, 1 reply; 27+ messages in thread
From: Alan Modra @ 2008-01-11 16:48 UTC (permalink / raw)
  To: Tristan Gingold; +Cc: binutils

On Thu, Jan 10, 2008 at 10:56:18AM +0100, Tristan Gingold wrote:
> I have updated the patch.  An error is now emitted if neither -e nor - 
> u is specified
> when --gc-sections -r.

OK to apply, thanks!

-- 
Alan Modra
Australia Development Lab, IBM

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

* Re: PATCH [v3]: makes --gc-sections compatible with -r/-q
  2008-01-11 16:48         ` Alan Modra
@ 2008-01-11 16:56           ` Tristan Gingold
  0 siblings, 0 replies; 27+ messages in thread
From: Tristan Gingold @ 2008-01-11 16:56 UTC (permalink / raw)
  To: Alan Modra; +Cc: binutils


On Jan 11, 2008, at 2:20 AM, Alan Modra wrote:
>
> OK to apply, thanks!

Committed.
Thank you for all your helpful comments.

Tristan.

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

* Re: PATCH [v3]: makes --gc-sections compatible with -r/-q
  2008-01-11 14:56       ` PATCH [v3]: " Tristan Gingold
  2008-01-11 16:48         ` Alan Modra
@ 2008-01-24  1:12         ` Andreas Schwab
  2008-01-24  1:26           ` Tristan Gingold
  2008-01-24  6:05           ` Tristan Gingold
  1 sibling, 2 replies; 27+ messages in thread
From: Andreas Schwab @ 2008-01-24  1:12 UTC (permalink / raw)
  To: Tristan Gingold; +Cc: binutils, Alan Modra

Tristan Gingold <gingold@adacore.com> writes:

> 	* ld-gc: New directory for testing --gc-sections.
> 	* ld-gc/gc.c: New file.
> 	* ld-gc/gc.exp: New file.
> 	* ld-gc/noent.s: New file.
> 	* ld-gc/noent.d: New file.

These tests are failing on ppc64, s390, s390x and x86_64.

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

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

* Re: PATCH [v3]: makes --gc-sections compatible with -r/-q
  2008-01-24  1:12         ` Andreas Schwab
@ 2008-01-24  1:26           ` Tristan Gingold
  2008-01-24  8:29             ` Andreas Schwab
  2008-01-24  6:05           ` Tristan Gingold
  1 sibling, 1 reply; 27+ messages in thread
From: Tristan Gingold @ 2008-01-24  1:26 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: binutils, Alan Modra


On Jan 23, 2008, at 1:34 PM, Andreas Schwab wrote:

> Tristan Gingold <gingold@adacore.com> writes:
>
>> 	* ld-gc: New directory for testing --gc-sections.
>> 	* ld-gc/gc.c: New file.
>> 	* ld-gc/gc.exp: New file.
>> 	* ld-gc/noent.s: New file.
>> 	* ld-gc/noent.d: New file.
>
> These tests are failing on ppc64, s390, s390x and x86_64.

I will first investigate x86_64.  For the other, it will take more  
time as I don't have a ready gcc for them.

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

* Re: PATCH [v3]: makes --gc-sections compatible with -r/-q
  2008-01-24  1:12         ` Andreas Schwab
  2008-01-24  1:26           ` Tristan Gingold
@ 2008-01-24  6:05           ` Tristan Gingold
  1 sibling, 0 replies; 27+ messages in thread
From: Tristan Gingold @ 2008-01-24  6:05 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: binutils, Alan Modra

>
> These tests are failing on ppc64, s390, s390x and x86_64.

I can't reproduce the issue on x86_64 (SUSE Linux Enterprise Server  
10 (x86_64) gcc 4.1.0))
and GNU ld (GNU Binutils) 2.18.50.20080122.

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

* Re: PATCH [v3]: makes --gc-sections compatible with -r/-q
  2008-01-24  1:26           ` Tristan Gingold
@ 2008-01-24  8:29             ` Andreas Schwab
  2008-01-24 14:13               ` H.J. Lu
  2008-01-24 14:16               ` Tristan Gingold
  0 siblings, 2 replies; 27+ messages in thread
From: Andreas Schwab @ 2008-01-24  8:29 UTC (permalink / raw)
  To: Tristan Gingold; +Cc: binutils, Alan Modra

Tristan Gingold <gingold@adacore.com> writes:

> I will first investigate x86_64.  For the other, it will take more time as
> I don't have a ready gcc for them.

I can provide you the object files so you don't need a compiler.

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

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

* Re: PATCH [v3]: makes --gc-sections compatible with -r/-q
  2008-01-24  8:29             ` Andreas Schwab
@ 2008-01-24 14:13               ` H.J. Lu
  2008-01-24 14:16               ` Tristan Gingold
  1 sibling, 0 replies; 27+ messages in thread
From: H.J. Lu @ 2008-01-24 14:13 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: Tristan Gingold, binutils, Alan Modra

On Wed, Jan 23, 2008 at 03:07:58PM +0100, Andreas Schwab wrote:
> Tristan Gingold <gingold@adacore.com> writes:
> 
> > I will first investigate x86_64.  For the other, it will take more time as
> > I don't have a ready gcc for them.
> 
> I can provide you the object files so you don't need a compiler.

It works for me on Fedora 7/x86_64 after I fixed the bug in gc.exp:

http://sourceware.org/ml/binutils/2008-01/msg00236.html

H.J.

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

* Re: PATCH [v3]: makes --gc-sections compatible with -r/-q
  2008-01-24  8:29             ` Andreas Schwab
  2008-01-24 14:13               ` H.J. Lu
@ 2008-01-24 14:16               ` Tristan Gingold
  2008-01-24 14:33                 ` Andreas Schwab
  2008-01-24 22:44                 ` Andreas Schwab
  1 sibling, 2 replies; 27+ messages in thread
From: Tristan Gingold @ 2008-01-24 14:16 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: binutils, Alan Modra


On Jan 23, 2008, at 3:07 PM, Andreas Schwab wrote:

> Tristan Gingold <gingold@adacore.com> writes:
>
>> I will first investigate x86_64.  For the other, it will take more  
>> time as
>> I don't have a ready gcc for them.
>
> I can provide you the object files so you don't need a compiler.

Will be useful.  But we first have to settle the x86_64 case: why  
does it work for me and not for you ?

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

* Re: PATCH [v3]: makes --gc-sections compatible with -r/-q
  2008-01-24 14:16               ` Tristan Gingold
@ 2008-01-24 14:33                 ` Andreas Schwab
  2008-01-24 22:44                 ` Andreas Schwab
  1 sibling, 0 replies; 27+ messages in thread
From: Andreas Schwab @ 2008-01-24 14:33 UTC (permalink / raw)
  To: Tristan Gingold; +Cc: binutils, Alan Modra

Tristan Gingold <gingold@adacore.com> writes:

> Will be useful.  But we first have to settle the x86_64 case: why does it
> work for me and not for you ?

Your compiler is too old.

(Checked in as obvious.)

Andreas.

2008-01-23  Andreas Schwab  <schwab@suse.de>

	* ld-gc/gc.c: Make sure used_func is not inlined.

--- ld/testsuite/ld-gc/gc.c.~1.1.~	2008-01-11 10:11:18.000000000 +0100
+++ ld/testsuite/ld-gc/gc.c	2008-01-23 16:39:07.000000000 +0100
@@ -8,6 +8,7 @@ unused_func (int v)
 }
 
 int
+__attribute__((noinline))
 used_func (int v)
 {
   return 2 * used_var;

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

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

* Re: PATCH [v3]: makes --gc-sections compatible with -r/-q
  2008-01-24 14:16               ` Tristan Gingold
  2008-01-24 14:33                 ` Andreas Schwab
@ 2008-01-24 22:44                 ` Andreas Schwab
  2008-01-25  9:24                   ` Alan Modra
  1 sibling, 1 reply; 27+ messages in thread
From: Andreas Schwab @ 2008-01-24 22:44 UTC (permalink / raw)
  To: Tristan Gingold; +Cc: binutils, Alan Modra

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

Tristan Gingold <gingold@adacore.com> writes:

> On Jan 23, 2008, at 3:07 PM, Andreas Schwab wrote:
>
>> Tristan Gingold <gingold@adacore.com> writes:
>>
>>> I will first investigate x86_64.  For the other, it will take more time
>>> as
>>> I don't have a ready gcc for them.
>>
>> I can provide you the object files so you don't need a compiler.
>
> Will be useful.

The object file for ppc64 is attached.  That is the only remaining
failure now.

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

[-- Attachment #2: gc.o --]
[-- Type: application/x-object, Size: 2600 bytes --]

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

* Re: PATCH [v3]: makes --gc-sections compatible with -r/-q
  2008-01-24 22:44                 ` Andreas Schwab
@ 2008-01-25  9:24                   ` Alan Modra
  2008-01-25 17:33                     ` Tristan Gingold
  0 siblings, 1 reply; 27+ messages in thread
From: Alan Modra @ 2008-01-25  9:24 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: Tristan Gingold, binutils

On Wed, Jan 23, 2008 at 05:50:24PM +0100, Andreas Schwab wrote:
> The object file for ppc64 is attached.  That is the only remaining
> failure now.

ppc64 is failing because ppc64_elf_gc_mark_hook isn't smart enough to
allow unused variables to be dropped.  It needs to treat .toc like
.opd, ie. not mark .toc when a reloc references .toc, but instead
mark the section referenced by that .toc entry.

If you'd like, xfail them for ppc64 at the moment.  I don't think I'll
get around to fixing this problem in the near future.

-- 
Alan Modra
Australia Development Lab, IBM

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

* Re: PATCH [v3]: makes --gc-sections compatible with -r/-q
  2008-01-25  9:24                   ` Alan Modra
@ 2008-01-25 17:33                     ` Tristan Gingold
  0 siblings, 0 replies; 27+ messages in thread
From: Tristan Gingold @ 2008-01-25 17:33 UTC (permalink / raw)
  To: Alan Modra; +Cc: Andreas Schwab, binutils


On Jan 24, 2008, at 2:08 AM, Alan Modra wrote:

> On Wed, Jan 23, 2008 at 05:50:24PM +0100, Andreas Schwab wrote:
>> The object file for ppc64 is attached.  That is the only remaining
>> failure now.
>
> ppc64 is failing because ppc64_elf_gc_mark_hook isn't smart enough to
> allow unused variables to be dropped.  It needs to treat .toc like
> .opd, ie. not mark .toc when a reloc references .toc, but instead
> mark the section referenced by that .toc entry.
>
> If you'd like, xfail them for ppc64 at the moment.  I don't think I'll
> get around to fixing this problem in the near future.

Given this indication, xfail them is fine for me.  I don't think  
there is a real use of
-r/-q + --gc-section on ppc64.

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

end of thread, other threads:[~2008-01-24 10:00 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-01-09  0:03 PATCH: makes --gc-sections compatible with -r/-q Tristan Gingold
2008-01-09  5:38 ` Daniel Jacobowitz
2008-01-09  6:09   ` Tristan Gingold
2008-01-09 15:44     ` Thiemo Seufer
2008-01-09 15:58     ` Daniel Jacobowitz
2008-01-09 16:26       ` Tristan Gingold
2008-01-09 16:32         ` H.J. Lu
2008-01-09 16:44           ` Tristan Gingold
2008-01-10  0:45             ` H.J. Lu
2008-01-10  1:12               ` Tristan Gingold
2008-01-10  8:04 ` Alan Modra
2008-01-10 14:50   ` PATCH [v2]: " Tristan Gingold
2008-01-11  1:16     ` Alan Modra
2008-01-11 10:50       ` Tristan Gingold
2008-01-11 14:56       ` PATCH [v3]: " Tristan Gingold
2008-01-11 16:48         ` Alan Modra
2008-01-11 16:56           ` Tristan Gingold
2008-01-24  1:12         ` Andreas Schwab
2008-01-24  1:26           ` Tristan Gingold
2008-01-24  8:29             ` Andreas Schwab
2008-01-24 14:13               ` H.J. Lu
2008-01-24 14:16               ` Tristan Gingold
2008-01-24 14:33                 ` Andreas Schwab
2008-01-24 22:44                 ` Andreas Schwab
2008-01-25  9:24                   ` Alan Modra
2008-01-25 17:33                     ` Tristan Gingold
2008-01-24  6:05           ` Tristan Gingold

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).