[lld] 69d10d2 - [ELF] Update st_size when merging a common symbol with a shared symbol

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 13 09:23:44 PST 2019


Author: Fangrui Song
Date: 2019-12-13T09:23:36-08:00
New Revision: 69d10d282e5896821e16d4bc5dd190adc0131520

URL: https://github.com/llvm/llvm-project/commit/69d10d282e5896821e16d4bc5dd190adc0131520
DIFF: https://github.com/llvm/llvm-project/commit/69d10d282e5896821e16d4bc5dd190adc0131520.diff

LOG: [ELF] Update st_size when merging a common symbol with a shared symbol

When a common symbol is merged with a shared symbol, increase st_size if
the shared symbol has a larger st_size. At runtime, the executable's
symbol overrides the shared symbol.  The shared symbol may be created
from common symbols in a previous link.  This rule makes sure we pick
the largest size among all common symbols.

This behavior matches GNU ld. See
https://sourceware.org/bugzilla/show_bug.cgi?id=25236 for discussions.

A shared symbol does not hold alignment constraints. Ignore the
alignment update.

Reviewed By: peter.smith

Differential Revision: https://reviews.llvm.org/D71161

Added: 
    lld/test/ELF/common-shared.s

Modified: 
    lld/ELF/Symbols.cpp

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Symbols.cpp b/lld/ELF/Symbols.cpp
index b5adbdca4a86..f81e73e5091b 100644
--- a/lld/ELF/Symbols.cpp
+++ b/lld/ELF/Symbols.cpp
@@ -620,7 +620,18 @@ void Symbol::resolveCommon(const CommonSymbol &other) {
     return;
 
   if (cmp > 0) {
-    replace(other);
+    if (auto *s = dyn_cast<SharedSymbol>(this)) {
+      // Increase st_size if the shared symbol has a larger st_size. The shared
+      // symbol may be created from common symbols. The fact that some object
+      // files were linked into a shared object first should not change the
+      // regular rule that picks the largest st_size.
+      uint64_t size = s->size;
+      replace(other);
+      if (size > cast<CommonSymbol>(this)->size)
+        cast<CommonSymbol>(this)->size = size;
+    } else {
+      replace(other);
+    }
     return;
   }
 
@@ -661,6 +672,12 @@ template <class LazyT> void Symbol::resolveLazy(const LazyT &other) {
 }
 
 void Symbol::resolveShared(const SharedSymbol &other) {
+  if (isCommon()) {
+    // See the comment in resolveCommon() above.
+    if (other.size > cast<CommonSymbol>(this)->size)
+      cast<CommonSymbol>(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
index 000000000000..c53c5bf0ef7d
--- /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


        


More information about the llvm-commits mailing list