コード例 #1
0
        /// <inheritdoc />
        public PEImageBuildResult CreateImage(ModuleDefinition module)
        {
            var context = new PEImageBuildContext();

            PEImage       image        = null;
            ITokenMapping tokenMapping = null;

            try
            {
                // Create basic PE image skeleton.
                image = new PEImage
                {
                    MachineType        = module.MachineType,
                    PEKind             = module.PEKind,
                    Characteristics    = module.FileCharacteristics,
                    SubSystem          = module.SubSystem,
                    DllCharacteristics = module.DllCharacteristics,
                    Resources          = module.NativeResourceDirectory,
                    TimeDateStamp      = module.TimeDateStamp
                };

                // Construct new .NET directory.
                var symbolProvider = new NativeSymbolsProvider(image.ImageBase);
                var result         = DotNetDirectoryFactory.CreateDotNetDirectory(
                    module,
                    symbolProvider,
                    context.DiagnosticBag);
                image.DotNetDirectory = result.Directory;
                tokenMapping          = result.TokenMapping;

                // Copy any collected native symbols over to the image.
                foreach (var import in symbolProvider.GetImportedModules())
                {
                    image.Imports.Add(import);
                }

                // Copy any collected base relocations over to the image.
                foreach (var relocation in symbolProvider.GetBaseRelocations())
                {
                    image.Relocations.Add(relocation);
                }

                // Copy over debug data.
                for (int i = 0; i < module.DebugData.Count; i++)
                {
                    image.DebugData.Add(module.DebugData[i]);
                }
            }
            catch (Exception ex)
            {
                context.DiagnosticBag.Exceptions.Add(ex);
                context.DiagnosticBag.MarkAsFatal();
            }

            tokenMapping ??= new TokenMapping();
            return(new PEImageBuildResult(image, context.DiagnosticBag, tokenMapping));
        }
コード例 #2
0
 /// <summary>
 /// Creates a new instance of the <see cref="PEImageBuildResult"/> class.
 /// </summary>
 /// <param name="image">The constructed image, or <c>null</c> if the construction failed.</param>
 /// <param name="diagnosticBag">The diagnostics that were collected during the construction of the image.</param>
 /// <param name="tokenMapping">An object that maps metadata members to their newly assigned tokens.</param>
 public PEImageBuildResult(IPEImage image, DiagnosticBag diagnosticBag, ITokenMapping tokenMapping)
 {
     ConstructedImage = image;
     DiagnosticBag    = diagnosticBag ?? throw new ArgumentNullException(nameof(diagnosticBag));
     TokenMapping     = tokenMapping ?? throw new ArgumentNullException(nameof(tokenMapping));
 }
コード例 #3
0
 /// <summary>
 /// Creates a new instance of teh <see cref="DotNetDirectoryBuildResult"/> class.
 /// </summary>
 /// <param name="directory">The constructed directory.</param>
 /// <param name="mapping">An object defining a mapping between members and their new metadata tokens.</param>
 public DotNetDirectoryBuildResult(IDotNetDirectory directory, ITokenMapping mapping)
 {
     Directory    = directory ?? throw new ArgumentNullException(nameof(directory));
     TokenMapping = mapping ?? throw new ArgumentNullException(nameof(mapping));
 }