Fix crash with -emit-relocs -shared.
The code to handle the input SHT_REL/SHT_RELA sections was getting
confused with the linker generated relocation sections.
llvm-svn: 295322
diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp
index 25ea64a..d1f760d 100644
--- a/lld/ELF/OutputSections.cpp
+++ b/lld/ELF/OutputSections.cpp
@@ -119,10 +119,14 @@
if (!Config->copyRelocs() || (Type != SHT_RELA && Type != SHT_REL))
return;
+ InputSection<ELFT> *First = Sections[0];
+ if (isa<SyntheticSection<ELFT>>(First))
+ return;
+
this->Link = In<ELFT>::SymTab->OutSec->SectionIndex;
// sh_info for SHT_REL[A] sections should contain the section header index of
// the section to which the relocation applies.
- InputSectionBase<ELFT> *S = Sections[0]->getRelocatedSection();
+ InputSectionBase<ELFT> *S = First->getRelocatedSection();
this->Info = S->OutSec->SectionIndex;
}
diff --git a/lld/test/ELF/emit-relocs-shared.s b/lld/test/ELF/emit-relocs-shared.s
new file mode 100644
index 0000000..7a0d791
--- /dev/null
+++ b/lld/test/ELF/emit-relocs-shared.s
@@ -0,0 +1,16 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: ld.lld --emit-relocs %t.o -o %t.so -shared
+# RUN: llvm-readobj -r %t.so | FileCheck %s
+
+.data
+.quad foo
+
+# CHECK: Relocations [
+# CHECK-NEXT: Section (4) .rela.dyn {
+# CHECK-NEXT: 0x1000 R_X86_64_64 foo 0x0
+# CHECK-NEXT: }
+# CHECK-NEXT: Section (8) .rela.data {
+# CHECK-NEXT: 0x1000 R_X86_64_64 foo 0x0
+# CHECK-NEXT: }
+# CHECK-NEXT: ]