This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] PR ld/18160: Segmentation Fault in lang_gc_sections when linking elf_x86_64 output


On Wed, Mar 25, 2015 at 5:38 PM, Alan Modra <amodra@gmail.com> wrote:
> On Wed, Mar 25, 2015 at 04:21:14PM -0700, H.J. Lu wrote:
>> This patch ignores gc-sections if input relocation is incompatible with
>> output.  OK for master?
>
> No.  For one, it looks like you can't call _bfd_elf_relocs_compatible
> with a non-ELF input, which is either a bug in that function or you
> need to first test for ELF.
>
> Secondly, if you take a look at the gc-sections code you'll see that
> it handles non-ELF input files by simply not garbage collecting those
> files.  Your patch would disable gc-sections entirely on finding a
> non-ELF file.  I'm not sure how important it is to keep the old
> behaviour, but I'm guessing you did not intend to make that change.
>

How about this one?

This patch skips gc-sections if input relocation is incompatible with
output.

bfd/

PR ld/18160
* elflink.c (elf_gc_sweep): Skip if relocation is incompatible.

ld/testsuite/

PR ld/18160
* ld-x86-64/pr18160.d: New file.
* ld-x86-64/pr18160.s: Likewise.
* ld-x86-64/pr18160.t: Likewise.
* ld-x86-64/x86-64.exp: Run pr18160.


-- 
H.J.
From 5bea4ddc7d50c262ad1b85f101f960eb6a9dc98d Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Wed, 25 Mar 2015 16:15:02 -0700
Subject: [PATCH] Skip gc-sections if relocation is incompatible

This patch skips gc-sections if input relocation is incompatible with
output.

bfd/

	PR ld/18160
	* elflink.c (elf_gc_sweep): Skip if relocation is incompatible.

ld/testsuite/

	PR ld/18160
	* ld-x86-64/pr18160.d: New file.
	* ld-x86-64/pr18160.s: Likewise.
	* ld-x86-64/pr18160.t: Likewise.
	* ld-x86-64/x86-64.exp: Run pr18160.
---
 bfd/elflink.c                     |  7 +++++++
 ld/testsuite/ld-x86-64/pr18160.d  |  3 +++
 ld/testsuite/ld-x86-64/pr18160.s  | 16 ++++++++++++++++
 ld/testsuite/ld-x86-64/pr18160.t  |  8 ++++++++
 ld/testsuite/ld-x86-64/x86-64.exp |  1 +
 5 files changed, 35 insertions(+)
 create mode 100644 ld/testsuite/ld-x86-64/pr18160.d
 create mode 100644 ld/testsuite/ld-x86-64/pr18160.s
 create mode 100644 ld/testsuite/ld-x86-64/pr18160.t

diff --git a/bfd/elflink.c b/bfd/elflink.c
index 9ccad8c..855d120 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -12162,6 +12162,13 @@ elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
       if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
 	continue;
 
+      if (!(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
+	{
+	  (*_bfd_error_handler)(_("Warning: skip gc-sections: incompatible relocation in `%B'"),
+				  sub);
+	  continue;
+	}
+
       for (o = sub->sections; o != NULL; o = o->next)
 	{
 	  /* When any section in a section group is kept, we keep all
diff --git a/ld/testsuite/ld-x86-64/pr18160.d b/ld/testsuite/ld-x86-64/pr18160.d
new file mode 100644
index 0000000..13af30c
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/pr18160.d
@@ -0,0 +1,3 @@
+#as: --64
+#ld: --gc-sections -melf_x86_64 -T pr18160.t
+#warning: .* skip gc-sections: incompatible relocation in `tmpdir/dump0.o'
diff --git a/ld/testsuite/ld-x86-64/pr18160.s b/ld/testsuite/ld-x86-64/pr18160.s
new file mode 100644
index 0000000..a0331aa
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/pr18160.s
@@ -0,0 +1,16 @@
+	.section	.text.start,"ax",@progbits
+	.globl	start
+	.type	start, @function
+start:
+	.cfi_startproc
+	jmp	foo
+	.cfi_endproc
+	.size	start, .-start
+	.section	.text.foo,"ax",@progbits
+	.globl	foo
+	.type	foo, @function
+foo:
+	.cfi_startproc
+	ret
+	.cfi_endproc
+	.size	foo, .-foo
diff --git a/ld/testsuite/ld-x86-64/pr18160.t b/ld/testsuite/ld-x86-64/pr18160.t
new file mode 100644
index 0000000..d3906e3
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/pr18160.t
@@ -0,0 +1,8 @@
+OUTPUT_FORMAT(elf32-i386)
+ENTRY(start)
+SECTIONS
+{
+  .text : { *(.text*) }
+  .data : { *(.data.*) }
+  .bss : { *(.bss.*) }
+}
diff --git a/ld/testsuite/ld-x86-64/x86-64.exp b/ld/testsuite/ld-x86-64/x86-64.exp
index 43427be..7bceb7f 100644
--- a/ld/testsuite/ld-x86-64/x86-64.exp
+++ b/ld/testsuite/ld-x86-64/x86-64.exp
@@ -317,6 +317,7 @@ run_dump_test "mov1c"
 run_dump_test "mov1d"
 run_dump_test "pr17935-1"
 run_dump_test "pr17935-2"
+run_dump_test "pr18160"
 
 # Must be native with the C compiler
 if { [isnative] && [which $CC] != 0 } {
-- 
2.1.0


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]