[PATCH] D44601: LLD: Avoid segfault with --emit-relocs

Domagoj Stolfa via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 20 17:07:31 PDT 2018


dstolfa updated this revision to Diff 139227.
dstolfa added a comment.

Import the test case from @grimar's https://reviews.llvm.org/D44679 to verify that this patch works with the linkerscript test as well.

Thanks for thinking of this case!


https://reviews.llvm.org/D44601

Files:
  ELF/LinkerScript.cpp
  ELF/Writer.cpp
  test/ELF/emit-relocs-eh-frame.s
  test/ELF/linkerscript/eh-frame-emit-relocs.s


Index: test/ELF/linkerscript/eh-frame-emit-relocs.s
===================================================================
--- /dev/null
+++ test/ELF/linkerscript/eh-frame-emit-relocs.s
@@ -0,0 +1,13 @@
+# REQUIRES: x86
+# RUN: echo "SECTIONS { .foo : { *(.eh_frame) } }" > %t.script
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+# RUN: ld.lld --emit-relocs %t.o -T %t.script -o %t
+# RUN: llvm-objdump -section-headers %t | FileCheck %s
+
+# CHECK-NOT: eh_frame
+# CHECK:     .rela.foo
+# CHECK-NOT: eh_frame
+
+.text
+ .cfi_startproc
+ .cfi_endproc
Index: test/ELF/emit-relocs-eh-frame.s
===================================================================
--- /dev/null
+++ test/ELF/emit-relocs-eh-frame.s
@@ -0,0 +1,17 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o
+# RUN: ld.lld --emit-relocs %t1.o -o %t
+# RUN: llvm-readobj -r %t | FileCheck %s
+
+# CHECK:      Relocations [
+# CHECK-NEXT:   Section {{.*}} .rela.eh_frame {
+# CHECK-NEXT:     0x{{.*}} R_X86_64_PC32 .text 0x0
+# CHECK-NEXT:   }
+# CHECK-NEXT: ]
+
+.globl _start
+.text
+_start:
+  .cfi_startproc
+  nop
+  .cfi_endproc
Index: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -98,6 +98,8 @@
       !isa<SyntheticSection>(S)) {
     OutputSection *Out =
         cast<InputSection>(S)->getRelocatedSection()->getOutputSection();
+    if (!Out)
+      return S->Name;
     if (S->Type == SHT_RELA)
       return Saver.save(".rela" + Out->Name);
     return Saver.save(".rel" + Out->Name);
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -555,6 +555,9 @@
   // other REL[A] sections created by linker itself.
   if (!isa<SyntheticSection>(IS) &&
       (IS->Type == SHT_REL || IS->Type == SHT_RELA)) {
+    if (!Config->Relocatable &&
+        OutsecName.find(".eh_frame") != std::string::npos)
+      return createSection(IS, OutsecName);
     auto *Sec = cast<InputSection>(IS);
     OutputSection *Out = Sec->getRelocatedSection()->getOutputSection();
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D44601.139227.patch
Type: text/x-patch
Size: 2170 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180321/f2cb3a2d/attachment.bin>


More information about the llvm-commits mailing list