Skip to content

Commit ba2de8f

Browse files
committed
[ELF] Allow absolute relocation referencing symbol index 0 in PIC mode
The value of an absolute relocation, like R_RISCV_HI20 or R_PPC64_LO16, with a symbol index of 0, the resulting value should be treated as absolute and permitted in both -pie and -shared links. This change also resolves an absolute relocation referencing an undefined symbol in statically-linked executables. PPC64 has unfortunate exceptions: * R_PPC64_TOCBASE uses symbol index 0 but it should be treated as referencing the linker-defined .TOC. * R_PPC64_PCREL_OPT (https://reviews.llvm.org/D84360) could no longer rely on `isAbsoluteValue` return false.
1 parent 1d4801f commit ba2de8f

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

lld/ELF/Relocations.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ static RelType getMipsPairType(RelType type, bool isLocal) {
178178
// True if non-preemptable symbol always has the same value regardless of where
179179
// the DSO is loaded.
180180
static bool isAbsolute(const Symbol &sym) {
181-
if (sym.isUndefWeak())
181+
if (sym.isUndefined())
182182
return true;
183183
if (const auto *dr = dyn_cast<Defined>(&sym))
184184
return dr->section == nullptr; // Absolute symbol.
@@ -1005,7 +1005,7 @@ bool RelocationScanner::isStaticLinkTimeConstant(RelExpr e, RelType type,
10051005

10061006
// For the target and the relocation, we want to know if they are
10071007
// absolute or relative.
1008-
bool absVal = isAbsoluteValue(sym);
1008+
bool absVal = isAbsoluteValue(sym) && e != RE_PPC64_TOCBASE;
10091009
bool relE = isRelExpr(e);
10101010
if (absVal && !relE)
10111011
return true;
@@ -1021,7 +1021,7 @@ bool RelocationScanner::isStaticLinkTimeConstant(RelExpr e, RelType type,
10211021
// calls to such symbols (e.g. glibc/stdlib/exit.c:__run_exit_handlers).
10221022
// Normally such a call will be guarded with a comparison, which will load a
10231023
// zero from the GOT.
1024-
if (sym.isUndefWeak())
1024+
if (sym.isUndefined())
10251025
return true;
10261026

10271027
// We set the final symbols values for linker script defined symbols later.
@@ -1066,7 +1066,8 @@ void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset,
10661066
type == R_HEX_GD_PLT_B22_PCREL_X ||
10671067
type == R_HEX_GD_PLT_B32_PCREL_X)))
10681068
expr = fromPlt(expr);
1069-
} else if (!isAbsoluteValue(sym)) {
1069+
} else if (!isAbsoluteValue(sym) ||
1070+
(type == R_PPC64_PCREL_OPT && ctx.arg.emachine == EM_PPC64)) {
10701071
expr = ctx.target->adjustGotPcExpr(type, addend,
10711072
sec->content().data() + offset);
10721073
// If the target adjusted the expression to R_RELAX_GOT_PC, we may end up

lld/test/ELF/riscv-relax-hi20-lo12-pie.s

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
# CHECK: lui a0, 0x200
1414
# CHECK-NEXT: addi a0, a0, 0x1
15+
# CHECK-NEXT: lui a0, 0x200
16+
# CHECK-NEXT: addi a0, a0, 0x1
1517
# CHECK-NEXT: lw a0, 0x1(a0)
1618
# CHECK-NEXT: sw a0, 0x1(a0)
1719

@@ -23,6 +25,11 @@ abs = 0x200001
2325
_start:
2426
lui a0, %hi(abs)
2527
addi a0, a0, %lo(abs)
28+
.reloc ., R_RISCV_HI20, abs
29+
lui a0, 0
30+
.reloc ., R_RISCV_LO12_I, abs
31+
addi a0, a0, 0
32+
2633
lw a0, %lo(abs)(a0)
2734
sw a0, %lo(abs)(a0)
2835

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
2929
# RUN: ld.lld a.o b.o -o out1 -z undefs
3030
# RUN: llvm-readelf -r -x .data out1 | FileCheck %s --check-prefix=STATIC1
3131
# RUN: ld.lld a.o b.o -o out1.pie -pie -z undefs
32-
# RUN: llvm-readelf -r -x .data out1.pie | FileCheck %s --check-prefix=STATIC2
32+
# RUN: llvm-readelf -r -x .data out1.pie | FileCheck %s --check-prefix=STATIC1
3333

3434
# STATIC1: no relocations
35-
# STATIC2: R_X86_64_RELATIVE
35+
# STATIC1: Hex dump of section '.data':
36+
# STATIC1-NEXT: {{.*}} 00000000 00000000 00000000 00000000 .
37+
# STATIC1-EMPTY:
3638

3739
# RUN: ld.lld a.o b.o c.o -pie -z undefs 2>&1 | count 0
3840

0 commit comments

Comments
 (0)