Browse Source

bindexplib: Do not export symbols from managed code

Fixes: #20653
Markus Mayer 5 years ago
parent
commit
be75622e49

+ 10 - 2
Source/bindexplib.cxx

@@ -276,8 +276,9 @@ public:
               symbol.compare(0, 4, vectorPrefix)) {
             SectChar = this->SectionHeaders[pSymbolTable->SectionNumber - 1]
                          .Characteristics;
-            // skip symbols containing a dot
-            if (symbol.find('.') == std::string::npos) {
+            // skip symbols containing a dot or are from managed code
+            if (symbol.find('.') == std::string::npos &&
+                !SymbolIsFromManagedCode(symbol)) {
               if (!pSymbolTable->Type && (SectChar & IMAGE_SCN_MEM_WRITE)) {
                 // Read only (i.e. constants) must be excluded
                 this->DataSymbols.insert(symbol);
@@ -302,6 +303,13 @@ public:
   }
 
 private:
+  bool SymbolIsFromManagedCode(std::string const& symbol)
+  {
+    return symbol == "__t2m" || symbol == "__m2mep" || symbol == "__mep" ||
+      symbol.find("$$F") != std::string::npos ||
+      symbol.find("$$J") != std::string::npos;
+  }
+
   std::set<std::string>& Symbols;
   std::set<std::string>& DataSymbols;
   DWORD_PTR SymbolCount;

+ 5 - 1
Tests/RunCMake/AutoExportDll/AutoExport.cmake

@@ -5,6 +5,10 @@ add_subdirectory(sub)
 add_library(objlib OBJECT objlib.c)
 set_property(TARGET objlib PROPERTY POSITION_INDEPENDENT_CODE 1)
 add_library(autoexport SHARED hello.cxx world.cxx foo.c $<TARGET_OBJECTS:objlib>)
+add_library(autoexport3 SHARED cppCLI.cxx)
+if(MSVC AND NOT MSVC_VERSION VERSION_LESS 1600)
+  set_property(TARGET autoexport3 PROPERTY COMMON_LANGUAGE_RUNTIME "")
+endif()
 
 add_executable(say say.cxx)
 if(MSVC)
@@ -18,4 +22,4 @@ if(MSVC)
     target_compile_definitions(say PRIVATE HAS_JUSTNOP)
   endif()
 endif()
-target_link_libraries(say autoexport autoexport2)
+target_link_libraries(say autoexport autoexport2 autoexport3)

+ 22 - 0
Tests/RunCMake/AutoExportDll/cppCLI.cxx

@@ -0,0 +1,22 @@
+#include <stdio.h>
+
+#ifdef __cplusplus_cli
+#  include <msclr\marshal_cppstd.h>
+
+void cliFunction()
+{
+  System::String ^ result = "cliFunction";
+  result = result->Trim();
+  printf(msclr::interop::marshal_as<std::string>(result).c_str());
+}
+#else
+void cliFunction()
+{
+  printf("cliFunction (but /cli was not passed to the compiler)");
+}
+#endif
+
+void nonCliFunction()
+{
+  printf("nonCliFunction");
+}

+ 7 - 1
Tests/RunCMake/AutoExportDll/say.cxx

@@ -17,9 +17,11 @@ void justnop();
 }
 
 // test c++ functions
-// forward declare hello and world
+// forward declare hello, world, cliFunction and nonCliFunction
 void hello();
 void world();
+void cliFunction();
+void nonCliFunction();
 
 // test exports for executable target
 extern "C" {
@@ -44,6 +46,10 @@ int main()
   bar();
   objlib();
   printf("\n");
+  cliFunction();
+  printf("\n");
+  nonCliFunction();
+  printf("\n");
 #ifdef HAS_JUSTNOP
   justnop();
 #endif