| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Collections.Immutable; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Reflection.Metadata; |
| | | 8 | | using System.Reflection.Metadata.Ecma335; |
| | | 9 | | |
| | | 10 | | namespace System.Reflection.PortableExecutable |
| | | 11 | | { |
| | | 12 | | public class ManagedPEBuilder : PEBuilder |
| | | 13 | | { |
| | | 14 | | public const int ManagedResourcesDataAlignment = ManagedTextSection.ManagedResourcesDataAlignment; |
| | | 15 | | public const int MappedFieldDataAlignment = ManagedTextSection.MappedFieldDataAlignment; |
| | | 16 | | |
| | | 17 | | private const int DefaultStrongNameSignatureSize = 128; |
| | | 18 | | |
| | | 19 | | private const string TextSectionName = ".text"; |
| | | 20 | | private const string ResourceSectionName = ".rsrc"; |
| | | 21 | | private const string RelocationSectionName = ".reloc"; |
| | | 22 | | |
| | | 23 | | private readonly PEDirectoriesBuilder _peDirectoriesBuilder; |
| | | 24 | | private readonly MetadataRootBuilder _metadataRootBuilder; |
| | | 25 | | private readonly BlobBuilder _ilStream; |
| | | 26 | | private readonly BlobBuilder? _mappedFieldDataOpt; |
| | | 27 | | private readonly BlobBuilder? _managedResourcesOpt; |
| | | 28 | | private readonly ResourceSectionBuilder? _nativeResourcesOpt; |
| | | 29 | | private readonly int _strongNameSignatureSize; |
| | | 30 | | private readonly MethodDefinitionHandle _entryPointOpt; |
| | | 31 | | private readonly DebugDirectoryBuilder? _debugDirectoryBuilderOpt; |
| | | 32 | | private readonly CorFlags _corFlags; |
| | | 33 | | |
| | | 34 | | private int _lazyEntryPointAddress; |
| | | 35 | | private Blob _lazyStrongNameSignature; |
| | | 36 | | |
| | | 37 | | public ManagedPEBuilder( |
| | | 38 | | PEHeaderBuilder header, |
| | | 39 | | MetadataRootBuilder metadataRootBuilder, |
| | | 40 | | BlobBuilder ilStream, |
| | | 41 | | BlobBuilder? mappedFieldData = null, |
| | | 42 | | BlobBuilder? managedResources = null, |
| | | 43 | | ResourceSectionBuilder? nativeResources = null, |
| | | 44 | | DebugDirectoryBuilder? debugDirectoryBuilder = null, |
| | | 45 | | int strongNameSignatureSize = DefaultStrongNameSignatureSize, |
| | | 46 | | MethodDefinitionHandle entryPoint = default(MethodDefinitionHandle), |
| | | 47 | | CorFlags flags = CorFlags.ILOnly, |
| | | 48 | | Func<IEnumerable<Blob>, BlobContentId>? deterministicIdProvider = null) |
| | 0 | 49 | | : base(header, deterministicIdProvider) |
| | 0 | 50 | | { |
| | 0 | 51 | | if (header is null) |
| | 0 | 52 | | { |
| | 0 | 53 | | Throw.ArgumentNull(nameof(header)); |
| | | 54 | | } |
| | 0 | 55 | | if (metadataRootBuilder is null) |
| | 0 | 56 | | { |
| | 0 | 57 | | Throw.ArgumentNull(nameof(metadataRootBuilder)); |
| | | 58 | | } |
| | 0 | 59 | | if (ilStream is null) |
| | 0 | 60 | | { |
| | 0 | 61 | | Throw.ArgumentNull(nameof(ilStream)); |
| | | 62 | | } |
| | | 63 | | |
| | 0 | 64 | | if (strongNameSignatureSize < 0) |
| | 0 | 65 | | { |
| | 0 | 66 | | Throw.ArgumentOutOfRange(nameof(strongNameSignatureSize)); |
| | | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | _metadataRootBuilder = metadataRootBuilder; |
| | 0 | 70 | | _ilStream = ilStream; |
| | 0 | 71 | | _mappedFieldDataOpt = mappedFieldData; |
| | 0 | 72 | | _managedResourcesOpt = managedResources; |
| | 0 | 73 | | _nativeResourcesOpt = nativeResources; |
| | 0 | 74 | | _strongNameSignatureSize = strongNameSignatureSize; |
| | 0 | 75 | | _entryPointOpt = entryPoint; |
| | 0 | 76 | | _debugDirectoryBuilderOpt = debugDirectoryBuilder ?? CreateDefaultDebugDirectoryBuilder(); |
| | 0 | 77 | | _corFlags = flags; |
| | | 78 | | |
| | 0 | 79 | | _peDirectoriesBuilder = new PEDirectoriesBuilder(); |
| | 0 | 80 | | } |
| | | 81 | | |
| | | 82 | | private DebugDirectoryBuilder? CreateDefaultDebugDirectoryBuilder() |
| | 0 | 83 | | { |
| | 0 | 84 | | if (IsDeterministic) |
| | 0 | 85 | | { |
| | 0 | 86 | | var builder = new DebugDirectoryBuilder(); |
| | 0 | 87 | | builder.AddReproducibleEntry(); |
| | 0 | 88 | | return builder; |
| | | 89 | | } |
| | | 90 | | |
| | 0 | 91 | | return null; |
| | 0 | 92 | | } |
| | | 93 | | |
| | | 94 | | protected override ImmutableArray<Section> CreateSections() |
| | 0 | 95 | | { |
| | 0 | 96 | | var builder = ImmutableArray.CreateBuilder<Section>(3); |
| | 0 | 97 | | builder.Add(new Section(TextSectionName, SectionCharacteristics.MemRead | SectionCharacteristics.MemExecute |
| | | 98 | | |
| | 0 | 99 | | if (_nativeResourcesOpt != null) |
| | 0 | 100 | | { |
| | 0 | 101 | | builder.Add(new Section(ResourceSectionName, SectionCharacteristics.MemRead | SectionCharacteristics.Con |
| | 0 | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | if (Header.Machine == Machine.I386 || Header.Machine == 0) |
| | 0 | 105 | | { |
| | 0 | 106 | | builder.Add(new Section(RelocationSectionName, SectionCharacteristics.MemRead | SectionCharacteristics.M |
| | 0 | 107 | | } |
| | | 108 | | |
| | 0 | 109 | | return builder.ToImmutable(); |
| | 0 | 110 | | } |
| | | 111 | | |
| | | 112 | | protected override BlobBuilder SerializeSection(string name, SectionLocation location) => |
| | 0 | 113 | | name switch |
| | 0 | 114 | | { |
| | 0 | 115 | | TextSectionName => SerializeTextSection(location), |
| | 0 | 116 | | ResourceSectionName => SerializeResourceSection(location), |
| | 0 | 117 | | RelocationSectionName => SerializeRelocationSection(location), |
| | 0 | 118 | | _ => throw new ArgumentException(SR.Format(SR.UnknownSectionName, name), nameof(name)), |
| | 0 | 119 | | }; |
| | | 120 | | |
| | | 121 | | private BlobBuilder SerializeTextSection(SectionLocation location) |
| | 0 | 122 | | { |
| | 0 | 123 | | var sectionBuilder = new BlobBuilder(); |
| | 0 | 124 | | var metadataBuilder = new BlobBuilder(); |
| | | 125 | | |
| | 0 | 126 | | var metadataSizes = _metadataRootBuilder.Sizes; |
| | | 127 | | |
| | 0 | 128 | | var textSection = new ManagedTextSection( |
| | 0 | 129 | | imageCharacteristics: Header.ImageCharacteristics, |
| | 0 | 130 | | machine: Header.Machine, |
| | 0 | 131 | | ilStreamSize: _ilStream.Count, |
| | 0 | 132 | | metadataSize: metadataSizes.MetadataSize, |
| | 0 | 133 | | resourceDataSize: _managedResourcesOpt?.Count ?? 0, |
| | 0 | 134 | | strongNameSignatureSize: _strongNameSignatureSize, |
| | 0 | 135 | | debugDataSize: _debugDirectoryBuilderOpt?.Size ?? 0, |
| | 0 | 136 | | mappedFieldDataSize: _mappedFieldDataOpt?.Count ?? 0); |
| | | 137 | | |
| | 0 | 138 | | int methodBodyStreamRva = location.RelativeVirtualAddress + textSection.OffsetToILStream; |
| | 0 | 139 | | int mappedFieldDataStreamRva = location.RelativeVirtualAddress + textSection.CalculateOffsetToMappedFieldDat |
| | 0 | 140 | | _metadataRootBuilder.Serialize(metadataBuilder, methodBodyStreamRva, mappedFieldDataStreamRva); |
| | | 141 | | |
| | | 142 | | DirectoryEntry debugDirectoryEntry; |
| | | 143 | | BlobBuilder? debugTableBuilderOpt; |
| | 0 | 144 | | if (_debugDirectoryBuilderOpt != null) |
| | 0 | 145 | | { |
| | 0 | 146 | | int debugDirectoryOffset = textSection.ComputeOffsetToDebugDirectory(); |
| | 0 | 147 | | debugTableBuilderOpt = new BlobBuilder(_debugDirectoryBuilderOpt.TableSize); |
| | 0 | 148 | | _debugDirectoryBuilderOpt.Serialize(debugTableBuilderOpt, location, debugDirectoryOffset); |
| | | 149 | | |
| | | 150 | | // Only the size of the fixed part of the debug table goes here. |
| | 0 | 151 | | debugDirectoryEntry = new DirectoryEntry( |
| | 0 | 152 | | location.RelativeVirtualAddress + debugDirectoryOffset, |
| | 0 | 153 | | _debugDirectoryBuilderOpt.TableSize); |
| | 0 | 154 | | } |
| | | 155 | | else |
| | 0 | 156 | | { |
| | 0 | 157 | | debugTableBuilderOpt = null; |
| | 0 | 158 | | debugDirectoryEntry = default(DirectoryEntry); |
| | 0 | 159 | | } |
| | | 160 | | |
| | 0 | 161 | | _lazyEntryPointAddress = textSection.GetEntryPointAddress(location.RelativeVirtualAddress); |
| | | 162 | | |
| | 0 | 163 | | textSection.Serialize( |
| | 0 | 164 | | sectionBuilder, |
| | 0 | 165 | | location.RelativeVirtualAddress, |
| | 0 | 166 | | _entryPointOpt.IsNil ? 0 : MetadataTokens.GetToken(_entryPointOpt), |
| | 0 | 167 | | _corFlags, |
| | 0 | 168 | | Header.ImageBase, |
| | 0 | 169 | | metadataBuilder, |
| | 0 | 170 | | _ilStream, |
| | 0 | 171 | | _mappedFieldDataOpt, |
| | 0 | 172 | | _managedResourcesOpt, |
| | 0 | 173 | | debugTableBuilderOpt, |
| | 0 | 174 | | out _lazyStrongNameSignature); |
| | | 175 | | |
| | 0 | 176 | | _peDirectoriesBuilder.AddressOfEntryPoint = _lazyEntryPointAddress; |
| | 0 | 177 | | _peDirectoriesBuilder.DebugTable = debugDirectoryEntry; |
| | 0 | 178 | | _peDirectoriesBuilder.ImportAddressTable = textSection.GetImportAddressTableDirectoryEntry(location.Relative |
| | 0 | 179 | | _peDirectoriesBuilder.ImportTable = textSection.GetImportTableDirectoryEntry(location.RelativeVirtualAddress |
| | 0 | 180 | | _peDirectoriesBuilder.CorHeaderTable = textSection.GetCorHeaderDirectoryEntry(location.RelativeVirtualAddres |
| | | 181 | | |
| | 0 | 182 | | return sectionBuilder; |
| | 0 | 183 | | } |
| | | 184 | | |
| | | 185 | | private BlobBuilder SerializeResourceSection(SectionLocation location) |
| | 0 | 186 | | { |
| | 0 | 187 | | Debug.Assert(_nativeResourcesOpt != null); |
| | | 188 | | |
| | 0 | 189 | | var sectionBuilder = new BlobBuilder(); |
| | 0 | 190 | | _nativeResourcesOpt.Serialize(sectionBuilder, location); |
| | | 191 | | |
| | 0 | 192 | | _peDirectoriesBuilder.ResourceTable = new DirectoryEntry(location.RelativeVirtualAddress, sectionBuilder.Cou |
| | 0 | 193 | | return sectionBuilder; |
| | 0 | 194 | | } |
| | | 195 | | |
| | | 196 | | private BlobBuilder SerializeRelocationSection(SectionLocation location) |
| | 0 | 197 | | { |
| | 0 | 198 | | var sectionBuilder = new BlobBuilder(); |
| | 0 | 199 | | WriteRelocationSection(sectionBuilder, Header.Machine, _lazyEntryPointAddress); |
| | | 200 | | |
| | 0 | 201 | | _peDirectoriesBuilder.BaseRelocationTable = new DirectoryEntry(location.RelativeVirtualAddress, sectionBuild |
| | 0 | 202 | | return sectionBuilder; |
| | 0 | 203 | | } |
| | | 204 | | |
| | | 205 | | private static void WriteRelocationSection(BlobBuilder builder, Machine machine, int entryPointAddress) |
| | 0 | 206 | | { |
| | 0 | 207 | | Debug.Assert(builder.Count == 0); |
| | | 208 | | |
| | 0 | 209 | | builder.WriteUInt32((((uint)entryPointAddress + 2) / 0x1000) * 0x1000); |
| | 0 | 210 | | builder.WriteUInt32((machine == Machine.IA64) ? 14u : 12u); |
| | 0 | 211 | | uint offsetWithinPage = ((uint)entryPointAddress + 2) % 0x1000; |
| | 0 | 212 | | uint relocType = (machine == Machine.Amd64 || machine == Machine.IA64 || machine == Machine.Arm64) ? 10u : 3 |
| | 0 | 213 | | ushort s = (ushort)((relocType << 12) | offsetWithinPage); |
| | 0 | 214 | | builder.WriteUInt16(s); |
| | 0 | 215 | | if (machine == Machine.IA64) |
| | 0 | 216 | | { |
| | 0 | 217 | | builder.WriteUInt32(relocType << 12); |
| | 0 | 218 | | } |
| | | 219 | | |
| | 0 | 220 | | builder.WriteUInt16(0); // next chunk's RVA |
| | 0 | 221 | | } |
| | | 222 | | |
| | | 223 | | protected internal override PEDirectoriesBuilder GetDirectories() |
| | 0 | 224 | | { |
| | 0 | 225 | | return _peDirectoriesBuilder; |
| | 0 | 226 | | } |
| | | 227 | | |
| | | 228 | | public void Sign(BlobBuilder peImage, Func<IEnumerable<Blob>, byte[]> signatureProvider) |
| | 0 | 229 | | { |
| | 0 | 230 | | if (peImage is null) |
| | 0 | 231 | | { |
| | 0 | 232 | | Throw.ArgumentNull(nameof(peImage)); |
| | | 233 | | } |
| | 0 | 234 | | if (signatureProvider is null) |
| | 0 | 235 | | { |
| | 0 | 236 | | Throw.ArgumentNull(nameof(signatureProvider)); |
| | | 237 | | } |
| | | 238 | | |
| | 0 | 239 | | Sign(peImage!, _lazyStrongNameSignature, signatureProvider!); |
| | 0 | 240 | | } |
| | | 241 | | } |
| | | 242 | | } |
| | | 243 | | |