[lld] b8dface - [ELF] -r: rename orphan SHT_REL/SHT_RELA when the relocated input section is placed in an output section

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 17 22:38:23 PST 2023


Author: Fangrui Song
Date: 2023-11-17T22:38:15-08:00
New Revision: b8dface221f4490933b0d39deb769e97ca134e5f

URL: https://github.com/llvm/llvm-project/commit/b8dface221f4490933b0d39deb769e97ca134e5f
DIFF: https://github.com/llvm/llvm-project/commit/b8dface221f4490933b0d39deb769e97ca134e5f.diff

LOG: [ELF] -r: rename orphan SHT_REL/SHT_RELA when the relocated input section is placed in an output section

This ports https://reviews.llvm.org/D40652 (--emit-relocs) to -r and
matches GNU ld.
Close #67910

Added: 
    

Modified: 
    lld/ELF/LinkerScript.cpp
    lld/test/ELF/linkerscript/emit-reloc-section-names.s
    lld/test/ELF/linkerscript/emit-relocs-multiple.s

Removed: 
    


################################################################################
diff  --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 1b8acbe1c908b1d..28ae4b8543062f8 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -51,11 +51,8 @@ static bool isSectionPrefix(StringRef prefix, StringRef name) {
 }
 
 static StringRef getOutputSectionName(const InputSectionBase *s) {
-  if (config->relocatable)
-    return s->name;
-
-  // This is for --emit-relocs. If .text.foo is emitted as .text.bar, we want
-  // to emit .rela.text.foo as .rela.text.bar for consistency (this is not
+  // This is for --emit-relocs and -r. If .text.foo is emitted as .text.bar, we
+  // want to emit .rela.text.foo as .rela.text.bar for consistency (this is not
   // technically required, but not doing it is odd). This code guarantees that.
   if (auto *isec = dyn_cast<InputSection>(s)) {
     if (InputSectionBase *rel = isec->getRelocatedSection()) {
@@ -66,6 +63,9 @@ static StringRef getOutputSectionName(const InputSectionBase *s) {
     }
   }
 
+  if (config->relocatable)
+    return s->name;
+
   // A BssSection created for a common symbol is identified as "COMMON" in
   // linker scripts. It should go to .bss section.
   if (s->name == "COMMON")

diff  --git a/lld/test/ELF/linkerscript/emit-reloc-section-names.s b/lld/test/ELF/linkerscript/emit-reloc-section-names.s
index 805d3c2cad4b2fb..fee28a16b40aadb 100644
--- a/lld/test/ELF/linkerscript/emit-reloc-section-names.s
+++ b/lld/test/ELF/linkerscript/emit-reloc-section-names.s
@@ -3,6 +3,8 @@
 # RUN: echo "SECTIONS { .text.zed : { *(.text.foo) } .text.qux : { *(.text.bar) } }" > %t.script
 # RUN: ld.lld -T %t.script --emit-relocs %t.o -o %t
 # RUN: llvm-objdump --section-headers %t | FileCheck %s
+# RUN: ld.lld -T %t.script -r %t.o -o %t.ro
+# RUN: llvm-readelf -S %t.ro | FileCheck %s
 
 ## Check we name relocation sections in according to
 ## their target sections names.

diff  --git a/lld/test/ELF/linkerscript/emit-relocs-multiple.s b/lld/test/ELF/linkerscript/emit-relocs-multiple.s
index dcf40b3d9312f8e..0036e8f67f04cbf 100644
--- a/lld/test/ELF/linkerscript/emit-relocs-multiple.s
+++ b/lld/test/ELF/linkerscript/emit-relocs-multiple.s
@@ -3,6 +3,8 @@
 # RUN: echo "SECTIONS { .zed : { *(.foo) *(.bar) } }" > %t.script
 # RUN: ld.lld --emit-relocs --script %t.script %t.o -o %t1
 # RUN: llvm-readobj -r %t1 | FileCheck %s
+# RUN: ld.lld -r --script %t.script %t.o -o %t.ro
+# RUN: llvm-readobj -r %t.ro | FileCheck %s
 
 # CHECK:      Relocations [
 # CHECK-NEXT:   Section {{.*}} .rela.zed {


        


More information about the llvm-commits mailing list