Skip to content

Commit

Permalink
ELF: Do not create relative relocations for undefined symbols.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
pcc committed Apr 13, 2016
1 parent f1c23dc commit 1f71d74
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lld/ELF/Writer.cpp
Expand Up @@ -573,6 +573,10 @@ void Writer<ELFT>::scanRelocs(InputSectionBase<ELFT> &C, ArrayRef<RelTy> Rels) {
DynType = Target->TlsGotRel;
else if (Preemptible)
DynType = Target->GotRel;
else if (Body.isUndefined())
// Weak undefined symbols evaluate to zero, so don't create
// relocations for them.
continue;
else
DynType = Target->RelativeRel;
AddDyn({DynType, Out<ELFT>::Got, Body.getGotOffset<ELFT>(),
Expand Down
16 changes: 16 additions & 0 deletions lld/test/ELF/pie-weak.s
@@ -0,0 +1,16 @@
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
# RUN: ld.lld -pie %t.o -o %t
# RUN: llvm-readobj -r %t | FileCheck --check-prefix=RELOCS %s
# RUN: llvm-objdump -d %t | FileCheck --check-prefix=DISASM %s

# RELOCS: Relocations [
# RELOCS-NEXT: ]

.weak foo

.globl _start
_start:
# DISASM: _start:
# DISASM-NEXT: 1000: 48 8b 05 69 10 00 00 movq 4201(%rip), %rax
# ^ .got - (.text + 7)
mov foo@gotpcrel(%rip), %rax

0 comments on commit 1f71d74

Please sign in to comment.