[PATCH] [lld][Core] Allow weak symbols in shared library when linking.

Rui Ueyama ruiu at google.com
Mon Apr 22 02:12:18 PDT 2013


  Added a test.

Hi kledzik,

http://llvm-reviews.chandlerc.com/D694

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D694?vs=1692&id=1707#toc

Files:
  lib/Core/Resolver.cpp
  test/elf/Inputs/shared.c
  test/elf/Inputs/shared.so-x86-64
  test/elf/dynamic-undef.test

Index: lib/Core/Resolver.cpp
===================================================================
--- lib/Core/Resolver.cpp
+++ lib/Core/Resolver.cpp
@@ -315,17 +315,19 @@
     bool foundUndefines = false;
     for (const UndefinedAtom *undefAtom : undefinedAtoms) {
       const File &f = undefAtom->file();
-      bool isAtomUndefined = false;
-      if (isa<SharedLibraryFile>(f)) {
-        if (!_targetInfo.allowShlibUndefines()) {
-          foundUndefines = true;
-          isAtomUndefined = true;
-        }
-      } else if (undefAtom->canBeNull() == UndefinedAtom::canBeNullNever) {
-        foundUndefines = true;
-        isAtomUndefined = true;
-      }
-      if (isAtomUndefined && _targetInfo.printRemainingUndefines()) {
+
+      // Skip over a weak symbol.
+      if (undefAtom->canBeNull() != UndefinedAtom::canBeNullNever)
+        continue;
+
+      // If this is a library and undefined symbols are allowed on the
+      // target platform, skip over it.
+      if (isa<SharedLibraryFile>(f) && _targetInfo.allowShlibUndefines())
+        continue;
+
+      // Seems like this symbol is undefined. Warn that.
+      foundUndefines = true;
+      if (_targetInfo.printRemainingUndefines()) {
         llvm::errs() << "Undefined Symbol: " << undefAtom->file().path()
                      << " : " << undefAtom->name() << "\n";
       }
Index: test/elf/Inputs/shared.c
===================================================================
--- test/elf/Inputs/shared.c
+++ test/elf/Inputs/shared.c
@@ -3,6 +3,14 @@
 extern int i;
 int i = 42;
 
+// Undefined weak function in a dynamic library.
+__attribute__((weak)) void weakfoo();
+
+// Regular funtion in a dynamic library.
 void foo() {
+  // Try to call weakfoo so that the reference to weekfoo will be included in
+  // the resulting .so file.
+  if (weakfoo)
+    weakfoo();
   puts("Fooo!!");
 }
Index: test/elf/dynamic-undef.test
===================================================================
--- test/elf/dynamic-undef.test
+++ test/elf/dynamic-undef.test
@@ -25,3 +25,5 @@
 
 EXEC: Undefined Symbol: {{[-_A-Za-z0-9.\\/]+}}shared.so-x86-64 : puts
 SHLIB: Undefined Symbol: {{[-_A-Za-z0-9.\\/]+}}shared.so-x86-64 : puts
+EXEC-NOT: Undefined Symbol: {{[-_A-Za-z0-9.\\/]+}}shared.so-x86-64 : weakfoo
+SHLIB-NOT: Undefined Symbol: {{[-_A-Za-z0-9.\\/]+}}shared.so-x86-64 : weakfoo
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D694.2.patch
Type: text/x-patch
Size: 2359 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130422/46df5d54/attachment.bin>


More information about the llvm-commits mailing list