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

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 6 22:43:32 PST 2019


MaskRay created this revision.
MaskRay added reviewers: grimar, peter.smith, ruiu.
Herald added subscribers: llvm-commits, arichardson, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D71161

Files:
  lld/ELF/Symbols.cpp
  lld/test/ELF/common-shared.s


Index: lld/test/ELF/common-shared.s
===================================================================
--- /dev/null
+++ 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
Index: lld/ELF/Symbols.cpp
===================================================================
--- lld/ELF/Symbols.cpp
+++ lld/ELF/Symbols.cpp
@@ -592,7 +592,14 @@
     return;
 
   if (cmp > 0) {
-    replace(other);
+    if (auto *s = dyn_cast<SharedSymbol>(this)) {
+      uint64_t size = s->size;
+      replace(other);
+      if (size > cast<CommonSymbol>(this)->size)
+        cast<CommonSymbol>(this)->size = size;
+    } else {
+      replace(other);
+    }
     return;
   }
 
@@ -633,6 +640,11 @@
 }
 
 void Symbol::resolveShared(const SharedSymbol &other) {
+  if (isCommon()) {
+    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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D71161.232687.patch
Type: text/x-patch
Size: 1557 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191207/b11b6aaa/attachment.bin>


More information about the llvm-commits mailing list