Skip to content

Commit 1f71d74

Browse files
committed
ELF: Do not create relative relocations for undefined symbols.
We need to ensure that the address of an undefined weak symbol evaluates to zero. We were getting this right for non-PIC executables (where the symbol can be evaluated directly) and for DSOs (where we emit a symbolic relocation for these symbols, as they are preemptible). But we weren't getting it right for PIEs. Probably the simplest way to ensure that these symbols evaluate to zero is by not creating a relocation in .got for them. Differential Revision: http://reviews.llvm.org/D19044 llvm-svn: 266161
1 parent f1c23dc commit 1f71d74

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lld/ELF/Writer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,10 @@ void Writer<ELFT>::scanRelocs(InputSectionBase<ELFT> &C, ArrayRef<RelTy> Rels) {
573573
DynType = Target->TlsGotRel;
574574
else if (Preemptible)
575575
DynType = Target->GotRel;
576+
else if (Body.isUndefined())
577+
// Weak undefined symbols evaluate to zero, so don't create
578+
// relocations for them.
579+
continue;
576580
else
577581
DynType = Target->RelativeRel;
578582
AddDyn({DynType, Out<ELFT>::Got, Body.getGotOffset<ELFT>(),

lld/test/ELF/pie-weak.s

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
2+
# RUN: ld.lld -pie %t.o -o %t
3+
# RUN: llvm-readobj -r %t | FileCheck --check-prefix=RELOCS %s
4+
# RUN: llvm-objdump -d %t | FileCheck --check-prefix=DISASM %s
5+
6+
# RELOCS: Relocations [
7+
# RELOCS-NEXT: ]
8+
9+
.weak foo
10+
11+
.globl _start
12+
_start:
13+
# DISASM: _start:
14+
# DISASM-NEXT: 1000: 48 8b 05 69 10 00 00 movq 4201(%rip), %rax
15+
# ^ .got - (.text + 7)
16+
mov foo@gotpcrel(%rip), %rax

0 commit comments

Comments
 (0)