From: Tristan Gingold Date: Fri, 11 Jan 2008 09:11:18 +0000 (+0000) Subject: ld: X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=ac69cbc672a0702152d34bac2da436dcee2e5346;p=binutils-gdb.git ld: 2008-01-11 Tristan Gingold Eric Botcazou * 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-11 Tristan Gingold * 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. --- diff --git a/ld/ChangeLog b/ld/ChangeLog index 9ef8fb7c4f6..372422d1b08 100644 --- a/ld/ChangeLog +++ b/ld/ChangeLog @@ -1,3 +1,14 @@ +2008-01-11 Tristan Gingold + Eric Botcazou + + * 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. + 2008-01-10 Daniel Jacobowitz PR ld/5533 diff --git a/ld/genscrba.sh b/ld/genscrba.sh index 621de7ab47a..030cb7d42ec 100644 --- a/ld/genscrba.sh +++ b/ld/genscrba.sh @@ -8,7 +8,9 @@ source_em() } fragment() { - local lineno=$[${BASH_LINENO[0]} + 1] - echo >> e${EMULATION_NAME}.c "#line $lineno \"$em_script\"" + if [ ${BASH_VERSINFO[3]} -ge 3 ]; then + local lineno=$[${BASH_LINENO[0]} + 1] + echo >> e${EMULATION_NAME}.c "#line $lineno \"$em_script\"" + fi cat >> e${EMULATION_NAME}.c } diff --git a/ld/ld.texinfo b/ld/ld.texinfo index 7cbc26ab0ab..7d77d1c7d1d 100644 --- a/ld/ld.texinfo +++ b/ld/ld.texinfo @@ -1280,8 +1280,7 @@ it ends in a @code{.exe} suffix. @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 +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. @@ -1295,6 +1294,11 @@ 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}. +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 diff --git a/ld/ldlang.c b/ld/ldlang.c index 9d3c513d54b..63326e53bf0 100644 --- a/ld/ldlang.c +++ b/ld/ldlang.c @@ -5077,11 +5077,20 @@ lang_end (void) struct bfd_link_hash_entry *h; bfd_boolean warn; - if (link_info.relocatable || link_info.shared) + if ((link_info.relocatable && !link_info.gc_sections) + || link_info.shared) warn = entry_from_cmdline; 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 diff --git a/ld/ldmain.c b/ld/ldmain.c index f1112cfc518..29f00d621cd 100644 --- a/ld/ldmain.c +++ b/ld/ldmain.c @@ -292,9 +292,7 @@ main (int argc, char **argv) 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) + 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")); diff --git a/ld/scripttempl/elf.sc b/ld/scripttempl/elf.sc index 2f86d916416..5c35e4f4aa6 100644 --- a/ld/scripttempl/elf.sc +++ b/ld/scripttempl/elf.sc @@ -250,7 +250,7 @@ cat < + + * 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. + 2008-01-09 Richard Sandiford PR ld/5526 diff --git a/ld/testsuite/ld-gc/gc.c b/ld/testsuite/ld-gc/gc.c new file mode 100644 index 00000000000..c662f72f8b7 --- /dev/null +++ b/ld/testsuite/ld-gc/gc.c @@ -0,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); +} diff --git a/ld/testsuite/ld-gc/gc.exp b/ld/testsuite/ld-gc/gc.exp new file mode 100644 index 00000000000..68a0ba11642 --- /dev/null +++ b/ld/testsuite/ld-gc/gc.exp @@ -0,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" diff --git a/ld/testsuite/ld-gc/noent.d b/ld/testsuite/ld-gc/noent.d new file mode 100644 index 00000000000..1741a744520 --- /dev/null +++ b/ld/testsuite/ld-gc/noent.d @@ -0,0 +1,3 @@ +# name: --gc-sections -r without -e +# ld: --gc-sections -r +# error: gc-sections requires either an entry or an undefined symbol diff --git a/ld/testsuite/ld-gc/noent.s b/ld/testsuite/ld-gc/noent.s new file mode 100644 index 00000000000..ea74bf421e7 --- /dev/null +++ b/ld/testsuite/ld-gc/noent.s @@ -0,0 +1,4 @@ + .text + .globl entry +entry: + .long 0 diff --git a/ld/testsuite/lib/ld-lib.exp b/ld/testsuite/lib/ld-lib.exp index f3264788dca..4e8903c1409 100644 --- a/ld/testsuite/lib/ld-lib.exp +++ b/ld/testsuite/lib/ld-lib.exp @@ -1559,14 +1559,6 @@ proc check_gc_sections_available { } { 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 } {