Skip to content

Commit

Permalink
[ELF] - Allow discarding .hash and .gnu.hash from linker script.
Browse files Browse the repository at this point in the history
Currently, LLD segfaults when linker script attempts to discard
one of the hash sections. This patch fixes that.

Differential revision: https://reviews.llvm.org/D44012

llvm-svn: 326891
  • Loading branch information
George Rimar committed Mar 7, 2018
1 parent 52ae4f0 commit 54baa5f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lld/ELF/LinkerScript.cpp
Expand Up @@ -380,6 +380,14 @@ void LinkerScript::discard(ArrayRef<InputSection *> V) {
S == InX::DynStrTab)
error("discarding " + S->Name + " section is not allowed");

// You can discard .hash and .gnu.hash sections by linker scripts. Since
// they are synthesized sections, we need to handle them differently than
// other regular sections.
if (S == InX::GnuHashTab)
InX::GnuHashTab = nullptr;
if (S == InX::HashTab)
InX::HashTab = nullptr;

S->Assigned = false;
S->Live = false;
discard(S->DependentSections);
Expand Down
23 changes: 23 additions & 0 deletions lld/test/ELF/linkerscript/discard-gnu-hash.s
@@ -0,0 +1,23 @@
# REQUIRES: x86
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t

# RUN: ld.lld --hash-style both -shared -o %t1 %t
# RUN: llvm-objdump -section-headers %t1 | FileCheck %s
# CHECK: .gnu.hash
# CHECK: .hash

# RUN: echo "SECTIONS { /DISCARD/ : { *(.hash) } }" > %t.script
# RUN: ld.lld --hash-style both -shared -o %t1 --script %t.script %t
# RUN: llvm-objdump -section-headers %t1 \
# RUN: | FileCheck %s --check-prefix=HASH
# HASH-NOT: .hash
# HASH: .gnu.hash
# HASH-NOT: .hash

# RUN: echo "SECTIONS { /DISCARD/ : { *(.gnu.hash) } }" > %t.script
# RUN: ld.lld --hash-style both -shared -o %t1 --script %t.script %t
# RUN: llvm-objdump -section-headers %t1 \
# RUN: | FileCheck %s --check-prefix=GNUHASH
# GNUHASH-NOT: .gnu.hash
# GNUHASH: .hash
# GNUHASH-NOT: .gnu.hash

0 comments on commit 54baa5f

Please sign in to comment.