summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFangrui Song <i@maskray.me>2023-02-22 23:34:49 -0800
committerTobias Hieta <tobias@hieta.se>2023-02-24 09:42:46 +0100
commite3ce92b7a904c2b4f0acebdf433121b2e45e5f0c (patch)
tree5a6448f76451a069082711a09071a9384dfdd894
parent91172bd36927e9444ee9e9c80e489a1e6a13c72e (diff)
downloadllvm-e3ce92b7a904c2b4f0acebdf433121b2e45e5f0c.tar.gz
[ELF] --icf: don't fold a section without relocation and a section with relocations for SHT_REL
Fix https://github.com/llvm/llvm-project/issues/57693 (cherry picked from commit 686cff17cc310884e48ae963bf7507f96950cc90)
-rw-r--r--lld/ELF/ICF.cpp10
-rw-r--r--lld/test/ELF/icf10.s9
2 files changed, 13 insertions, 6 deletions
diff --git a/lld/ELF/ICF.cpp b/lld/ELF/ICF.cpp
index d7a85bcf844f..c2b0ce9b12b9 100644
--- a/lld/ELF/ICF.cpp
+++ b/lld/ELF/ICF.cpp
@@ -323,8 +323,9 @@ bool ICF<ELFT>::equalsConstant(const InputSection *a, const InputSection *b) {
const RelsOrRelas<ELFT> ra = a->template relsOrRelas<ELFT>();
const RelsOrRelas<ELFT> rb = b->template relsOrRelas<ELFT>();
- return ra.areRelocsRel() ? constantEq(a, ra.rels, b, rb.rels)
- : constantEq(a, ra.relas, b, rb.relas);
+ return ra.areRelocsRel() || rb.areRelocsRel()
+ ? constantEq(a, ra.rels, b, rb.rels)
+ : constantEq(a, ra.relas, b, rb.relas);
}
// Compare two lists of relocations. Returns true if all pairs of
@@ -371,8 +372,9 @@ template <class ELFT>
bool ICF<ELFT>::equalsVariable(const InputSection *a, const InputSection *b) {
const RelsOrRelas<ELFT> ra = a->template relsOrRelas<ELFT>();
const RelsOrRelas<ELFT> rb = b->template relsOrRelas<ELFT>();
- return ra.areRelocsRel() ? variableEq(a, ra.rels, b, rb.rels)
- : variableEq(a, ra.relas, b, rb.relas);
+ return ra.areRelocsRel() || rb.areRelocsRel()
+ ? variableEq(a, ra.rels, b, rb.rels)
+ : variableEq(a, ra.relas, b, rb.relas);
}
template <class ELFT> size_t ICF<ELFT>::findBoundary(size_t begin, size_t end) {
diff --git a/lld/test/ELF/icf10.s b/lld/test/ELF/icf10.s
index b782f9248448..3c18c431c3b9 100644
--- a/lld/test/ELF/icf10.s
+++ b/lld/test/ELF/icf10.s
@@ -1,5 +1,7 @@
# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=i686 %s -o %t.o
+# RUN: ld.lld --icf=all %t.o -o /dev/null --print-icf-sections 2>&1 | FileCheck %s
# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-freebsd %s -o %t.o
# RUN: ld.lld --icf=all %t.o -o /dev/null --print-icf-sections 2>&1 | FileCheck %s
@@ -8,10 +10,13 @@
# CHECK-NOT: selected
+.section .text.orig,"ax"
+ .quad -1
+
.section .text.foo,"ax"
.quad -1
- .reloc 0, R_X86_64_NONE, 0
+ .reloc 0, BFD_RELOC_NONE, 0
.section .text.bar,"ax"
.quad -1
- .reloc 1, R_X86_64_NONE, 0
+ .reloc 1, BFD_RELOC_NONE, 0