From 30c1b436add85460c33b52565691adaa71a22236 Mon Sep 17 00:00:00 2001 From: Eugene Leviant Date: Tue, 14 Mar 2017 08:49:09 +0000 Subject: [PATCH] [ELF] Fix crash when .eh_frame(_hdr) is discarded lld crashes when .eh_frame or .eh_frame_hdr section is discarded in linker script and there is no PHDRS directive. Differential revision: https://reviews.llvm.org/D30885 llvm-svn: 297712 --- lld/ELF/Writer.cpp | 3 ++- lld/test/ELF/linkerscript/pt_gnu_eh_frame.s | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 lld/test/ELF/linkerscript/pt_gnu_eh_frame.s diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 344f86fa5ca3..3375fc25b54b 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -1355,7 +1355,8 @@ template std::vector Writer::createPhdrs() { Ret.push_back(std::move(RelRo)); // PT_GNU_EH_FRAME is a special section pointing on .eh_frame_hdr. - if (!In::EhFrame->empty() && In::EhFrameHdr) + if (!In::EhFrame->empty() && In::EhFrameHdr && + In::EhFrame->OutSec && In::EhFrameHdr->OutSec) AddHdr(PT_GNU_EH_FRAME, In::EhFrameHdr->OutSec->getPhdrFlags()) ->add(In::EhFrameHdr->OutSec); diff --git a/lld/test/ELF/linkerscript/pt_gnu_eh_frame.s b/lld/test/ELF/linkerscript/pt_gnu_eh_frame.s new file mode 100644 index 000000000000..81b4c6307d4c --- /dev/null +++ b/lld/test/ELF/linkerscript/pt_gnu_eh_frame.s @@ -0,0 +1,13 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t +# RUN: echo "SECTIONS { /DISCARD/ : { *(.eh_frame*) *(.eh_frame_hdr*) } }" > %t.script +# RUN: ld.lld -o %t1 --eh-frame-hdr --script %t.script %t + +.global _start +_start: + nop + +.section .dah,"ax",@progbits +.cfi_startproc + nop +.cfi_endproc