diff --git a/lld/ELF/Symbols.cpp b/lld/ELF/Symbols.cpp --- a/lld/ELF/Symbols.cpp +++ b/lld/ELF/Symbols.cpp @@ -592,7 +592,14 @@ return; if (cmp > 0) { - replace(other); + if (auto *s = dyn_cast(this)) { + uint64_t size = s->size; + replace(other); + if (size > cast(this)->size) + cast(this)->size = size; + } else { + replace(other); + } return; } @@ -633,6 +640,11 @@ } void Symbol::resolveShared(const SharedSymbol &other) { + if (isCommon()) { + if (other.size > cast(this)->size) + cast(this)->size = other.size; + return; + } if (visibility == STV_DEFAULT && (isUndefined() || isLazy())) { // An undefined symbol with non default visibility must be satisfied // in the same DSO. diff --git a/lld/test/ELF/common-shared.s b/lld/test/ELF/common-shared.s new file mode 100644 --- /dev/null +++ b/lld/test/ELF/common-shared.s @@ -0,0 +1,16 @@ +# REQUIRES: x86 +## When a common symbol is merged with a shared symbol, pick the larger st_size. + +# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o +# RUN: echo '.globl com; .comm com, 16' | llvm-mc -filetype=obj -triple=x86_64 - -o %t1.o +# RUN: ld.lld -shared %t1.o -o %t1.so + +# RUN: ld.lld %t.o %t1.so -o %t +# RUN: llvm-readelf -s %t | FileCheck %s +# RUN: ld.lld %t1.so %t.o -o %t +# RUN: llvm-readelf -s %t | FileCheck %s + +# CHECK: 16 OBJECT GLOBAL DEFAULT 7 com + +.globl com +.comm com,1