diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -2155,7 +2155,7 @@ // Adding a local symbol to a .dynsym is a bug. assert(this->type != SHT_DYNSYM || !b->isLocal()); - bool hashIt = b->isLocal(); + bool hashIt = b->isLocal() && config->optimize >= 2; symbols.push_back({b, strTabSec.addString(b->getName(), hashIt)}); } diff --git a/lld/test/ELF/strtab-dedup.s b/lld/test/ELF/strtab-dedup.s new file mode 100644 --- /dev/null +++ b/lld/test/ELF/strtab-dedup.s @@ -0,0 +1,20 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o + +## By default local symbol names are not deduplicated. +# RUN: ld.lld %t.o %t.o -o %t +# RUN: llvm-readelf -p .strtab %t | FileCheck %s --check-prefix=NODEDUP + +# NODEDUP: [ 1] local +# NODEDUP-NEXT: [ 7] local +# NODEDUP-EMPTY: + +## -O2 deduplicates local symbol names. +# RUN: ld.lld -O2 %t.o %t.o -o %t +# RUN: llvm-readelf -p .strtab %t | FileCheck %s --check-prefix=DEDUP + +# DEDUP: [ 1] local +# DEDUP-EMPTY: + +local: + ret