Skip to content

Commit 2fcaa00

Browse files
committed
[ELF] -z undefs: handle relocations referencing undefined non-weak like undefined weak
* Merge the special case into isStaticLinkTimeConstant * Generalize isUndefWeak to isUndefined. undefined non-weak is an error case. We choose to be general, which also brings us in line with GNU ld.
1 parent 968d8ea commit 2fcaa00

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

lld/ELF/Relocations.cpp

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -990,10 +990,17 @@ bool RelocationScanner::isStaticLinkTimeConstant(RelExpr e, RelType type,
990990
// only the low bits are used.
991991
if (e == R_GOT || e == R_PLT)
992992
return ctx.target->usesOnlyLowPageBits(type) || !ctx.arg.isPic;
993-
994993
// R_AARCH64_AUTH_ABS64 requires a dynamic relocation.
995-
if (sym.isPreemptible || e == RE_AARCH64_AUTH)
994+
if (e == RE_AARCH64_AUTH)
996995
return false;
996+
997+
// The behavior of an undefined weak reference is implementation defined.
998+
// (We treat undefined non-weak the same as undefined weak.) For static
999+
// -no-pie linking, dynamic relocations are generally avoided (except
1000+
// IRELATIVE). Emitting dynamic relocations for -shared aligns with its -z
1001+
// undefs default. Dynamic -no-pie linking and -pie allow flexibility.
1002+
if (sym.isPreemptible)
1003+
return sym.isUndefined() && !ctx.arg.isPic;
9971004
if (!ctx.arg.isPic)
9981005
return true;
9991006

@@ -1113,19 +1120,7 @@ void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset,
11131120
// If the relocation is known to be a link-time constant, we know no dynamic
11141121
// relocation will be created, pass the control to relocateAlloc() or
11151122
// relocateNonAlloc() to resolve it.
1116-
//
1117-
// The behavior of an undefined weak reference is implementation defined. For
1118-
// non-link-time constants, we resolve relocations statically (let
1119-
// relocate{,Non}Alloc() resolve them) for -no-pie and try producing dynamic
1120-
// relocations for -pie and -shared.
1121-
//
1122-
// The general expectation of -no-pie static linking is that there is no
1123-
// dynamic relocation (except IRELATIVE). Emitting dynamic relocations for
1124-
// -shared matches the spirit of its -z undefs default. -pie has freedom on
1125-
// choices, and we choose dynamic relocations to be consistent with the
1126-
// handling of GOT-generating relocations.
1127-
if (isStaticLinkTimeConstant(expr, type, sym, offset) ||
1128-
(!ctx.arg.isPic && sym.isUndefWeak())) {
1123+
if (isStaticLinkTimeConstant(expr, type, sym, offset)) {
11291124
sec->addReloc({expr, type, offset, addend, &sym});
11301125
return;
11311126
}

lld/test/ELF/weak-undef-rw.s

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,21 @@
3333
# RUN: ld.lld a.o b.o -o ab -z undefs
3434
# RUN: llvm-readelf -r -x .data ab | FileCheck %s --check-prefix=STATIC1
3535
# RUN: ld.lld a.o b.o s.so -o abs -z undefs
36-
# RUN: llvm-readelf -r -x .data abs | FileCheck %s --check-prefix=DYN1
37-
# RUN: ld.lld a.o b.o -o abs.pie -pie -z undefs
38-
# RUN: llvm-readelf -r -x .data abs.pie | FileCheck %s --check-prefix=STATIC1
36+
# RUN: llvm-readelf -r -x .data abs | FileCheck %s --check-prefix=STATIC1
37+
# RUN: ld.lld a.o b.o -o ab.pie -pie -z undefs
38+
# RUN: llvm-readelf -r -x .data ab.pie | FileCheck %s --check-prefix=STATIC1
39+
# RUN: ld.lld a.o b.o s.so -o abs.pie -pie -z undefs
40+
# RUN: llvm-readelf -r -x .data abs.pie | FileCheck %s --check-prefix=DYN1
3941

4042
# STATIC1: no relocations
4143
# STATIC1: Hex dump of section '.data':
4244
# STATIC1-NEXT: {{.*}} 00000000 00000000 03000000 00000000 .
4345
# STATIC1-NEXT: {{.*}} 05000000 00000000 .
4446
# STATIC1-EMPTY:
4547

46-
# DYN1: Relocation section '.rela.dyn' {{.*}} contains 1
48+
# DYN1: Relocation section '.rela.dyn' {{.*}} contains 3
4749
# DYN1: Hex dump of section '.data':
48-
# DYN1-NEXT: {{.*}} 00000000 00000000 03000000 00000000 .
50+
# DYN1-NEXT: {{.*}} 00000000 00000000 00000000 00000000 .
4951
# DYN1-NEXT: {{.*}} 00000000 00000000 .
5052
# DYN1-EMPTY:
5153

0 commit comments

Comments
 (0)