From a9d4f377c2db4f9775a8ee57ead9764af247ecbb Mon Sep 17 00:00:00 2001 From: Anton Lapounov Date: Fri, 19 Mar 2021 02:56:16 -0700 Subject: [PATCH] Use latest DiaSymReader and support ARM64 host in crossgen2 (#49841) --- .../tools/aot/ILCompiler.Diagnostics/PdbWriter.cs | 50 +++++++++++----------- src/coreclr/tools/aot/crossgen2/crossgen2.csproj | 44 ++++++++++++++++--- 2 files changed, 64 insertions(+), 30 deletions(-) diff --git a/src/coreclr/tools/aot/ILCompiler.Diagnostics/PdbWriter.cs b/src/coreclr/tools/aot/ILCompiler.Diagnostics/PdbWriter.cs index 620fb78..1483cfb 100644 --- a/src/coreclr/tools/aot/ILCompiler.Diagnostics/PdbWriter.cs +++ b/src/coreclr/tools/aot/ILCompiler.Diagnostics/PdbWriter.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Reflection; using System.Reflection.PortableExecutable; using System.Runtime.InteropServices; using System.Text; @@ -73,6 +74,8 @@ namespace ILCompiler.Diagnostics public class PdbWriter { + private const string DiaSymReaderLibrary = "Microsoft.DiaSymReader.Native"; + string _pdbPath; PDBExtraData _pdbExtraData; @@ -86,34 +89,33 @@ namespace ILCompiler.Diagnostics UIntPtr _pdbMod; ISymNGenWriter2 _ngenWriter; - private const string DiaSymReaderModuleName32 = "Microsoft.DiaSymReader.Native.x86.dll"; - private const string DiaSymReaderModuleName64 = "Microsoft.DiaSymReader.Native.amd64.dll"; - - private const string CreateNGenPdbWriterFactoryName = "CreateNGenPdbWriter"; - - [DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory | DllImportSearchPath.SafeDirectories)] - [DllImport(DiaSymReaderModuleName32, EntryPoint = CreateNGenPdbWriterFactoryName, PreserveSig = false)] - private extern static void CreateNGenPdbWriter32([MarshalAs(UnmanagedType.LPWStr)] string ngenImagePath, [MarshalAs(UnmanagedType.LPWStr)] string pdbPath, [MarshalAs(UnmanagedType.IUnknown)] out object ngenPdbWriter); - - [DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory | DllImportSearchPath.SafeDirectories)] - [DllImport(DiaSymReaderModuleName64, EntryPoint = CreateNGenPdbWriterFactoryName, PreserveSig = false)] - private extern static void CreateNGenPdbWriter64([MarshalAs(UnmanagedType.LPWStr)] string ngenImagePath, [MarshalAs(UnmanagedType.LPWStr)] string pdbPath, [MarshalAs(UnmanagedType.IUnknown)] out object ngenPdbWriter); - - private static ISymNGenWriter2 CreateNGenWriter(string ngenImagePath, string pdbPath) + static PdbWriter() { - object instance; + NativeLibrary.SetDllImportResolver(typeof(PdbWriter).Assembly, DllImportResolver); + } - if (IntPtr.Size == 4) - { - CreateNGenPdbWriter32(ngenImagePath, pdbPath, out instance); - } - else + private static IntPtr DllImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath) + { + IntPtr libraryHandle = IntPtr.Zero; + if (libraryName == DiaSymReaderLibrary) { - CreateNGenPdbWriter64(ngenImagePath, pdbPath, out instance); + string archSuffix = RuntimeInformation.ProcessArchitecture.ToString().ToLowerInvariant(); + if (archSuffix == "x64") + { + archSuffix = "amd64"; + } + libraryHandle = NativeLibrary.Load(DiaSymReaderLibrary + "." + archSuffix + ".dll", assembly, searchPath); } - return (ISymNGenWriter2)instance; + return libraryHandle; } + [DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory | DllImportSearchPath.SafeDirectories)] + [DllImport(DiaSymReaderLibrary, PreserveSig = false)] + private extern static void CreateNGenPdbWriter( + [MarshalAs(UnmanagedType.LPWStr)] string ngenImagePath, + [MarshalAs(UnmanagedType.LPWStr)] string pdbPath, + [MarshalAs(UnmanagedType.Interface)] out ISymNGenWriter2 ngenPdbWriter); + public PdbWriter(string pdbPath, PDBExtraData pdbExtraData) { SymDocument unknownDocument = new SymDocument(); @@ -209,10 +211,10 @@ namespace ILCompiler.Diagnostics _pdbFilePath = Path.Combine(_pdbPath, dllNameWithoutExtension + ".ni.pdb"); } - // Delete any preexisting PDB file upfront otherwise CreateNGenWriter silently opens it + // Delete any preexisting PDB file upfront, otherwise CreateNGenPdbWriter silently opens it File.Delete(_pdbFilePath); - _ngenWriter = CreateNGenWriter(dllPath, _pdbFilePath); + CreateNGenPdbWriter(dllPath, _pdbFilePath, out _ngenWriter); { // PDB file is now created. Get its path and update _pdbFilePath so the PDB file diff --git a/src/coreclr/tools/aot/crossgen2/crossgen2.csproj b/src/coreclr/tools/aot/crossgen2/crossgen2.csproj index 87e123a..e30e6d1 100644 --- a/src/coreclr/tools/aot/crossgen2/crossgen2.csproj +++ b/src/coreclr/tools/aot/crossgen2/crossgen2.csproj @@ -72,13 +72,13 @@ CopyToOutputDirectory="PreserveNewest" CopyToPublishDirectory="PreserveNewest" Link="%(FileName)%(Extension)" - /> + /> + /> + $(CrossHostArch) + amd64 + Microsoft.DiaSymReader.Native.$(DiaSymReaderCrossArch).dll + $(PkgMicrosoft_DiaSymReader_Native)\runtimes\win\native\$(DiaSymReaderCrossArchFileName) + + + $(TargetArchitecture) + amd64 + Microsoft.DiaSymReader.Native.$(DiaSymReaderTargetArch).dll + $(PkgMicrosoft_DiaSymReader_Native)\runtimes\win\native\$(DiaSymReaderTargetArchFileName) + + + + + + $(RuntimeBinDir)$(CrossHostArch)\crossgen2 - + + + + + + -- 2.7.4