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,6 +1298,8 @@ 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(S->body()); @@ -1266,6 +1307,14 @@ 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; 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