Skip to content

Commit 08c915f

Browse files
committed
[ELF] -z notext: avoid dynamic relocations in .eh_frame
Fix #60392 ``` // a.cc void raise() { throw 42; } bool foo() { try { raise(); } catch (int) { return true; } return false; } int main() { foo(); } ``` ``` clang++ --target=x86_64-linux-gnu -fno-pic -mcmodel=large -no-pie -fuse-ld=lld -z notext a.cc -o a && ./a clang++ --target=aarch64-linux-gnu -fno-pic -no-pie -fuse-ld=lld -Wl,--dynamic-linker=/usr/aarch64-linux-gnu/lib/ld-linux-aarch64.so.1 -Wl,-rpath=/usr/aarch64-linux-gnu/lib -z notext a.cc -o a && ./a ``` Both commands fail because we produce a dynamic relocation for R_X86_64_64/R_AARCH64_ABS64 in .eh_frame which will be adjusted to a wrong offset by `SectionBase::getOffset` after D122459. Since GNU ld uses a canonical PLT entry instead of a dynamic relocation for .eh_frame, we follow suit as well to avoid the issue. Mips has an ABI issue (#5837) and we don't implement GNU ld's DW_EH_PE_absptr conversion. mips64-eh-abs-reloc.s wants a dynamic relocation, so keep the original behavior for EM_MIPS. Differential Revision: https://reviews.llvm.org/D143136
1 parent 98f0e4f commit 08c915f

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

lld/ELF/Relocations.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,15 @@ void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset,
10791079
return;
10801080
}
10811081

1082-
bool canWrite = (sec->flags & SHF_WRITE) || !config->zText;
1082+
// Use a simple -z notext rule that treats all sections except .eh_frame as
1083+
// writable. GNU ld does not produce dynamic relocations in .eh_frame (and our
1084+
// SectionBase::getOffset would incorrectly adjust the offset).
1085+
//
1086+
// For MIPS, we don't implement GNU ld's DW_EH_PE_absptr to DW_EH_PE_pcrel
1087+
// conversion. We still emit a dynamic relocation.
1088+
bool canWrite = (sec->flags & SHF_WRITE) ||
1089+
!(config->zText ||
1090+
(isa<EhInputSection>(sec) && config->emachine != EM_MIPS));
10831091
if (canWrite) {
10841092
RelType rel = target->getDynRel(type);
10851093
if (expr == R_GOT || (rel == target->symbolicRel && !sym.isPreemptible)) {

lld/test/ELF/eh-frame-znotext.s

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# REQUIRES: aarch64
2+
## While a symbolic relocation for -z notext in .eh_frame can emit a dynamic
3+
## relocation, we try avoiding that (https://github.com/llvm/llvm-project/issues/60392)
4+
## and use a canonical PLT entry instead.
5+
6+
# RUN: rm -rf %t && split-file %s %t && cd %t
7+
# RUN: llvm-mc -filetype=obj -triple=aarch64 a.s -o a.o
8+
# RUN: llvm-mc -filetype=obj -triple=aarch64 abi.s -o abi.o
9+
# RUN: ld.lld -shared abi.o -o abi.so
10+
11+
# RUN: ld.lld a.o abi.so -o a
12+
# RUN: llvm-readelf -r --dyn-syms a | FileCheck %s
13+
# RUN: ld.lld -z notext a.o abi.so -o a
14+
# RUN: llvm-readelf -r --dyn-syms a | FileCheck %s
15+
16+
# CHECK: R_AARCH64_JUMP_SLOT {{.*}} __gxx_personality_v0 + 0
17+
18+
# CHECK: 1: 00000000002{{.*}} 0 FUNC GLOBAL DEFAULT UND __gxx_personality_v0
19+
20+
#--- a.s
21+
foo:
22+
.cfi_startproc
23+
.cfi_personality 0, __gxx_personality_v0
24+
ret
25+
.cfi_endproc
26+
27+
#--- abi.s
28+
.globl __gxx_personality_v0
29+
.type __gxx_personality_v0, @function
30+
__gxx_personality_v0:

0 commit comments

Comments
 (0)