From 858a659a4fe242cc1aabfa9c7648bb2818224620 Mon Sep 17 00:00:00 2001 From: George Rimar Date: Fri, 17 Feb 2017 19:46:47 +0000 Subject: [PATCH] [ELF] - Added support of linkerscript's "/DISCARD/" for --emit-relocs Previously LLD crashed on on provided testcases because "/DISCARD/" was not supported. Patch implements that. After this I think there is no known issues with --emit-relocs implementation required for linux kernel linking. Differential revision: https://reviews.llvm.org/D29273 llvm-svn: 295488 --- lld/ELF/InputFiles.cpp | 8 ++++++-- lld/test/ELF/linkerscript/emit-relocs-discard.s | 14 ++++++++++++++ .../ELF/linkerscript/emit-relocs-ehframe-discard.s | 11 +++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 lld/test/ELF/linkerscript/emit-relocs-discard.s create mode 100644 lld/test/ELF/linkerscript/emit-relocs-ehframe-discard.s diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index c9e775d92c2b..8564b25f59da 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -406,8 +406,12 @@ elf::ObjectFile::createInputSection(const Elf_Shdr &Sec, // from the output, so returning `nullptr` for the normal case. // However, if -emit-relocs is given, we need to leave them in the output. // (Some post link analysis tools need this information.) - if (Config->EmitRelocs) - return make>(this, &Sec, Name); + if (Config->EmitRelocs) { + InputSection *RelocSec = make>(this, &Sec, Name); + // We will not emit relocation section if target was discarded. + Target->DependentSections.push_back(RelocSec); + return RelocSec; + } return nullptr; } } diff --git a/lld/test/ELF/linkerscript/emit-relocs-discard.s b/lld/test/ELF/linkerscript/emit-relocs-discard.s new file mode 100644 index 000000000000..2f1f6c6b4b0a --- /dev/null +++ b/lld/test/ELF/linkerscript/emit-relocs-discard.s @@ -0,0 +1,14 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o +# RUN: echo "SECTIONS { /DISCARD/ : { *(.bbb) } }" > %t.script +# RUN: ld.lld --emit-relocs --script %t.script %t.o -o %t1 +# RUN: llvm-readobj -r %t1 | FileCheck %s + +# CHECK: Relocations [ +# CHECK-NEXT: ] + +.section .aaa,"",@progbits +.Lfoo: + +.section .bbb,"",@progbits +.long .Lfoo diff --git a/lld/test/ELF/linkerscript/emit-relocs-ehframe-discard.s b/lld/test/ELF/linkerscript/emit-relocs-ehframe-discard.s new file mode 100644 index 000000000000..9df0e8ce9dcb --- /dev/null +++ b/lld/test/ELF/linkerscript/emit-relocs-ehframe-discard.s @@ -0,0 +1,11 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o +# RUN: echo "SECTIONS { /DISCARD/ : { *(.eh_frame) } }" > %t.script +# RUN: ld.lld --emit-relocs --script %t.script %t1.o -o %t +# RUN: llvm-objdump -section-headers %t | FileCheck %s + +# CHECK-NOT: .rela.eh_frame + +.section .foo,"ax",@progbits +.cfi_startproc +.cfi_endproc