New flag --keep-section <section_name> to prevent certain sections from being linker garbage collected.

Sriraman Tallam tmsriram@google.com
Wed Apr 4 00:49:00 GMT 2012


Hi,

  This patch adds a new flag, --keep-section to allow specifying
section names that should not be gc'ed.

  For use cases, as an example,  a statically linked executable with
pthreads needs .rodata.nptl_version to be debuggable with gdb but
--gc-sections simply discards it. With this option,
--keep-section,.rodata.nptl_version will solve the problem. The
-u,<symbolname> does not work if the symbol is local.


	* options.h (--keep-section): New option.
	* object.cc (Relobj::is_section_name_included): Return true
	if section name matches any section specified with --keep-section.
	* testsuite/Makefile.am (gc_keep_section_test): New test.
	* testsuite/Makefile.in: Regenerate.
	* testsuite/gc_keep_section_test.cc: New file.
	* testsuite/gc_keep_section_test.sh: New file.


Comments?

Thanks,
-Sri.
-------------- next part --------------
Index: object.cc
===================================================================
RCS file: /cvs/src/src/gold/object.cc,v
retrieving revision 1.154
diff -u -u -p -r1.154 object.cc
--- object.cc	21 Mar 2012 19:02:21 -0000	1.154
+++ object.cc	4 Apr 2012 00:34:54 -0000
@@ -335,6 +335,18 @@ Relobj::is_section_name_included(const c
     {
       return true; 
     }
+
+  // Keep all section names mentioned with option --keep-section.
+  for (options::String_set::const_iterator p
+	 = parameters->options().keep_section_begin();
+       p != parameters->options().keep_section_end();
+       ++p)
+    {
+      const char* section_name = p->c_str();
+      if (strcmp (name, section_name) == 0)
+	return true;		  
+    }  
+
   return false;
 }
 
Index: options.h
===================================================================
RCS file: /cvs/src/src/gold/options.h,v
retrieving revision 1.174
diff -u -u -p -r1.174 options.h
--- options.h	21 Mar 2012 19:02:21 -0000	1.174
+++ options.h	4 Apr 2012 00:34:55 -0000
@@ -1053,6 +1053,10 @@ class General_options
               N_("List removed unused sections on stderr"),
               N_("Do not list removed unused sections"));
 
+  DEFINE_set(keep_section, options::TWO_DASHES, '\0',
+	     N_("Do not consider section for garbage collection"),
+	     N_("SYMBOL")); 
+
   DEFINE_bool(stats, options::TWO_DASHES, '\0', false,
               N_("Print resource usage statistics"), NULL);
 
cvs diff: Diffing po
cvs diff: Diffing testsuite
Index: testsuite/Makefile.am
===================================================================
RCS file: /cvs/src/src/gold/testsuite/Makefile.am,v
retrieving revision 1.191
diff -u -u -p -r1.191 Makefile.am
--- testsuite/Makefile.am	21 Mar 2012 19:02:22 -0000	1.191
+++ testsuite/Makefile.am	4 Apr 2012 00:34:55 -0000
@@ -164,6 +164,16 @@ gc_orphan_section_test:gc_orphan_section
 gc_orphan_section_test.stdout: gc_orphan_section_test
 	$(TEST_NM) gc_orphan_section_test > gc_orphan_section_test.stdout
 
+check_SCRIPTS += gc_keep_section_test.sh
+check_DATA += gc_keep_section_test.stdout
+MOSTLYCLEANFILES += gc_keep_section_test
+gc_keep_section_test.o: gc_keep_section_test.cc
+	$(CXXCOMPILE) -O0 -c -ffunction-sections -g -o $@ $<
+gc_keep_section_test:gc_keep_section_test.o gcctestdir/ld
+	$(CXXLINK) -Bgcctestdir/ -Wl,--gc-sections -Wl,--keep-section,.text._Z3foov gc_keep_section_test.o
+gc_keep_section_test.stdout: gc_keep_section_test
+	$(TEST_NM) gc_keep_section_test > gc_keep_section_test.stdout
+
 check_SCRIPTS += icf_test.sh
 check_DATA += icf_test.stdout
 MOSTLYCLEANFILES += icf_test
