Allow emit-relocs with gc-sections

Sriraman Tallam via binutils binutils@sourceware.org
Tue Jul 3 23:58:00 GMT 2018


Ping.

* object.cc (Sized_relobj_file<size, big_endian>::do_layout): Check
  if it is pass two or single pass when allowing emit relocs.
* testsuite/Makefile.am (gc_icf_emit_relocs_test): New test.
* testsuite/gc_icf_emit_relocs_test.cc: New test source.
* testsuite/gc_icf_emit_relocs_test.sh: New test.

On Tue, Jun 26, 2018 at 6:05 PM, Sriraman Tallam <tmsriram@google.com> wrote:
> Resending as plain text.
>
> * object.cc (Sized_relobj_file<size, big_endian>::do_layout): Check
>  if it is pass two or single pass when allowing emit relocs.
> * testsuite/Makefile.am (gc_icf_emit_relocs_test): New test.
> * testsuite/gc_icf_emit_relocs_test.cc: New test source.
> * testsuite/gc_icf_emit_relocs_test.sh: New test.
>
> On Tue, Jun 26, 2018 at 6:03 PM, Sriraman Tallam <tmsriram@google.com> wrote:
>> Hi Cary,
>>
>>    This patch allows emit-relocs when gc-sections or icf is present.  Is
>> this alright?
>>
>> * object.cc (Sized_relobj_file<size, big_endian>::do_layout): Check
>>   if it is pass two or single pass when allowing emit relocs.
>> * testsuite/Makefile.am (gc_icf_emit_relocs_test): New test.
>> * testsuite/gc_icf_emit_relocs_test.cc: New test source.
>> * testsuite/gc_icf_emit_relocs_test.sh: New test.
>>
>> Thanks
>> Sri
>>
-------------- next part --------------
	* object.cc (Sized_relobj_file<size, big_endian>::do_layout): Check
 	if it is pass two or single pass when allowing emit relocs.
	* testsuite/Makefile.am (gc_icf_emit_relocs_test): New test.
	* testsuite/gc_icf_emit_relocs_test.cc: New test source.
	* testsuite/gc_icf_emit_relocs_test.sh: New test.

diff --git a/gold/object.cc b/gold/object.cc
index 374340fa16..0d6d268012 100644
--- a/gold/object.cc
+++ b/gold/object.cc
@@ -1706,9 +1706,9 @@ Sized_relobj_file<size, big_endian>::do_layout(Symbol_table* symtab,
       // reloc sections into the output.  We only want to copy the
       // ones associated with sections which are not being discarded.
       // However, we don't know that yet for all sections.  So save
-      // reloc sections and process them later. Garbage collection is
-      // not triggered when relocatable code is desired.
-      if (emit_relocs
+      // reloc sections and process them later.  With gc-sections
+      // or ICF, do this in the second pass.
+      if (emit_relocs && !is_pass_one
 	  && (sh_type == elfcpp::SHT_REL
 	      || sh_type == elfcpp::SHT_RELA))
 	{
@@ -1890,12 +1890,12 @@ Sized_relobj_file<size, big_endian>::do_layout(Symbol_table* symtab,
     }
 
   // When doing a relocatable link handle the reloc sections at the
-  // end.  Garbage collection  and Identical Code Folding is not
-  // turned on for relocatable code.
-  if (emit_relocs)
+  // end.  Garbage collection  and Identical Code Folding should do
+  // do this in the second pass.
+  if (emit_relocs && !is_pass_one)
     this->size_relocatable_relocs();
 
-  gold_assert(!is_two_pass || reloc_sections.empty());
+  gold_assert(!is_pass_one || reloc_sections.empty());
 
   for (std::vector<unsigned int>::const_iterator p = reloc_sections.begin();
        p != reloc_sections.end();
diff --git a/gold/testsuite/Makefile.am b/gold/testsuite/Makefile.am
index 5f7d981e2e..cbf9edeb48 100644
--- a/gold/testsuite/Makefile.am
+++ b/gold/testsuite/Makefile.am
@@ -316,6 +316,18 @@ icf_safe_so_test_1.stdout: icf_safe_so_test
 icf_safe_so_test_2.stdout: icf_safe_so_test
 	$(TEST_READELF) -h $< > $@
 
+if DEFAULT_TARGET_X86_64
+check_SCRIPTS += gc_icf_emit_relocs_test.sh
+check_DATA += gc_icf_emit_relocs_test.stdout
+MOSTLYCLEANFILES += gc_icf_emit_relocs_test
+gc_icf_emit_relocs_test.o: gc_icf_emit_relocs_test.cc
+	$(CXXCOMPILE) -O0 -c -ffunction-sections -o $@ $<
+gc_icf_emit_relocs_test: gc_icf_emit_relocs_test.o gcctestdir/ld
+	$(CXXLINK) -o gc_icf_emit_relocs_test -Bgcctestdir/ -Wl,--icf=all,--gc-sections -Wl,--emit-relocs gc_icf_emit_relocs_test.o
+gc_icf_emit_relocs_test.stdout: gc_icf_emit_relocs_test
+	$(TEST_READELF) -S $< > $@
+endif DEFAULT_TARGET_X86_64
+
diff --git a/gold/testsuite/gc_icf_emit_relocs_test.cc b/gold/testsuite/gc_icf_emit_relocs_test.cc
new file mode 100644
index 0000000000..3611676660
--- /dev/null
+++ b/gold/testsuite/gc_icf_emit_relocs_test.cc
@@ -0,0 +1,30 @@
+// gc_icf_emit_relocs_test.cc
+
+// Copyright (C) 2018 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.
+
+int a;
+int garbage() {
+  return a;
+}
+
+int main() {
+  return a;
+}
diff --git a/gold/testsuite/gc_icf_emit_relocs_test.sh b/gold/testsuite/gc_icf_emit_relocs_test.sh
new file mode 100755
index 0000000000..74e21b5ab1
--- /dev/null
+++ b/gold/testsuite/gc_icf_emit_relocs_test.sh
@@ -0,0 +1,29 @@
+#!/bin/sh
+
+# gc_icf_emit_relocs_test.sh
+
+# Copyright (C) 2010-2017 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.
+
+
+set -e
+
+grep "\.rela\.text" gc_icf_emit_relocs_test.stdout
+echo $?


More information about the Binutils mailing list