[PATCH] D38937: [LLD] [COFF] Exclude certain static libraries and object files when exporting all symbols

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 15 14:12:01 PDT 2017


mstorsjo created this revision.

This more or less matches what GNU ld does.

The clang_rt.builtins-$arch cases are a bit problematic; an alternative would be to trim the library name at the first period, and just have it match against libclang_rt.


https://reviews.llvm.org/D38937

Files:
  COFF/Driver.cpp
  test/COFF/export-all.s


Index: test/COFF/export-all.s
===================================================================
--- test/COFF/export-all.s
+++ test/COFF/export-all.s
@@ -36,3 +36,21 @@
 # CHECK2-DEF: exportfn1 @3
 # CHECK2-DEF: exportfn2 @4
 # CHECK2-DEF: exportfn3 @5
+
+# Test ignoring certain object files and libs.
+
+# RUN: echo -e ".global foobar\n.global DllMainCRTStartup\n.text\nDllMainCRTStartup:\nret\nfoobar:\ncall mingwfunc\ncall crtfunc\nret\n" > %t.main.s
+# RUN: llvm-mc -triple=x86_64-windows-gnu %t.main.s -filetype=obj -o %t.main.obj
+# RUN: mkdir -p %T/libs
+# RUN: echo -e ".global mingwfunc\n.text\nmingwfunc:\nret\n" > %T/libs/mingwfunc.s
+# RUN: llvm-mc -triple=x86_64-windows-gnu %T/libs/mingwfunc.s -filetype=obj -o %T/libs/mingwfunc.o
+# RUN: llvm-ar rcs %T/libs/libmingwex.a %T/libs/mingwfunc.o
+# RUN: echo -e ".global crtfunc\n.text\ncrtfunc:\nret\n" > %T/libs/crtfunc.s
+# RUN: llvm-mc -triple=x86_64-windows-gnu %T/libs/crtfunc.s -filetype=obj -o %T/libs/crt2.o
+# RUN: lld-link -out:%t.dll -dll -entry:DllMainCRTStartup %t.main.obj -lldmingw %T/libs/crt2.o %T/libs/libmingwex.a -output-def:%t.def
+# RUN: echo "EOF" >> %t.def
+# RUN: cat %t.def | FileCheck -check-prefix=CHECK-EXCLUDE %s
+
+# CHECK-EXCLUDE: EXPORTS
+# CHECK-EXCLUDE-NEXT: foobar @1
+# CHECK-EXCLUDE-NEXT: EOF
Index: COFF/Driver.cpp
===================================================================
--- COFF/Driver.cpp
+++ COFF/Driver.cpp
@@ -584,6 +584,45 @@
   };
 }
 
+// Get a set of libs to not automatically export
+// when exporting all global symbols for MinGW.
+static StringSet<> getExportExcludeLibs() {
+  return {
+      "libgcc",
+      "libgcc_s",
+      "libstdc++",
+      "libmingw32",
+      "libmingwex",
+      "libg2c",
+      "libsupc++",
+      "libobjc",
+      "libgcj",
+      "libclang_rt.builtins-aarch64",
+      "libclang_rt.builtins-arm",
+      "libclang_rt.builtins-i386",
+      "libclang_rt.builtins-x86_64",
+  };
+}
+
+// Get a set of object files to not automatically export
+// when exporting all global symbols for MinGW.
+static StringSet<> getExportExcludeObjects() {
+  return {
+      "crt0.o",
+      "crt1.o",
+      "crt1u.o",
+      "crt2.o",
+      "crt2u.o",
+      "dllcrt1.o",
+      "dllcrt2.o",
+      "gcrt0.o",
+      "gcrt1.o",
+      "gcrt2.o",
+      "crtbegin.o",
+      "crtend.o",
+  };
+}
+
 // This is MinGW specific.
 static void writeDefFile(StringRef Name) {
   std::error_code EC;
@@ -1259,13 +1298,23 @@
   if (Config->DLL && ((Config->MinGW && Config->Exports.empty()) ||
                       Args.hasArg(OPT_export_all_symbols))) {
     StringSet<> ExcludeSymbols = getExportExcludeSymbols();
+    StringSet<> ExcludeLibs = getExportExcludeLibs();
+    StringSet<> ExcludeObjects = getExportExcludeObjects();
 
     Symtab->forEachSymbol([=](Symbol *S) {
       auto *Def = dyn_cast<Defined>(S->body());
       if (!Def || !Def->isLive() || !Def->getChunk())
         return;
       if (ExcludeSymbols.count(Def->getName()))
         return;
+      StringRef LibName = sys::path::filename(Def->getFile()->ParentName);
+      // Drop the file extension.
+      LibName = LibName.substr(0, LibName.rfind('.'));
+      if (ExcludeLibs.count(LibName))
+        return;
+      StringRef FileName = sys::path::filename(Def->getFile()->getName());
+      if (LibName.empty() && ExcludeObjects.count(FileName))
+        return;
       Export E;
       E.Name = Def->getName();
       E.Sym = Def;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D38937.119097.patch
Type: text/x-patch
Size: 3450 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171015/08a8d311/attachment.bin>


More information about the llvm-commits mailing list