Index: testsuite/Makefile.in
===================================================================
RCS file: /cvs/src/src/gold/testsuite/Makefile.in,v
retrieving revision 1.201
diff -u -u -p -r1.201 Makefile.in
--- testsuite/Makefile.in	21 Mar 2012 19:02:22 -0000	1.201
+++ testsuite/Makefile.in	4 Apr 2012 00:34:56 -0000
@@ -76,6 +76,7 @@ check_PROGRAMS = $(am__EXEEXT_1) $(am__E
 @GCC_TRUE@@NATIVE_LINKER_TRUE@am__append_2 = incremental_test.sh \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	gc_comdat_test.sh gc_tls_test.sh \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	gc_orphan_section_test.sh \
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	gc_keep_section_test.sh \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	icf_test.sh \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	icf_keep_unique_test.sh \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	icf_safe_test.sh \
@@ -104,6 +105,7 @@ check_PROGRAMS = $(am__EXEEXT_1) $(am__E
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	gc_comdat_test.stdout \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	gc_tls_test.stdout \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	gc_orphan_section_test.stdout \
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	gc_keep_section_test.stdout \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	icf_test.stdout \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	icf_keep_unique_test.stdout \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	icf_safe_test_1.stdout \
@@ -118,7 +120,8 @@ check_PROGRAMS = $(am__EXEEXT_1) $(am__E
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	weak_plt_shared.so debug_msg.err
 @GCC_TRUE@@NATIVE_LINKER_TRUE@am__append_4 = incremental_test \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	gc_comdat_test gc_tls_test \
-@GCC_TRUE@@NATIVE_LINKER_TRUE@	gc_orphan_section_test icf_test \
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	gc_orphan_section_test \
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	gc_keep_section_test icf_test \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	icf_keep_unique_test \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	icf_safe_test icf_safe_so_test \
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	final_layout \
@@ -3645,6 +3648,8 @@ gc_tls_test.sh.log: gc_tls_test.sh
 	@p='gc_tls_test.sh'; $(am__check_pre) $(LOG_COMPILE) "$$tst" $(am__check_post)
 gc_orphan_section_test.sh.log: gc_orphan_section_test.sh
 	@p='gc_orphan_section_test.sh'; $(am__check_pre) $(LOG_COMPILE) "$$tst" $(am__check_post)
+gc_keep_section_test.sh.log: gc_keep_section_test.sh
+	@p='gc_keep_section_test.sh'; $(am__check_pre) $(LOG_COMPILE) "$$tst" $(am__check_post)
 icf_test.sh.log: icf_test.sh
 	@p='icf_test.sh'; $(am__check_pre) $(LOG_COMPILE) "$$tst" $(am__check_post)
 icf_keep_unique_test.sh.log: icf_keep_unique_test.sh
@@ -4252,6 +4257,12 @@ uninstall-am:
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	$(CXXLINK) -Bgcctestdir/ -Wl,--gc-sections gc_orphan_section_test.o
 @GCC_TRUE@@NATIVE_LINKER_TRUE@gc_orphan_section_test.stdout: gc_orphan_section_test
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	$(TEST_NM) gc_orphan_section_test > gc_orphan_section_test.stdout
+@GCC_TRUE@@NATIVE_LINKER_TRUE@gc_keep_section_test.o: gc_keep_section_test.cc
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	$(CXXCOMPILE) -O0 -c -ffunction-sections -g -o $@ $<
+@GCC_TRUE@@NATIVE_LINKER_TRUE@gc_keep_section_test:gc_keep_section_test.o gcctestdir/ld
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	$(CXXLINK) -Bgcctestdir/ -Wl,--gc-sections -Wl,--keep-section,.text._Z3foov gc_keep_section_test.o
+@GCC_TRUE@@NATIVE_LINKER_TRUE@gc_keep_section_test.stdout: gc_keep_section_test
+@GCC_TRUE@@NATIVE_LINKER_TRUE@	$(TEST_NM) gc_keep_section_test > gc_keep_section_test.stdout
 @GCC_TRUE@@NATIVE_LINKER_TRUE@icf_test.o: icf_test.cc 
 @GCC_TRUE@@NATIVE_LINKER_TRUE@	$(CXXCOMPILE) -O0 -c -ffunction-sections -g -o $@ $<
 @GCC_TRUE@@NATIVE_LINKER_TRUE@icf_test: icf_test.o gcctestdir/ld
Index: testsuite/gc_keep_section_test.cc
===================================================================
RCS file: testsuite/gc_keep_section_test.cc
diff -N testsuite/gc_keep_section_test.cc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ testsuite/gc_keep_section_test.cc	4 Apr 2012 00:34:56 -0000
@@ -0,0 +1,35 @@
+// gc_orphan_section_test.cc -- a test case for gold
+
+// Copyright 2012 Free Software Foundation, Inc.
+// Written by Sriraman Tallam <tmsriram@google.com>.
+
+// This file is part of gold.
+
+// 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.
+
+// The goal of this program is to verify if --keep-section flag works
+// correctly with --gc-sections.
+
+
+int foo ()
+{
+  return 0;
+}
+
+int main ()
+{
+  return 1;
+}
Index: testsuite/gc_keep_section_test.sh
===================================================================
RCS file: testsuite/gc_keep_section_test.sh
diff -N testsuite/gc_keep_section_test.sh
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ testsuite/gc_keep_section_test.sh	4 Apr 2012 00:34:56 -0000
@@ -0,0 +1,40 @@
+#!/bin/sh
+
+# gc_keep_section_test.sh -- test --gc-sections --keep-section
+
+# Copyright 2012 Free Software Foundation, Inc.
+# Written by Sriraman Tallam <tmsriram@google.com>.
+
+# This file is part of gold.
+
+# 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.
+
+# The goal of this program is to verify if gc-sections works as expected
+# with --keep-section flag.
+# File gc_keep_section_test.cc is in this test. This program checks if
+# .text.foo is retained even though it is garbage.
+
+check()
+{
+    grep_foo=`grep -q "foo" $1`
+    if [ $? != 0 ];
+    then
+        echo "Garbage collection should not discard foo"
+	exit 1
+    fi
+}
+
+check gc_keep_section_test.stdout


More information about the Binutils mailing list