Limit the libcoreclr.so exports to the required minimum
authorJan Vorlicek <janvorli@microsoft.com>
Thu, 28 May 2015 00:15:00 +0000 (02:15 +0200)
committerJan Vorlicek <janvorli@microsoft.com>
Fri, 29 May 2015 09:13:46 +0000 (11:13 +0200)
This change limits the libcoreclr.so exports on Unix to functions that are PInvoked
by the managed mscorlib / corefx and the DllMain, CoreDllMain and ExecuteAssembly.

CMakeLists.txt
generateexportedsymbols.awk [new file with mode: 0644]
generateversionscript.awk [new file with mode: 0644]
src/dlls/mscoree/CMakeLists.txt
src/dlls/mscoree/coreclr/CMakeLists.txt
src/dlls/mscoree/mscorwks_unixexports.src [new file with mode: 0644]
src/dlls/mscorrc/CMakeLists.txt

index f757063..652da4f 100644 (file)
@@ -49,6 +49,12 @@ if(WIN32)
     enable_language(ASM_MASM)
 else()
     enable_language(ASM)
+
+    # Ensure that awk is present
+    find_program(AWK awk)
+    if (AWK STREQUAL "AWK-NOTFOUND")
+        message(FATAL_ERROR "AWK not found")
+    endif()
 endif(WIN32)
 
 # Build a list of compiler definitions by putting -D in front of each define.
@@ -103,6 +109,24 @@ function(preprocess_def_file inputFilename outputFilename)
                               PROPERTIES GENERATED TRUE)
 endfunction()
 
+function(generate_exports_file inputFilename outputFilename)
+
+  if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
+    set(AWK_SCRIPT generateexportedsymbols.awk)
+  else()
+    set(AWK_SCRIPT generateversionscript.awk)
+  endif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
+
+  add_custom_command(
+    OUTPUT ${outputFilename}
+    COMMAND ${AWK} -f ${CMAKE_SOURCE_DIR}/${AWK_SCRIPT} ${inputFilename} >${outputFilename}
+    DEPENDS ${inputFilename}
+    COMMENT "Generating exports file ${outputFilename}"
+  )
+  set_source_files_properties(${outputFilename}
+                              PROPERTIES GENERATED TRUE)
+endfunction()
+
 function(add_precompiled_header header cppFile targetSources)
   if(MSVC)
     set(precompiledBinary "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/stdafx.pch")
