| | | 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.Numerics; |
| | | 5 | | |
| | | 6 | | namespace System.Reflection.PortableExecutable |
| | | 7 | | { |
| | | 8 | | public sealed class PEHeaderBuilder |
| | | 9 | | { |
| | | 10 | | // COFF: |
| | 0 | 11 | | public Machine Machine { get; } |
| | 0 | 12 | | public Characteristics ImageCharacteristics { get; } |
| | | 13 | | |
| | | 14 | | // PE: |
| | 0 | 15 | | public byte MajorLinkerVersion { get; } |
| | 0 | 16 | | public byte MinorLinkerVersion { get; } |
| | | 17 | | |
| | 0 | 18 | | public ulong ImageBase { get; } |
| | 0 | 19 | | public int SectionAlignment { get; } |
| | 0 | 20 | | public int FileAlignment { get; } |
| | | 21 | | |
| | 0 | 22 | | public ushort MajorOperatingSystemVersion { get; } |
| | 0 | 23 | | public ushort MinorOperatingSystemVersion { get; } |
| | | 24 | | |
| | 0 | 25 | | public ushort MajorImageVersion { get; } |
| | 0 | 26 | | public ushort MinorImageVersion { get; } |
| | | 27 | | |
| | 0 | 28 | | public ushort MajorSubsystemVersion { get; } |
| | 0 | 29 | | public ushort MinorSubsystemVersion { get; } |
| | | 30 | | |
| | 0 | 31 | | public Subsystem Subsystem { get; } |
| | 0 | 32 | | public DllCharacteristics DllCharacteristics { get; } |
| | | 33 | | |
| | 0 | 34 | | public ulong SizeOfStackReserve { get; } |
| | 0 | 35 | | public ulong SizeOfStackCommit { get; } |
| | 0 | 36 | | public ulong SizeOfHeapReserve { get; } |
| | 0 | 37 | | public ulong SizeOfHeapCommit { get; } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Creates PE header builder. |
| | | 41 | | /// </summary> |
| | | 42 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | | 43 | | /// <paramref name="fileAlignment"/> is not power of 2 between 512 and 64K, or |
| | | 44 | | /// <paramref name="sectionAlignment"/> not power of 2 or it's less than <paramref name="fileAlignment"/>. |
| | | 45 | | /// </exception> |
| | 0 | 46 | | public PEHeaderBuilder( |
| | 0 | 47 | | Machine machine = 0, |
| | 0 | 48 | | int sectionAlignment = 0x2000, |
| | 0 | 49 | | int fileAlignment = 0x200, |
| | 0 | 50 | | ulong imageBase = 0x00400000, |
| | 0 | 51 | | byte majorLinkerVersion = 0x30, // (what is ref.emit using?) |
| | 0 | 52 | | byte minorLinkerVersion = 0, |
| | 0 | 53 | | ushort majorOperatingSystemVersion = 4, |
| | 0 | 54 | | ushort minorOperatingSystemVersion = 0, |
| | 0 | 55 | | ushort majorImageVersion = 0, |
| | 0 | 56 | | ushort minorImageVersion = 0, |
| | 0 | 57 | | ushort majorSubsystemVersion = 4, |
| | 0 | 58 | | ushort minorSubsystemVersion = 0, |
| | 0 | 59 | | Subsystem subsystem = Subsystem.WindowsCui, |
| | 0 | 60 | | DllCharacteristics dllCharacteristics = DllCharacteristics.DynamicBase | DllCharacteristics.NxCompatible | D |
| | 0 | 61 | | Characteristics imageCharacteristics = Characteristics.Dll, |
| | 0 | 62 | | ulong sizeOfStackReserve = 0x00100000, |
| | 0 | 63 | | ulong sizeOfStackCommit = 0x1000, |
| | 0 | 64 | | ulong sizeOfHeapReserve = 0x00100000, |
| | 0 | 65 | | ulong sizeOfHeapCommit = 0x1000) |
| | 0 | 66 | | { |
| | 0 | 67 | | if (fileAlignment < 512 || fileAlignment > 64 * 1024 || BitOperations.PopCount((uint)fileAlignment) != 1) |
| | 0 | 68 | | { |
| | 0 | 69 | | Throw.ArgumentOutOfRange(nameof(fileAlignment)); |
| | | 70 | | } |
| | | 71 | | |
| | 0 | 72 | | if (sectionAlignment < fileAlignment || BitOperations.PopCount((uint)sectionAlignment) != 1) |
| | 0 | 73 | | { |
| | 0 | 74 | | Throw.ArgumentOutOfRange(nameof(sectionAlignment)); |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | Machine = machine; |
| | 0 | 78 | | SectionAlignment = sectionAlignment; |
| | 0 | 79 | | FileAlignment = fileAlignment; |
| | 0 | 80 | | ImageBase = imageBase; |
| | 0 | 81 | | MajorLinkerVersion = majorLinkerVersion; |
| | 0 | 82 | | MinorLinkerVersion = minorLinkerVersion; |
| | 0 | 83 | | MajorOperatingSystemVersion = majorOperatingSystemVersion; |
| | 0 | 84 | | MinorOperatingSystemVersion = minorOperatingSystemVersion; |
| | 0 | 85 | | MajorImageVersion = majorImageVersion; |
| | 0 | 86 | | MinorImageVersion = minorImageVersion; |
| | 0 | 87 | | MajorSubsystemVersion = majorSubsystemVersion; |
| | 0 | 88 | | MinorSubsystemVersion = minorSubsystemVersion; |
| | 0 | 89 | | Subsystem = subsystem; |
| | 0 | 90 | | DllCharacteristics = dllCharacteristics; |
| | 0 | 91 | | ImageCharacteristics = imageCharacteristics; |
| | 0 | 92 | | SizeOfStackReserve = sizeOfStackReserve; |
| | 0 | 93 | | SizeOfStackCommit = sizeOfStackCommit; |
| | 0 | 94 | | SizeOfHeapReserve = sizeOfHeapReserve; |
| | 0 | 95 | | SizeOfHeapCommit = sizeOfHeapCommit; |
| | 0 | 96 | | } |
| | | 97 | | |
| | | 98 | | public static PEHeaderBuilder CreateExecutableHeader() |
| | 0 | 99 | | { |
| | 0 | 100 | | return new PEHeaderBuilder(imageCharacteristics: Characteristics.ExecutableImage); |
| | 0 | 101 | | } |
| | | 102 | | |
| | | 103 | | public static PEHeaderBuilder CreateLibraryHeader() |
| | 0 | 104 | | { |
| | 0 | 105 | | return new PEHeaderBuilder(imageCharacteristics: Characteristics.ExecutableImage | Characteristics.Dll); |
| | 0 | 106 | | } |
| | | 107 | | |
| | 0 | 108 | | internal bool Is32Bit => Machine != Machine.Amd64 && Machine != Machine.IA64 && Machine != Machine.Arm64 && Mach |
| | | 109 | | |
| | | 110 | | internal int ComputeSizeOfPEHeaders(int sectionCount) => |
| | 0 | 111 | | PEBuilder.DosHeader.Length + |
| | 0 | 112 | | PEHeaders.PESignatureSize + |
| | 0 | 113 | | CoffHeader.Size + |
| | 0 | 114 | | PEHeader.Size(Is32Bit) + |
| | 0 | 115 | | SectionHeader.Size * sectionCount; |
| | | 116 | | } |
| | | 117 | | } |
| | | 118 | | |