From 3c126e6e42b0f9c879cae93d94a1deb7e7680f66 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Wed, 23 Mar 2022 09:05:54 -0700 Subject: [PATCH] Change CoreCLR diagnostic resource production to remove cross-component build dependency (#67018) --- src/coreclr/build-runtime.cmd | 6 +- src/coreclr/crosscomponents.cmake | 4 - src/coreclr/debug/runtimeinfo/CMakeLists.txt | 2 +- src/coreclr/dlls/CMakeLists.txt | 4 +- .../dlls/mscoree/coreclr/CMakeLists.txt | 46 ++-- .../mscoree/coreclr/GenClrDebugResource.ps1 | 47 ++++ src/coreclr/dlls/mscoree/coreclr/README.md | 5 +- .../mscoree/coreclr/dump_helper_resource.bin | Bin 17 -> 0 bytes .../coreclr/dump_helper_resource.rc.in | 13 ++ src/coreclr/runtime-prereqs.proj | 5 +- src/coreclr/tools/CMakeLists.txt | 6 - .../tools/GenClrDebugResource/CMakeLists.txt | 9 - .../GenClrDebugResource.cpp | 218 ------------------ .../tools/GenClrDebugResource/native.rc | 6 - .../tools/InjectResource/CMakeLists.txt | 12 - .../tools/InjectResource/InjectResource.cpp | 157 ------------- .../InjectResource/InjectResource.csproj | 16 ++ src/coreclr/tools/InjectResource/Program.cs | 30 +++ .../tools/InjectResource/ResourceUpdater.cs | 69 ++++++ src/coreclr/tools/InjectResource/native.rc | 6 - src/native/corehost/build.cmd | 20 -- src/native/corehost/build.sh | 12 +- src/native/corehost/corehost.proj | 37 ++- 23 files changed, 246 insertions(+), 484 deletions(-) create mode 100644 src/coreclr/dlls/mscoree/coreclr/GenClrDebugResource.ps1 delete mode 100644 src/coreclr/dlls/mscoree/coreclr/dump_helper_resource.bin create mode 100644 src/coreclr/dlls/mscoree/coreclr/dump_helper_resource.rc.in delete mode 100644 src/coreclr/tools/GenClrDebugResource/CMakeLists.txt delete mode 100644 src/coreclr/tools/GenClrDebugResource/GenClrDebugResource.cpp delete mode 100644 src/coreclr/tools/GenClrDebugResource/native.rc delete mode 100644 src/coreclr/tools/InjectResource/CMakeLists.txt delete mode 100644 src/coreclr/tools/InjectResource/InjectResource.cpp create mode 100644 src/coreclr/tools/InjectResource/InjectResource.csproj create mode 100644 src/coreclr/tools/InjectResource/Program.cs create mode 100644 src/coreclr/tools/InjectResource/ResourceUpdater.cs delete mode 100644 src/coreclr/tools/InjectResource/native.rc diff --git a/src/coreclr/build-runtime.cmd b/src/coreclr/build-runtime.cmd index 50bcbb06d36..9e259e33340 100644 --- a/src/coreclr/build-runtime.cmd +++ b/src/coreclr/build-runtime.cmd @@ -528,13 +528,9 @@ if %__BuildNative% EQU 1 ( if /i "%__BuildArch%" == "x86" ( set __VCBuildArch=x86 ) if /i "%__BuildArch%" == "arm" ( set __VCBuildArch=x86_arm - set ___CrossBuildDefine="-DCLR_CMAKE_CROSS_ARCH=1" "-DCLR_CMAKE_CROSS_HOST_ARCH=%__CrossArch%" ) if /i "%__BuildArch%" == "arm64" ( set __VCBuildArch=x86_arm64 - if defined __CrossArch ( - set ___CrossBuildDefine="-DCLR_CMAKE_CROSS_ARCH=1" "-DCLR_CMAKE_CROSS_HOST_ARCH=%__CrossArch%" - ) ) echo %__MsgPrefix%Using environment: "%__VCToolsRoot%\vcvarsall.bat" !__VCBuildArch! @@ -549,7 +545,7 @@ if %__BuildNative% EQU 1 ( set __ExtraCmakeArgs="-DCMAKE_BUILD_TYPE=!__BuildType!" ) - set __ExtraCmakeArgs=!__ExtraCmakeArgs! !___CrossBuildDefine! "-DCLR_CMAKE_PGO_INSTRUMENT=%__PgoInstrument%" "-DCLR_CMAKE_OPTDATA_PATH=%__PgoOptDataPath%" "-DCLR_CMAKE_PGO_OPTIMIZE=%__PgoOptimize%" %__CMakeArgs% + set __ExtraCmakeArgs=!__ExtraCmakeArgs! "-DCLR_CMAKE_PGO_INSTRUMENT=%__PgoInstrument%" "-DCLR_CMAKE_OPTDATA_PATH=%__PgoOptDataPath%" "-DCLR_CMAKE_PGO_OPTIMIZE=%__PgoOptimize%" %__CMakeArgs% call "%__RepoRootDir%\eng\native\gen-buildsys.cmd" "%__ProjectDir%" "%__IntermediatesDir%" %__VSVersion% %__BuildArch% !__ExtraCmakeArgs! if not !errorlevel! == 0 ( echo %__ErrMsgPrefix%%__MsgPrefix%Error: failed to generate native component build project! diff --git a/src/coreclr/crosscomponents.cmake b/src/coreclr/crosscomponents.cmake index ceb5b5384e5..40784e58fa0 100644 --- a/src/coreclr/crosscomponents.cmake +++ b/src/coreclr/crosscomponents.cmake @@ -42,7 +42,3 @@ if(NOT CLR_CMAKE_HOST_LINUX AND NOT CLR_CMAKE_HOST_OSX AND NOT FEATURE_CROSSBITN COMPONENT crosscomponents ) endif() - -if (CLR_CMAKE_TARGET_WIN32 AND NOT CLR_CMAKE_CROSS_ARCH) - add_dependencies(crosscomponents InjectResource GenClrDebugResource) -endif() diff --git a/src/coreclr/debug/runtimeinfo/CMakeLists.txt b/src/coreclr/debug/runtimeinfo/CMakeLists.txt index ea59ecea8a3..d2dd921fa95 100644 --- a/src/coreclr/debug/runtimeinfo/CMakeLists.txt +++ b/src/coreclr/debug/runtimeinfo/CMakeLists.txt @@ -29,8 +29,8 @@ function(generate_module_index Target ModuleIndexFile) target_sources(runtimeinfo PRIVATE ${ModuleIndexFile}) endfunction(generate_module_index) -generate_module_index(coreclr ${CMAKE_CURRENT_BINARY_DIR}/runtimemoduleindex.h) if(NOT DEFINED CLR_CROSS_COMPONENTS_BUILD) + generate_module_index(coreclr ${CMAKE_CURRENT_BINARY_DIR}/runtimemoduleindex.h) add_dependencies(runtimeinfo inject_debug_resources_coreclr) generate_module_index(mscordaccore ${CMAKE_CURRENT_BINARY_DIR}/dacmoduleindex.h) generate_module_index(mscordbi ${CMAKE_CURRENT_BINARY_DIR}/dbimoduleindex.h) diff --git a/src/coreclr/dlls/CMakeLists.txt b/src/coreclr/dlls/CMakeLists.txt index 6ef420d165f..fd98b3d4718 100644 --- a/src/coreclr/dlls/CMakeLists.txt +++ b/src/coreclr/dlls/CMakeLists.txt @@ -2,10 +2,10 @@ if(CLR_CMAKE_TARGET_WIN32) add_subdirectory(clretwrc) endif(CLR_CMAKE_TARGET_WIN32) add_subdirectory(dbgshim) -if (NOT (CLR_CMAKE_TARGET_WIN32 AND (CLR_CMAKE_TARGET_ARCH_I386 OR CLR_CMAKE_TARGET_ARCH_ARM) AND CLR_CMAKE_HOST_ARCH_AMD64)) +if (NOT (CLR_CMAKE_TARGET_WIN32 AND FEATURE_CROSSBITNESS)) add_subdirectory(mscordbi) add_subdirectory(mscordac) + add_subdirectory(mscoree) endif() -add_subdirectory(mscoree) add_subdirectory(mscorpe) add_subdirectory(mscorrc) diff --git a/src/coreclr/dlls/mscoree/coreclr/CMakeLists.txt b/src/coreclr/dlls/mscoree/coreclr/CMakeLists.txt index 31c5b0151ff..a3837f80492 100644 --- a/src/coreclr/dlls/mscoree/coreclr/CMakeLists.txt +++ b/src/coreclr/dlls/mscoree/coreclr/CMakeLists.txt @@ -173,6 +173,26 @@ target_link_libraries(coreclr_static PUBLIC ${CORECLR_LIBRARIES} clrjit_static c target_compile_definitions(coreclr_static PUBLIC CORECLR_EMBEDDED) if(CLR_CMAKE_TARGET_WIN32) + set(CLRDEBUGINFO_RESOURCE_PATH ${CMAKE_CURRENT_BINARY_DIR}/clr_debug_resource.bin) + + add_custom_target( + clr_debug_resources + DEPENDS mscordaccore mscordbi + # make CLRDEBUGINFO resource + COMMAND powershell -NoProfile -ExecutionPolicy ByPass -File "${CMAKE_CURRENT_SOURCE_DIR}/GenClrDebugResource.ps1" -dac $ -dbi $ -out ${CLRDEBUGINFO_RESOURCE_PATH} + ) + + configure_file(dump_helper_resource.rc.in ${CMAKE_CURRENT_BINARY_DIR}/dump_helper_resource.rc) + + set(EMBEDDED_MINIDUMP_AUXILIARY_PROVIDER ON) + configure_file(dump_helper_resource.rc.in ${CMAKE_CURRENT_BINARY_DIR}/dump_helper_resource_embedded_minidump_provider.rc) + + target_sources(coreclr PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/dump_helper_resource.rc) + target_sources(coreclr_static PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/dump_helper_resource_embedded_minidump_provider.rc) + + add_dependencies(coreclr clr_debug_resources) + add_dependencies(coreclr_static clr_debug_resources) + # Add dac table & debug resource to coreclr get_include_directories(INC_DIR) get_compile_definitions(PREPROCESS_DEFINITIONS) @@ -191,10 +211,9 @@ if(CLR_CMAKE_TARGET_WIN32) clr_unknown_arch() endif() - - if (CLR_CMAKE_CROSS_ARCH) - include(${CMAKE_INSTALL_PREFIX}/${CLR_CMAKE_CROSS_HOST_ARCH}/dactabletools/dactabletools.cmake) - endif() + # dacess.h defines this constant + # The value is a constract between the DAC and the runtime + set (DACCESS_TABLE_RESOURCE COREXTERNALDATAACCESSRESOURCE) add_custom_command( DEPENDS coreclr mscordaccore mscordbi ${CLR_DIR}/debug/daccess/daccess.cpp @@ -203,17 +222,10 @@ if(CLR_CMAKE_TARGET_WIN32) # make and inject dactable for coreclr COMMAND cmd /c ${CLR_REPO_ROOT_DIR}/dotnet.cmd exec ${CMAKE_INSTALL_PREFIX}/DacTableGen/DacTableGen.dll /dac:${CMAKE_CURRENT_BINARY_DIR}/daccess.i /pdb:$ /dll:$ /bin:${CMAKE_CURRENT_BINARY_DIR}/wks.bin - COMMAND InjectResource /bin:${CMAKE_CURRENT_BINARY_DIR}/wks.bin /dll:$ - - # make CLRDEBUGINFO resource and inject into coreclr - COMMAND GenClrDebugResource /dac:$ /dbi:$ /sku:onecoreclr /out:${CMAKE_CURRENT_BINARY_DIR}/clrDebugResource.bin - COMMAND InjectResource /bin:${CMAKE_CURRENT_BINARY_DIR}/clrDebugResource.bin /dll:$ /name:CLRDEBUGINFO - - # inject MINIDUMP_AUXILIARY_PROVIDER into coreclr - COMMAND InjectResource /bin:${CMAKE_CURRENT_SOURCE_DIR}/dump_helper_resource.bin /dll:$ /name:MINIDUMP_AUXILIARY_PROVIDER + COMMAND cmd /c ${CLR_REPO_ROOT_DIR}/dotnet.cmd exec ${CMAKE_INSTALL_PREFIX}/InjectResource/InjectResource.dll --bin ${CMAKE_CURRENT_BINARY_DIR}/wks.bin --image $ --name ${DACCESS_TABLE_RESOURCE} COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/inject_debug_resources_coreclr.timestamp - COMMENT Add dactable, debug resources, and dump helper resources to coreclr + COMMENT Add dactable to coreclr ) if(NOT DEFINED CLR_CROSS_COMPONENTS_BUILD) @@ -228,14 +240,10 @@ if(CLR_CMAKE_TARGET_WIN32) # make and inject dactable for singlefilehost COMMAND cmd /c ${CLR_REPO_ROOT_DIR}/dotnet.cmd exec ${CMAKE_INSTALL_PREFIX}/DacTableGen/DacTableGen.dll /dac:${CMAKE_CURRENT_BINARY_DIR}/daccess.i /pdb:$ /dll:$ /bin:${CMAKE_CURRENT_BINARY_DIR}/sfh.bin - COMMAND InjectResource /bin:${CMAKE_CURRENT_BINARY_DIR}/sfh.bin /dll:$ - - # make CLRDEBUGINFO resource and inject into singlefilehost - COMMAND GenClrDebugResource /dac:$ /dbi:$ /sku:onecoreclr /out:${CMAKE_CURRENT_BINARY_DIR}/clrDebugResource.bin - COMMAND InjectResource /bin:${CMAKE_CURRENT_BINARY_DIR}/clrDebugResource.bin /dll:$ /name:CLRDEBUGINFO + COMMAND cmd /c ${CLR_REPO_ROOT_DIR}/dotnet.cmd exec ${CMAKE_INSTALL_PREFIX}/InjectResource/InjectResource.dll --bin ${CMAKE_CURRENT_BINARY_DIR}/sfh.bin --image $ --name ${DACCESS_TABLE_RESOURCE} COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/inject_debug_resources_singlefilehost.timestamp - COMMENT Add dactable, debug resources, and dump helper resources to singlefilehost + COMMENT Add dactable to singlefilehost ) if(NOT DEFINED CLR_CROSS_COMPONENTS_BUILD) diff --git a/src/coreclr/dlls/mscoree/coreclr/GenClrDebugResource.ps1 b/src/coreclr/dlls/mscoree/coreclr/GenClrDebugResource.ps1 new file mode 100644 index 00000000000..b627c55ae17 --- /dev/null +++ b/src/coreclr/dlls/mscoree/coreclr/GenClrDebugResource.ps1 @@ -0,0 +1,47 @@ +param ( + [string] + $dac, + [string] + $dbi, + [string] + $out +) + +function Parse-Int { + param ( + [parameter(Mandatory = $true, ValueFromPipeline = $true)] + [string]$hexValue + ) + return [System.Int32]::Parse($hexValue, [System.Globalization.NumberStyles]::HexNumber) +} + +$clrDebugResource = [System.IO.BinaryWriter]::new([System.IO.File]::OpenWrite($out)) + +try { + # We're creating the resource with the following layout (represented as a C struct) + # struct CLR_DEBUG_RESOURCE + # { + # int version; + # GUID clrSkuGuid; + # int dacTimeStamp; + # int dacImageSize; + # int dbiTimeStamp; + # int dacImageSize; + # }; + + # Write the debug resource version + $clrDebugResource.Write(0); + # Write the GUID for CoreCLR (should match the CLR_ID_ONECORE_CLR GUID in clrinternal.idl) + $clrDebugResource.Write([System.Guid]::Parse("{0xb1ee760d,0x6c4a,0x4533,{0xba,0x41,0x6f,0x4f,0x66,0x1f,0xab,0xaf}}").ToByteArray()) + [int]$dacTimeStamp = dumpbin $dac /HEADERS | Select-String "([0-9A-Fa-f]+) time date stamp" | %{ $_.Matches.Groups[1].Value } | Parse-Int + [int]$dacImageSize = dumpbin $dac /HEADERS | Select-String "([0-9A-Fa-f]+) size of image" | %{ $_.Matches.Groups[1].Value } | Parse-Int + [int]$dbiTimeStamp = dumpbin $dbi /HEADERS | Select-String "([0-9A-Fa-f]+) time date stamp" | %{ $_.Matches.Groups[1].Value } | Parse-Int + [int]$dbiImageSize = dumpbin $dbi /HEADERS | Select-String "([0-9A-Fa-f]+) size of image" | %{ $_.Matches.Groups[1].Value } | Parse-Int + $clrDebugResource.Write($dacTimeStamp) + $clrDebugResource.Write($dacImageSize) + $clrDebugResource.Write($dbiTimeStamp) + $clrDebugResource.Write($dbiImageSize) +} +finally { + $clrDebugResource.Dispose() +} \ No newline at end of file diff --git a/src/coreclr/dlls/mscoree/coreclr/README.md b/src/coreclr/dlls/mscoree/coreclr/README.md index b4f3e6f8845..d878be7b781 100644 --- a/src/coreclr/dlls/mscoree/coreclr/README.md +++ b/src/coreclr/dlls/mscoree/coreclr/README.md @@ -1,7 +1,4 @@ -dump\_helper\_resource.bin in this folder is a text file with a single 0 byte appended on the end using a hex-editor. It is unlikely it will need to be modified frequently if at all, -but if that changes we can always create a little nicer tooling for it. - -dump\_helper\_resource.bin is used to populate the DUMP\_HELPER resource inside coreclr.dll on Windows. When an application crashes, Windows MinidumpWriteDump is planning to scan +dump\_helper\_resource.rc is used to populate the MINIDUMP\_AUXILIARY\_PROVIDER resource inside coreclr.dll on Windows. When an application crashes, Windows MinidumpWriteDump is planning to scan modules looking for this resource. The content of the resource is expected to be the name of a dll in the same folder, encoded in UTF8, null terminated, that implements the CLRDataCreateInterface function. For OS security purposes MinidumpWriteDump will do an authenticode signing check before loading the indicated binary, however if your build isn't signed you can get around this limitation by registering it at HKLM\Software\Microsoft\WindowsNT\CurrentVersion\MiniDumpAuxilliaryDlls. diff --git a/src/coreclr/dlls/mscoree/coreclr/dump_helper_resource.bin b/src/coreclr/dlls/mscoree/coreclr/dump_helper_resource.bin deleted file mode 100644 index aa2d9eae1f8c5fa5108c6854a1c09c90ad32ea02..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 17 Wcmc~TPR=h%NlXTksd_0nISc?j;Rav; diff --git a/src/coreclr/dlls/mscoree/coreclr/dump_helper_resource.rc.in b/src/coreclr/dlls/mscoree/coreclr/dump_helper_resource.rc.in new file mode 100644 index 00000000000..f555c1221d0 --- /dev/null +++ b/src/coreclr/dlls/mscoree/coreclr/dump_helper_resource.rc.in @@ -0,0 +1,13 @@ +#include + +LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL + +#cmakedefine EMBEDDED_MINIDUMP_AUXILIARY_PROVIDER + +#ifndef EMBEDDED_MINIDUMP_AUXILIARY_PROVIDER + +MINIDUMP_AUXILIARY_PROVIDER RCDATA { "mscordaccore.dll\0" } + +#endif + +CLRDEBUGINFO RCDATA @CLRDEBUGINFO_RESOURCE_PATH@ \ No newline at end of file diff --git a/src/coreclr/runtime-prereqs.proj b/src/coreclr/runtime-prereqs.proj index 167346101e0..4b368d16ca7 100644 --- a/src/coreclr/runtime-prereqs.proj +++ b/src/coreclr/runtime-prereqs.proj @@ -7,8 +7,9 @@ .NET Runtime - - + + + diff --git a/src/coreclr/tools/CMakeLists.txt b/src/coreclr/tools/CMakeLists.txt index db59797e26f..48e1466b197 100644 --- a/src/coreclr/tools/CMakeLists.txt +++ b/src/coreclr/tools/CMakeLists.txt @@ -1,12 +1,6 @@ add_subdirectory(SOS) add_subdirectory(superpmi) -if (CLR_CMAKE_TARGET_WIN32 AND NOT CLR_CMAKE_CROSS_ARCH) - add_subdirectory(GenClrDebugResource) - add_subdirectory(InjectResource) - - install(EXPORT dactabletools DESTINATION dactabletools COMPONENT crosscomponents) -endif() if (CLR_CMAKE_TARGET_ARCH_AMD64 OR CLR_CMAKE_TARGET_ARCH_ARM64) add_subdirectory(StressLogAnalyzer) endif() diff --git a/src/coreclr/tools/GenClrDebugResource/CMakeLists.txt b/src/coreclr/tools/GenClrDebugResource/CMakeLists.txt deleted file mode 100644 index 2ea56e1ea87..00000000000 --- a/src/coreclr/tools/GenClrDebugResource/CMakeLists.txt +++ /dev/null @@ -1,9 +0,0 @@ -set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded) -add_executable(GenClrDebugResource GenClrDebugResource.cpp) - -target_link_libraries(GenClrDebugResource - ${STATIC_MT_CRT_LIB} - ${STATIC_MT_VCRT_LIB} -) - -install(TARGETS GenClrDebugResource EXPORT dactabletools DESTINATION dactabletools COMPONENT crosscomponents) diff --git a/src/coreclr/tools/GenClrDebugResource/GenClrDebugResource.cpp b/src/coreclr/tools/GenClrDebugResource/GenClrDebugResource.cpp deleted file mode 100644 index ded0cee406d..00000000000 --- a/src/coreclr/tools/GenClrDebugResource/GenClrDebugResource.cpp +++ /dev/null @@ -1,218 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -/* This app writes out the data for a special resource which is embedded in clr.dll - * The resource serves two purposes, to differentiate a random dll named clr.dll from - * an official microsoft clr dll (it isn't foolproof but it should prevent anyone from - * accidentally appearing to be a clr) and to provide file information about DAC and DBI - * which correspond to this build of the CLR. - */ - - -#include -#include -#include -#include - -char* g_appName; - - -struct ClrDebugResource -{ - DWORD dwVersion; - GUID signature; - DWORD dwDacTimeStamp; - DWORD dwDacSizeOfImage; - DWORD dwDbiTimeStamp; - DWORD dwDbiSizeOfImage; -}; - -BOOL -GetBinFileData(_In_z_ char* binFileName, DWORD* pTimeStamp, DWORD* pSizeOfImage) -{ - HANDLE binFileHandle; - DWORD peHeaderOffset; - ULONG done; - - binFileHandle = CreateFileA(binFileName, GENERIC_READ, FILE_SHARE_READ, - NULL, OPEN_EXISTING, 0, NULL); - if (!binFileHandle || binFileHandle == INVALID_HANDLE_VALUE) - { - printf("%s: Unable to open '%s', %d\n", g_appName, - binFileName, GetLastError()); - goto error; - } - - // Read the 4 byte value at 0x3c, which is the offset to PE header - if (INVALID_SET_FILE_POINTER == SetFilePointer(binFileHandle, 0x3c, NULL, FILE_BEGIN)) - { - printf("%s: Unable to move file pointer '%s', %d\n", g_appName, - binFileName, GetLastError()); - goto error; - } - - if (!ReadFile(binFileHandle, &peHeaderOffset, 4, &done, NULL) || - done != 4) - { - printf("%s: Unable to read '%s', %d\n", g_appName, - binFileName, GetLastError()); - goto error; - } - - // Read the 4 byte value at 8 bytes after peHeader, that is the timestamp - if (INVALID_SET_FILE_POINTER == SetFilePointer(binFileHandle, peHeaderOffset + 8, NULL, FILE_BEGIN)) - { - printf("%s: Unable to move file pointer '%s', %d\n", g_appName, - binFileName, GetLastError()); - goto error; - } - - if (!ReadFile(binFileHandle, pTimeStamp, 4, &done, NULL) || - done != 4) - { - printf("%s: Unable to read '%s', %d\n", g_appName, - binFileName, GetLastError()); - goto error; - } - - // Read the 4 byte value at 80 bytes after peHeader, that is the sizeOfImage - if (INVALID_SET_FILE_POINTER == SetFilePointer(binFileHandle, peHeaderOffset + 80, NULL, FILE_BEGIN)) - { - printf("%s: Unable to move file pointer '%s', %d\n", g_appName, - binFileName, GetLastError()); - goto error; - } - - if (!ReadFile(binFileHandle, pSizeOfImage, 4, &done, NULL) || - done != 4) - { - printf("%s: Unable to read '%s', %d\n", g_appName, - binFileName, GetLastError()); - goto error; - } - - CloseHandle(binFileHandle); - return TRUE; - -error: - CloseHandle(binFileHandle); - return FALSE; -} - -void -Usage(void) -{ - printf("Usage: %s [options]\n", g_appName); - printf("Options are:\n"); - printf(" /out: - output binary file that contains the raw resource data\n"); - printf(" /dac: - path to mscordacwks that should be referenced\n"); - printf(" /dbi: - path to mscordbi that should be referenced\n"); - printf(" /sku: - Either clr, coreclr, or phoneclr indicating the CLR sku\n"); -} - -void __cdecl -main(int argc, _In_z_ char** argv) -{ - char* outFile = NULL; - char* dacFile = NULL; - char* dbiFile = NULL; - char* sku = NULL; - - g_appName = argv[0]; - - while (--argc) - { - argv++; - - if (!strncmp(*argv, "/out:", 5)) - { - outFile = *argv + 5; - } - else if (!strncmp(*argv, "/dac:", 5)) - { - dacFile = *argv + 5; - } - else if (!strncmp(*argv, "/dbi:", 5)) - { - dbiFile = *argv + 5; - } - else if (!strncmp(*argv, "/sku:", 5)) - { - sku = *argv + 5; - } - else - { - Usage(); - exit(1); - } - } - - if (!outFile || !dacFile || !dbiFile || !sku) - { - Usage(); - exit(1); - } - - ClrDebugResource res; - res.dwVersion = 0; - - if(strcmp(sku, "clr")==0) - { - res.signature = CLR_ID_V4_DESKTOP; - } - else if(strcmp(sku, "coreclr")==0) - { - res.signature = CLR_ID_CORECLR; - } - else if(strcmp(sku, "phoneclr")==0) - { - res.signature = CLR_ID_PHONE_CLR; - } - else if (strcmp(sku, "onecoreclr") == 0) - { - res.signature = CLR_ID_ONECORE_CLR; - } - else - { - printf("Error: Unrecognized sku option: %s\n", sku); - Usage(); - exit(1); - } - - printf("%s: Reading data from DAC: %s\n", g_appName, dacFile); - if(!GetBinFileData(dacFile, &(res.dwDacTimeStamp), &(res.dwDacSizeOfImage))) - exit(1); - printf("%s: DAC timeStamp = 0x%x sizeOfImage = 0x%x\n", g_appName, res.dwDacTimeStamp, res.dwDacSizeOfImage); - - printf("%s: Reading data from DBI: %s\n", g_appName, dbiFile); - if(!GetBinFileData(dbiFile, &(res.dwDbiTimeStamp), &(res.dwDbiSizeOfImage))) - exit(1); - printf("%s: DBI timeStamp = 0x%x sizeOfImage = 0x%x\n", g_appName, res.dwDbiTimeStamp, res.dwDbiSizeOfImage); - - printf("%s: Writing binary resource file: %s\n", g_appName, outFile); - HANDLE outFileHandle = CreateFileA(outFile, GENERIC_WRITE, 0, - NULL, CREATE_ALWAYS, 0, NULL); - if (!outFileHandle || outFileHandle == INVALID_HANDLE_VALUE) - { - printf("%s: Unable to create '%s', %d\n", - g_appName, outFile, GetLastError()); - goto error; - } - - - DWORD done = 0; - if(!WriteFile(outFileHandle, &res, sizeof(res), &done, NULL) || done != sizeof(res)) - { - printf("%s: Unable to write file data '%s', %d\n", - g_appName, outFile, GetLastError()); - goto error; - } - - CloseHandle(outFileHandle); - printf("%s: Success. Returning 0\n", g_appName); - exit(0); - -error: - CloseHandle(outFileHandle); - exit(1); -} diff --git a/src/coreclr/tools/GenClrDebugResource/native.rc b/src/coreclr/tools/GenClrDebugResource/native.rc deleted file mode 100644 index 3c494dcd862..00000000000 --- a/src/coreclr/tools/GenClrDebugResource/native.rc +++ /dev/null @@ -1,6 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -#define FX_VER_FILEDESCRIPTION_STR "Microsoft\0" - -#include -#include diff --git a/src/coreclr/tools/InjectResource/CMakeLists.txt b/src/coreclr/tools/InjectResource/CMakeLists.txt deleted file mode 100644 index 51f2b19349b..00000000000 --- a/src/coreclr/tools/InjectResource/CMakeLists.txt +++ /dev/null @@ -1,12 +0,0 @@ -remove_definitions(-DUNICODE) -remove_definitions(-D_UNICODE) -set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded) - -add_executable(InjectResource InjectResource.cpp) - -target_link_libraries(InjectResource - ${STATIC_MT_CRT_LIB} - ${STATIC_MT_VCRT_LIB} -) - -install(TARGETS InjectResource EXPORT dactabletools DESTINATION dactabletools COMPONENT crosscomponents) diff --git a/src/coreclr/tools/InjectResource/InjectResource.cpp b/src/coreclr/tools/InjectResource/InjectResource.cpp deleted file mode 100644 index cf5c3917df9..00000000000 --- a/src/coreclr/tools/InjectResource/InjectResource.cpp +++ /dev/null @@ -1,157 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -#include -#include -#include -#include -#include - -char* g_appName; - -#define MAX(x,y) ((x) > (y) ? (x) : (y)) - -void -AddBinaryResourceToDll(_In_z_ char* dllName, - _In_z_ const char* resName, - PVOID resData, - ULONG resDataSize) -{ - // a retry loop in case we get transient file access issues - // does exponential backoff with retries after 1,2,4,8,16, and 32 seconds - for(int seconds = 0; seconds < 60; seconds = MAX(seconds*2,1)) - { - if(seconds != 0) - Sleep(seconds * 1000); - - HANDLE dllUpdate = BeginUpdateResourceA(dllName, FALSE); - if (!dllUpdate) - { - printf("Unable to open '%s' for update, error=%d\n", - dllName, GetLastError()); - continue; - } - - if (!UpdateResourceA(dllUpdate, - (LPCSTR)RT_RCDATA, - resName, - MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), - resData, - resDataSize)) - { - printf("Unable to update '%s', error=%d\n", - dllName, GetLastError()); - continue; - } - - if(!EndUpdateResource(dllUpdate, FALSE)) - { - printf("Unable to write updates to '%s', error=%d\n", - dllName, GetLastError()); - continue; - } - - return; - } - - printf("Stopping after excessive failures\n"); - exit(1); -} - -void -GetBinFileData(_In_z_ char* binFileName, PVOID* binData, PULONG binDataSize) -{ - HANDLE binFileHandle; - PVOID data; - ULONG size, done; - - binFileHandle = CreateFileA(binFileName, GENERIC_READ, FILE_SHARE_READ, - NULL, OPEN_EXISTING, 0, NULL); - if (!binFileHandle || binFileHandle == INVALID_HANDLE_VALUE) - { - printf("Unable to open '%s', %d\n", - binFileName, GetLastError()); - exit(1); - } - - size = GetFileSize(binFileHandle, NULL); - data = malloc(size); - if (!data) - { - printf("Out of memory\n"); - exit(1); - } - - if (!ReadFile(binFileHandle, data, size, &done, NULL) || - done != size) - { - printf("Unable to read '%s', %d\n", - binFileName, GetLastError()); - exit(1); - } - - CloseHandle(binFileHandle); - - *binData = data; - *binDataSize = size; -} - -void -Usage(void) -{ - printf("Usage: %s [options]\n", g_appName); - printf("Options are:\n"); - printf(" /bin: - Binary data to attach to DLL\n"); - printf(" /dll: - DLL to modify\n"); - printf(" /name: - resource name [Optional]\n"); - exit(1); -} - -void __cdecl -main(int argc, _In_z_ char** argv) -{ - char* binFile = NULL; - char* dllFile = NULL; - char* resName = NULL; - - g_appName = argv[0]; - - while (--argc) - { - argv++; - - if (!strncmp(*argv, "/bin:", 5)) - { - binFile = *argv + 5; - } - else if (!strncmp(*argv, "/dll:", 5)) - { - dllFile = *argv + 5; - } - else if (!strncmp(*argv, "/name:", 6)) - { - resName = *argv + 6; - } - else - { - Usage(); - } - } - - if (!binFile || !dllFile) - { - Usage(); - } - - PVOID resData; - ULONG resDataSize; - - GetBinFileData(binFile, &resData, &resDataSize); - - AddBinaryResourceToDll(dllFile, resName?resName:DACCESS_TABLE_RESOURCE, - resData, resDataSize); - - free(resData); - - printf("Updated %s\n", dllFile); -} diff --git a/src/coreclr/tools/InjectResource/InjectResource.csproj b/src/coreclr/tools/InjectResource/InjectResource.csproj new file mode 100644 index 00000000000..a62f3059dd6 --- /dev/null +++ b/src/coreclr/tools/InjectResource/InjectResource.csproj @@ -0,0 +1,16 @@ + + + + Exe + $(NetCoreAppToolCurrent) + enable + AnyCPU + AnyCPU + false + $(RuntimeBinDir)\InjectResource + + + + + + diff --git a/src/coreclr/tools/InjectResource/Program.cs b/src/coreclr/tools/InjectResource/Program.cs new file mode 100644 index 00000000000..55d221b6eb5 --- /dev/null +++ b/src/coreclr/tools/InjectResource/Program.cs @@ -0,0 +1,30 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +using System.CommandLine; +using System.IO; + +var binOption = new Option( + name: "--bin", + description: "Binary data to attach to the image"); +var imageOption = new Option( + name: "--image", + description: "PE image to add the binary resource into"); +var nameOption = new Option( + name: "--name", + description: "Resource name"); +var rootCommand = new RootCommand("Inject native resources into a Portable Executable image"); +rootCommand.AddOption(binOption); +rootCommand.AddOption(imageOption); +rootCommand.AddOption(nameOption); + +rootCommand.SetHandler( + (FileInfo binaryData, FileInfo peImage, string name) => + { + using ResourceUpdater updater = new(peImage); + updater.AddBinaryResource(name, File.ReadAllBytes(binaryData.FullName)); + }, + binOption, + imageOption, + nameOption); + +return rootCommand.Invoke(args); \ No newline at end of file diff --git a/src/coreclr/tools/InjectResource/ResourceUpdater.cs b/src/coreclr/tools/InjectResource/ResourceUpdater.cs new file mode 100644 index 00000000000..dcb0ee92f37 --- /dev/null +++ b/src/coreclr/tools/InjectResource/ResourceUpdater.cs @@ -0,0 +1,69 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +using System; +using System.ComponentModel; +using System.IO; +using System.Runtime.InteropServices; + +class ResourceUpdater : IDisposable +{ + private class UpdateResourceHandle : SafeHandle + { + public UpdateResourceHandle() + :base(IntPtr.Zero, true) + { + + } + + protected override bool ReleaseHandle() + { + return EndUpdateResource(handle, false); + } + + public override bool IsInvalid => handle == IntPtr.Zero; + } + + [DllImport("kernel32", EntryPoint = "BeginUpdateResourceA", CharSet = CharSet.Ansi, SetLastError = true)] + private static extern UpdateResourceHandle BeginUpdateResource(string pFileName, bool bDeleteExistingResources); + + [DllImport("kernel32", EntryPoint = "UpdateResourceA", CharSet = CharSet.Ansi, SetLastError = true)] + private static extern bool UpdateResource( + UpdateResourceHandle hUpdate, + nint lpType, + string lpName, + ushort wLanguage, + byte[] lpData, + int cb); + + [DllImport("kernel32", EntryPoint = "EndUpdateResourceA", CharSet = CharSet.Ansi, SetLastError = true)] + private static extern bool EndUpdateResource(IntPtr hUpdate, bool fDiscard); + + private UpdateResourceHandle handle; + + public ResourceUpdater(FileInfo peFile) + { + handle = BeginUpdateResource(peFile.FullName, false); + if (handle.IsInvalid) + { + throw new Win32Exception(Marshal.GetLastPInvokeError(), peFile.FullName); + } + } + + private static readonly nint RT_RCDATA = 10; + + private const ushort LANG_NEUTRAL = 0; + + public void AddBinaryResource(string name, byte[] data) + { + bool success = UpdateResource(handle, RT_RCDATA, name, LANG_NEUTRAL, data, data.Length); + if (!success) + { + throw new Win32Exception(Marshal.GetLastPInvokeError(), name); + } + } + + public void Dispose() + { + handle.Dispose(); + } +} \ No newline at end of file diff --git a/src/coreclr/tools/InjectResource/native.rc b/src/coreclr/tools/InjectResource/native.rc deleted file mode 100644 index 3c494dcd862..00000000000 --- a/src/coreclr/tools/InjectResource/native.rc +++ /dev/null @@ -1,6 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -#define FX_VER_FILEDESCRIPTION_STR "Microsoft\0" - -#include -#include diff --git a/src/native/corehost/build.cmd b/src/native/corehost/build.cmd index 62e0c44678b..84dd22e7263 100644 --- a/src/native/corehost/build.cmd +++ b/src/native/corehost/build.cmd @@ -41,7 +41,6 @@ if /i [%1] == [commit] (set __CommitSha=%2&&shift&&shift&goto Arg_Loop) if /i [%1] == [configureonly] ( set __ConfigureOnly=1&&shift&goto Arg_Loop) if /i [%1] == [incremental-native-build] ( set __IncrementalNativeBuild=1&&shift&goto Arg_Loop) if /i [%1] == [rootDir] ( set __rootDir=%2&&shift&&shift&goto Arg_Loop) -if /i [%1] == [coreclrartifacts] (set __CoreClrArtifacts=%2&&shift&&shift&goto Arg_Loop) if /i [%1] == [msbuild] (set __Ninja=0) if /i [%1] == [runtimeflavor] (set __RuntimeFlavor=%2&&shift&&shift&goto Arg_Loop) if /i [%1] == [runtimeconfiguration] (set __RuntimeConfiguration=%2&&shift&&shift&goto Arg_Loop) @@ -129,25 +128,6 @@ IF ERRORLEVEL 1 ( goto :Failure ) -if "%__RuntimeFlavor%" NEQ "Mono" ( - echo Copying "%__CoreClrArtifacts%\corehost\singlefilehost.exe" "%__CMakeBinDir%/corehost/" - copy /B /Y "%__CoreClrArtifacts%\corehost\singlefilehost.exe" "%__CMakeBinDir%/corehost/" - - echo Copying "%__CoreClrArtifacts%\corehost\PDB\singlefilehost.pdb" "%__CMakeBinDir%/corehost/PDB/" - copy /B /Y "%__CoreClrArtifacts%\corehost\PDB\singlefilehost.pdb" "%__CMakeBinDir%/corehost/PDB/" - - echo Embedding "%__CoreClrArtifacts%\mscordaccore.dll" into "%__CMakeBinDir%\corehost\singlefilehost.exe" - if not exist "%__CoreClrArtifacts%\x64\dactabletools\InjectResource.exe" ( - "%__CoreClrArtifacts%\dactabletools\InjectResource.exe" /bin:"%__CoreClrArtifacts%\mscordaccore.dll" /dll:"%__CMakeBinDir%\corehost\singlefilehost.exe" /name:MINIDUMP_EMBEDDED_AUXILIARY_PROVIDER - ) else ( - "%__CoreClrArtifacts%\x64\dactabletools\InjectResource.exe" /bin:"%__CoreClrArtifacts%\mscordaccore.dll" /dll:"%__CMakeBinDir%\corehost\singlefilehost.exe" /name:MINIDUMP_EMBEDDED_AUXILIARY_PROVIDER - ) - - IF ERRORLEVEL 1 ( - goto :Failure - ) -) - echo Done building Native components :Exit diff --git a/src/native/corehost/build.sh b/src/native/corehost/build.sh index 8d2eb46fa91..221440b6685 100755 --- a/src/native/corehost/build.sh +++ b/src/native/corehost/build.sh @@ -57,11 +57,6 @@ handle_arguments() { __ShiftArgs=1 ;; - coreclrartifacts|-coreclrartifacts) - __CoreClrArtifacts="$2" - __ShiftArgs=1 - ;; - runtimeflavor|-runtimeflavor) __RuntimeFlavor="$2" __ShiftArgs=1 @@ -83,7 +78,7 @@ __DistroRidLower="$(echo $__DistroRid | tr '[:upper:]' '[:lower:]')" __BinDir="$__RootBinDir/bin/$__DistroRidLower.$__BuildType" __IntermediatesDir="$__RootBinDir/obj/$__DistroRidLower.$__BuildType" -export __BinDir __IntermediatesDir __CoreClrArtifacts __RuntimeFlavor +export __BinDir __IntermediatesDir __RuntimeFlavor __CMakeArgs="-DCLI_CMAKE_HOST_VER=\"$__host_ver\" -DCLI_CMAKE_COMMON_HOST_VER=\"$__apphost_ver\" -DCLI_CMAKE_HOST_FXR_VER=\"$__fxr_ver\" $__CMakeArgs" __CMakeArgs="-DCLI_CMAKE_HOST_POLICY_VER=\"$__policy_ver\" -DCLI_CMAKE_PKG_RID=\"$__DistroRid\" -DCLI_CMAKE_COMMIT_HASH=\"$__commit_hash\" $__CMakeArgs" @@ -103,8 +98,3 @@ check_prereqs # Build the installer native components. build_native "$__TargetOS" "$__BuildArch" "$__scriptpath" "$__IntermediatesDir" "install" "$__CMakeArgs" "installer component" - -if [[ "$__RuntimeFlavor" != "Mono" ]]; then - echo Copying "$__CoreClrArtifacts/corehost/." to "$__CMakeBinDir/corehost" - cp -a "$__CoreClrArtifacts/corehost/." "$__CMakeBinDir/corehost" -fi diff --git a/src/native/corehost/corehost.proj b/src/native/corehost/corehost.proj index 737c67e85ba..72f46d92406 100644 --- a/src/native/corehost/corehost.proj +++ b/src/native/corehost/corehost.proj @@ -11,6 +11,41 @@ $(ArtifactsObjDir)$(OutputRid).$(Configuration)\ + + + + + + + $(CoreCLRArtifactsPath)/corehost/singlefilehost$(ExeSuffix) + $(CoreCLRArtifactsPath)/corehost/PDB/singlefilehost$(SymbolsSuffix) + $(CoreCLRArtifactsPath)/mscordaccore$(LibSuffix) + $(DotNetHostBinDir)/singlefilehost$(ExeSuffix) + $(DotNetHostBinDir)/PDB/singlefilehost$(SymbolsSuffix) + + + + + + + + + + + @@ -30,7 +65,6 @@ $(BuildArgs) -cross $(BuildArgs) $(Compiler) $(BuildArgs) $(CMakeArgs) - $(BuildArgs) -coreclrartifacts $(CoreCLRArtifactsPath) $(BuildArgs) -ninja $(BuildArgs) -runtimeflavor $(RuntimeFlavor) $(BuildArgs) /p:OfficialBuildId="$(OfficialBuildId)" @@ -89,7 +123,6 @@ $(BuildArgs) portable $(BuildArgs) incremental-native-build $(BuildArgs) rootdir $(RepoRoot) - $(BuildArgs) coreclrartifacts $(CoreCLRArtifactsPath) $(BuildArgs) msbuild $(BuildArgs) runtimeflavor $(RuntimeFlavor) $(BuildArgs) runtimeconfiguration $(RuntimeConfiguration) -- GitLab