diff --git a/generateexportedsymbols.awk b/generateexportedsymbols.awk
new file mode 100644 (file)
index 0000000..19312c3
--- /dev/null
@@ -0,0 +1,6 @@
+{ 
+       # Remove the CR character in case the sources are mapped from
+       # a Windows share and contain CRLF line endings
+       gsub(/\r/,"", $0);
+       print "_"  $0;
+} 
diff --git a/generateversionscript.awk b/generateversionscript.awk
new file mode 100644 (file)
index 0000000..2aa1f4f
--- /dev/null
@@ -0,0 +1,14 @@
+BEGIN {
+       print "V1.0 {";
+       print "    global:";
+} 
+{ 
+       # Remove the CR character in case the sources are mapped from
+       # a Windows share and contain CRLF line endings
+       gsub(/\r/,"", $0);
+       print "        "  $0 ";";
+} 
+END {
+       print "    local: *;"
+       print "};";
+}
index 346ec0d..5959c0a 100644 (file)
@@ -14,12 +14,16 @@ list(APPEND CLR_SOURCES
 set (DEF_SOURCES
   mscorwks_ntdef.src
 )
-convert_to_absolute_path(DEF_SOURCES ${DEF_SOURCES})
 else()
 list(APPEND CLR_SOURCES
     unixinterface.cpp
 )
+
+set (DEF_SOURCES
+  mscorwks_unixexports.src
+)
 endif(WIN32)
 
+convert_to_absolute_path(DEF_SOURCES ${DEF_SOURCES})
 convert_to_absolute_path(CLR_SOURCES ${CLR_SOURCES})
 add_subdirectory(coreclr)
index 0e6184c..9337fc5 100644 (file)
@@ -12,6 +12,9 @@ if (WIN32)
 else()
     add_definitions(-DNO_CRT_INIT)
 
+    set(EXPORTS_FILE ${CMAKE_CURRENT_BINARY_DIR}/coreclr.exports)
+    generate_exports_file(${DEF_SOURCES} ${EXPORTS_FILE})
+
     if(CMAKE_SYSTEM_NAME STREQUAL Linux OR CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
         # This option is necessary to ensure that the overloaded delete operator defined inside
         # of the utilcode will be used instead of the standard library delete operator.
@@ -25,12 +28,16 @@ else()
         # These options are used to force every object to be included even if it's unused.
         set(START_WHOLE_ARCHIVE -Wl,--whole-archive)
         set(END_WHOLE_ARCHIVE -Wl,--no-whole-archive) 
+
+        set(EXPORTS_LINKER_OPTION -Wl,--version-script=${EXPORTS_FILE})
     endif(CMAKE_SYSTEM_NAME STREQUAL Linux OR CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
 
     if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
         # These options are used to force every object to be included even if it's unused.
         set(START_WHOLE_ARCHIVE -force_load)
         set(END_WHOLE_ARCHIVE )
+
+        set(EXPORTS_LINKER_OPTION -Wl,-exported_symbols_list,${EXPORTS_FILE})
     endif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
 
 endif (WIN32)
@@ -42,6 +49,12 @@ add_library(coreclr
     ${CLR_SOURCES}
 )
 
+add_custom_target(coreclr_exports DEPENDS ${EXPORTS_FILE})
+add_dependencies(coreclr coreclr_exports)
+
+set_property(TARGET coreclr APPEND_STRING PROPERTY LINK_FLAGS ${EXPORTS_LINKER_OPTION})
+set_property(TARGET coreclr APPEND_STRING PROPERTY LINK_DEPENDS ${EXPORTS_FILE})
+
 if (CLR_CMAKE_PLATFORM_UNIX)
     set(LIB_UNWINDER unwinder_wks)
 endif (CLR_CMAKE_PLATFORM_UNIX)
diff --git a/src/dlls/mscoree/mscorwks_unixexports.src b/src/dlls/mscoree/mscorwks_unixexports.src
new file mode 100644 (file)
index 0000000..00eb072
--- /dev/null
@@ -0,0 +1,79 @@
+CloseHandle
+CoCreateGuid
+CopyFileW
+CoreDllMain
+CoTaskMemAlloc
+CoTaskMemFree
+CreateDirectoryW
+CreateEventW
+CreateFileW
+CreateMutexW
+CreateSemaphoreW
+DeleteFileW
+DllMain
+DuplicateHandle
+ExecuteAssembly
+FindClose
+FindFirstFileW
+FindNextFileW
+FlushFileBuffers
+ForkAndExecProcess
+FormatMessageW
+FreeEnvironmentStringsW
+GetACP
+GetConsoleCP
+GetConsoleOutputCP
+GetCurrentDirectoryW
+GetCurrentProcess
+GetCurrentProcessId
+GetEnvironmentStringsW
+GetEnvironmentVariableW
+GetFileAttributesExW
+GetFileInformationFromFd
+GetFileInformationFromPath
+GetFileSize
+GetFileType
+GetFullPathNameW
+GetLongPathNameW
+GetProcAddress
+GetStdHandle
+GetSystemInfo
+GetTempFileNameW
+GetTempPathW
+LocalAlloc
+LocalFree
+LockFile
+lstrlenA
+lstrlenW
+MapViewOfFile
+MoveFileExW
+MultiByteToWideChar
+OpenEventW
+OpenMutexW
+OpenSemaphoreW
+PAL_Random
+QueryPerformanceCounter  
+QueryPerformanceFrequency
+RaiseException
+ReadFile
+ReleaseMutex
+ReleaseSemaphore
+RemoveDirectoryW
+ResetEvent
+RtlZeroMemory
+SetConsoleCtrlHandler
+SetCurrentDirectoryW
+SetEndOfFile
+SetEnvironmentVariableW
+SetErrorMode
+SetEvent
+SetFileAttributesW
+SetFilePointer
+SetFileTime
+UnlockFile
+UnmapViewOfFile
+VirtualAlloc
+VirtualFree
+VirtualQuery
+WideCharToMultiByte
+WriteFile
index f307815..47d87ae 100644 (file)
@@ -12,11 +12,6 @@ else()
     # given Windows .rc file. The target C++ file path is returned in the
     # variable specified by the TARGET_FILE parameter.
     function(build_resources SOURCE TARGET_NAME TARGET_FILE)
-        # Ensure that the necessary tools are present
-        find_program(AWK awk)
-        if (AWK STREQUAL "AWK-NOTFOUND")
-            message(FATAL_ERROR "AWK not found")
-        endif()
 
         get_compile_definitions(PREPROCESS_DEFINITIONS)
         get_include_directories(INCLUDE_DIRECTORIES)