diff options
author | Fangrui Song <i@maskray.me> | 2023-11-17 22:38:15 -0800 |
---|---|---|
committer | Fangrui Song <i@maskray.me> | 2023-11-17 22:38:15 -0800 |
commit | b8dface221f4490933b0d39deb769e97ca134e5f (patch) | |
tree | adbd589ca1b1883c68074e077d1a5891d5e3505a | |
parent | 2b71f91b06ad4f5a0c54725b06283fd731620b92 (diff) |
[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
-rw-r--r-- | lld/ELF/LinkerScript.cpp | 10 | ||||
-rw-r--r-- | lld/test/ELF/linkerscript/emit-reloc-section-names.s | 2 | ||||
-rw-r--r-- | lld/test/ELF/linkerscript/emit-relocs-multiple.s | 2 |
3 files changed, 9 insertions, 5 deletions
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index 1b8acbe1c908..28ae4b854306 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 805d3c2cad4b..fee28a16b40a 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 dcf40b3d9312..0036e8f67f04 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 { |