This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Re: [PATCH] PR ld/18160: Segmentation Fault in lang_gc_sections when linking elf_x86_64 output
- From: "H.J. Lu" <hjl dot tools at gmail dot com>
- To: Binutils <binutils at sourceware dot org>
- Date: Wed, 25 Mar 2015 18:58:25 -0700
- Subject: Re: [PATCH] PR ld/18160: Segmentation Fault in lang_gc_sections when linking elf_x86_64 output
- Authentication-results: sourceware.org; auth=none
- References: <20150325232114 dot GA31855 at gmail dot com> <20150326003816 dot GR26234 at bubble dot grove dot modra dot org> <CAMe9rOpdxRQ2fTcA6nnSkO2YdW8v5TR5CYmy-BFFZzorXXcF7Q at mail dot gmail dot com> <20150326012737 dot GS26234 at bubble dot grove dot modra dot org>
On Wed, Mar 25, 2015 at 6:27 PM, Alan Modra <amodra@gmail.com> wrote:
> On Wed, Mar 25, 2015 at 06:14:29PM -0700, H.J. Lu wrote:
>> 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.
>
> Is the warning really necessary? Also, it looks to me that the
> relocs_compatible test should be in more places, eg. to stop the
> backend gc_mark_hook from being confused by incompatible relocs.
Like this?
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.
(bfd_elf_gc_sections): Likewise.
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 b6d25efce0c3f4fef9bd93c830327c5fc7c9c0fd 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.
(bfd_elf_gc_sections): Likewise.
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 | 6 ++++--
ld/testsuite/ld-x86-64/pr18160.d | 14 ++++++++++++++
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, 43 insertions(+), 2 deletions(-)
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..748ff1b 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -12159,7 +12159,8 @@ elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
{
asection *o;
- if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
+ if (bfd_get_flavour (sub) != bfd_target_elf_flavour
+ || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
continue;
for (o = sub->sections; o != NULL; o = o->next)
@@ -12446,7 +12447,8 @@ bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
{
asection *o;
- if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
+ if (bfd_get_flavour (sub) != bfd_target_elf_flavour
+ || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
continue;
/* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
diff --git a/ld/testsuite/ld-x86-64/pr18160.d b/ld/testsuite/ld-x86-64/pr18160.d
new file mode 100644
index 0000000..b944bbe
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/pr18160.d
@@ -0,0 +1,14 @@
+#as: --64
+#ld: --gc-sections -melf_x86_64 -T pr18160.t
+#objdump: -dw
+
+.*: +file format elf32-i386
+
+
+Disassembly of section .text:
+
+0+ <start>:
+[ ]*[a-f0-9]+: e9 00 00 00 00 jmp 5 <foo>
+
+0+5 <foo>:
+[ ]*[a-f0-9]+: c3 ret
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