[PATCH] D69364: [ELF] -r: fix crash when processing a SHT_REL[A] that relocates a SHF_MERGE after D67504/r372734

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 24 10:33:18 PDT 2019


MaskRay updated this revision to Diff 226289.
MaskRay edited the summary of this revision.
MaskRay added a comment.

Update description


Repository:
  rLLD LLVM Linker

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D69364/new/

https://reviews.llvm.org/D69364

Files:
  ELF/InputFiles.cpp
  test/ELF/merge-relocatable.s


Index: test/ELF/merge-relocatable.s
===================================================================
--- /dev/null
+++ test/ELF/merge-relocatable.s
@@ -0,0 +1,25 @@
+# REQUIRES: x86
+
+## Test we keep a SHT_REL[A] section that relocates a SHF_MERGE section in -r mode.
+## The relocated SHF_MERGE section is not merged.
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: ld.lld -r %t.o -o %t
+# RUN: llvm-readobj -S %t | FileCheck %s
+
+# CHECK:     Name: .rodata.cst8
+# CHECK-NOT: }
+# CHECK:     Size: 16
+# CHECK:     Name: .rela.rodata.cst8
+# CHECK-NOT: }
+# CHECK:     Size: 48
+
+foo:
+
+.section .rodata.cst8,"aM", at progbits,8,unique,0
+bar:
+.quad foo
+
+.section .rodata.cst8,"aM", at progbits,8,unique,1
+baz:
+.quad foo
Index: ELF/InputFiles.cpp
===================================================================
--- ELF/InputFiles.cpp
+++ ELF/InputFiles.cpp
@@ -838,6 +838,16 @@
     if (!target)
       return nullptr;
 
+    // ELF spec allows mergeable sections with relocations, but they are
+    // rare, and it is in practice hard to merge such sections by contents,
+    // because applying relocations at end of linking changes section
+    // contents. So, we simply handle such sections as non-mergeable ones.
+    // Degrading like this is acceptable because section merging is optional.
+    if (auto *ms = dyn_cast<MergeInputSection>(target)) {
+      target = toRegularSection(ms);
+      this->sections[sec.sh_info] = target;
+    }
+
     // This section contains relocation information.
     // If -r is given, we do not interpret or apply relocation
     // but just copy relocation sections to output.
@@ -856,16 +866,6 @@
       fatal(toString(this) +
             ": multiple relocation sections to one section are not supported");
 
-    // ELF spec allows mergeable sections with relocations, but they are
-    // rare, and it is in practice hard to merge such sections by contents,
-    // because applying relocations at end of linking changes section
-    // contents. So, we simply handle such sections as non-mergeable ones.
-    // Degrading like this is acceptable because section merging is optional.
-    if (auto *ms = dyn_cast<MergeInputSection>(target)) {
-      target = toRegularSection(ms);
-      this->sections[sec.sh_info] = target;
-    }
-
     if (sec.sh_type == SHT_RELA) {
       ArrayRef<Elf_Rela> rels = CHECK(getObj().relas(&sec), this);
       target->firstRelocation = rels.begin();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69364.226289.patch
Type: text/x-patch
Size: 2472 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191024/0dce86eb/attachment.bin>


More information about the llvm-commits mailing list