From b634d11d61e3236f4602e9461dba67d1320f65e3 Mon Sep 17 00:00:00 2001 From: "H.J. Lu" Date: Wed, 13 Jan 2021 06:48:07 -0800 Subject: [PATCH] ld: Check for ELF input before accessing ELF section data commit b209b5a6b8a accesses ELF section data without checking if input is ELF. It caused: sh: line 1: 1355479 Segmentation fault (core dumped) /export/build/gnu/tools-build/binutils-gitlab-x32/build-x86_64-linux-gnux32/ld/ld-new -o tmpdir/pe-x86-64-1 -z norelro -L/export/gnu/import/git/gitlab/x86-binutils/ld/testsuite/ld-x86-64 -m elf_x86_64 --entry=begin tmpdir/pe-x86-64-1a.obj tmpdir/pe-x86-64-1b.obj tmpdir/pe-x86-64-1c.obj 2>&1 FAIL: Build pe-x86-64-1 on Linux/x86-64 with PE/x86-64 inputs. Add check for ELF input before accessing ELF section data. * ldelfgen.c (add_link_order_input_section): Check for ELF input before accessing ELF section data. --- ld/ChangeLog | 5 +++++ ld/ldelfgen.c | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/ld/ChangeLog b/ld/ChangeLog index 49e580d2345..2dad0b9878d 100644 --- a/ld/ChangeLog +++ b/ld/ChangeLog @@ -1,3 +1,8 @@ +2021-01-13 H.J. Lu + + * ldelfgen.c (add_link_order_input_section): Check for ELF input + before accessing ELF section data. + 2021-01-13 Alan Modra * Makefile.in: Regenerate. diff --git a/ld/ldelfgen.c b/ld/ldelfgen.c index 8014e2229b9..47e7a2a68ed 100644 --- a/ld/ldelfgen.c +++ b/ld/ldelfgen.c @@ -79,7 +79,8 @@ add_link_order_input_section (lang_input_section_type *is, os_info->isec[os_info->count].idx = os_info->count; os_info->count++; s = is->section; - if ((s->flags & SEC_LINKER_CREATED) == 0 + if (s->owner->xvec->flavour == bfd_target_elf_flavour + && (s->flags & SEC_LINKER_CREATED) == 0 && elf_section_data (s) != NULL && elf_linked_to_section (s) != NULL) os_info->ordered++; -- 2.30.2