| | | 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.Reflection.Metadata; |
| | | 7 | | |
| | | 8 | | namespace System.Reflection.PortableExecutable |
| | | 9 | | { |
| | | 10 | | public sealed partial class DebugDirectoryBuilder |
| | | 11 | | { |
| | | 12 | | private struct Entry |
| | | 13 | | { |
| | | 14 | | public uint Stamp; |
| | | 15 | | public uint Version; |
| | | 16 | | public DebugDirectoryEntryType Type; |
| | | 17 | | public int DataSize; |
| | | 18 | | } |
| | | 19 | | |
| | | 20 | | private readonly List<Entry> _entries; |
| | | 21 | | private readonly BlobBuilder _dataBuilder; |
| | | 22 | | |
| | 0 | 23 | | public DebugDirectoryBuilder() |
| | 0 | 24 | | { |
| | 0 | 25 | | _entries = new List<Entry>(3); |
| | 0 | 26 | | _dataBuilder = new BlobBuilder(); |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | internal void AddEntry(DebugDirectoryEntryType type, uint version, uint stamp, int dataSize) |
| | 0 | 30 | | { |
| | 0 | 31 | | _entries.Add(new Entry() |
| | 0 | 32 | | { |
| | 0 | 33 | | Stamp = stamp, |
| | 0 | 34 | | Version = version, |
| | 0 | 35 | | Type = type, |
| | 0 | 36 | | DataSize = dataSize, |
| | 0 | 37 | | }); |
| | 0 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Adds an entry. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="type">Entry type.</param> |
| | | 44 | | /// <param name="version">Entry version.</param> |
| | | 45 | | /// <param name="stamp">Entry stamp.</param> |
| | | 46 | | public void AddEntry(DebugDirectoryEntryType type, uint version, uint stamp) |
| | 0 | 47 | | => AddEntry(type, version, stamp, dataSize: 0); |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Adds an entry. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <typeparam name="TData">Type of data passed to <paramref name="dataSerializer"/>.</typeparam> |
| | | 53 | | /// <param name="type">Entry type.</param> |
| | | 54 | | /// <param name="version">Entry version.</param> |
| | | 55 | | /// <param name="stamp">Entry stamp.</param> |
| | | 56 | | /// <param name="data">Data passed to <paramref name="dataSerializer"/>.</param> |
| | | 57 | | /// <param name="dataSerializer">Serializes data to a <see cref="BlobBuilder"/>.</param> |
| | | 58 | | public void AddEntry<TData>(DebugDirectoryEntryType type, uint version, uint stamp, TData data, Action<BlobBuild |
| | 0 | 59 | | { |
| | 0 | 60 | | if (dataSerializer is null) |
| | 0 | 61 | | { |
| | 0 | 62 | | Throw.ArgumentNull(nameof(dataSerializer)); |
| | | 63 | | } |
| | | 64 | | |
| | 0 | 65 | | int start = _dataBuilder.Count; |
| | 0 | 66 | | dataSerializer(_dataBuilder, data); |
| | 0 | 67 | | int dataSize = _dataBuilder.Count - start; |
| | | 68 | | |
| | 0 | 69 | | AddEntry(type, version, stamp, dataSize); |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Adds a CodeView entry. |
| | | 74 | | /// </summary> |
| | | 75 | | /// <param name="pdbPath">Path to the PDB. Shall not be empty.</param> |
| | | 76 | | /// <param name="pdbContentId">Unique id of the PDB content.</param> |
| | | 77 | | /// <param name="portablePdbVersion">Version of Portable PDB format (e.g. 0x0100 for 1.0), or 0 if the PDB is no |
| | | 78 | | /// <exception cref="ArgumentNullException"><paramref name="pdbPath"/> is null.</exception> |
| | | 79 | | /// <exception cref="ArgumentException"><paramref name="pdbPath"/> contains NUL character.</exception> |
| | | 80 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="portablePdbVersion"/> is smaller than 0x0100.< |
| | | 81 | | public void AddCodeViewEntry( |
| | | 82 | | string pdbPath, |
| | | 83 | | BlobContentId pdbContentId, |
| | | 84 | | ushort portablePdbVersion) |
| | 0 | 85 | | { |
| | 0 | 86 | | AddCodeViewEntry(pdbPath, pdbContentId, portablePdbVersion, age: 1); |
| | 0 | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Adds a CodeView entry. |
| | | 91 | | /// </summary> |
| | | 92 | | /// <param name="pdbPath">Path to the PDB. Shall not be empty.</param> |
| | | 93 | | /// <param name="pdbContentId">Unique id of the PDB content.</param> |
| | | 94 | | /// <param name="portablePdbVersion">Version of Portable PDB format (e.g. 0x0100 for 1.0), or 0 if the PDB is no |
| | | 95 | | /// <param name="age">Age (iteration) of the PDB. Shall be 1 for Portable PDBs.</param> |
| | | 96 | | /// <exception cref="ArgumentNullException"><paramref name="pdbPath"/> is null.</exception> |
| | | 97 | | /// <exception cref="ArgumentException"><paramref name="pdbPath"/> contains NUL character.</exception> |
| | | 98 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="age"/> is less than 1.</exception> |
| | | 99 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="portablePdbVersion"/> is smaller than 0x0100.< |
| | | 100 | | public void AddCodeViewEntry( |
| | | 101 | | string pdbPath, |
| | | 102 | | BlobContentId pdbContentId, |
| | | 103 | | ushort portablePdbVersion, |
| | | 104 | | int age) |
| | 0 | 105 | | { |
| | 0 | 106 | | if (pdbPath is null) |
| | 0 | 107 | | { |
| | 0 | 108 | | Throw.ArgumentNull(nameof(pdbPath)); |
| | | 109 | | } |
| | | 110 | | |
| | 0 | 111 | | if (age < 1) |
| | 0 | 112 | | { |
| | 0 | 113 | | Throw.ArgumentOutOfRange(nameof(age)); |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | // We allow NUL characters to allow for padding for backward compat purposes. |
| | 0 | 117 | | if (pdbPath.Length == 0 || pdbPath[0] == '\0') |
| | 0 | 118 | | { |
| | 0 | 119 | | Throw.InvalidArgument(SR.ExpectedNonEmptyString, nameof(pdbPath)); |
| | | 120 | | } |
| | | 121 | | |
| | 0 | 122 | | if (portablePdbVersion > 0 && portablePdbVersion < PortablePdbVersions.MinFormatVersion) |
| | 0 | 123 | | { |
| | 0 | 124 | | Throw.ArgumentOutOfRange(nameof(portablePdbVersion)); |
| | | 125 | | } |
| | | 126 | | |
| | 0 | 127 | | int dataSize = WriteCodeViewData(_dataBuilder, pdbPath, pdbContentId.Guid, age); |
| | | 128 | | |
| | 0 | 129 | | AddEntry( |
| | 0 | 130 | | type: DebugDirectoryEntryType.CodeView, |
| | 0 | 131 | | version: (portablePdbVersion == 0) ? 0 : PortablePdbVersions.DebugDirectoryEntryVersion(portablePdbVersi |
| | 0 | 132 | | stamp: pdbContentId.Stamp, |
| | 0 | 133 | | dataSize); |
| | 0 | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Adds Reproducible entry. |
| | | 138 | | /// </summary> |
| | | 139 | | public void AddReproducibleEntry() |
| | 0 | 140 | | => AddEntry(type: DebugDirectoryEntryType.Reproducible, version: 0, stamp: 0); |
| | | 141 | | |
| | | 142 | | private static int WriteCodeViewData(BlobBuilder builder, string pdbPath, Guid pdbGuid, int age) |
| | 0 | 143 | | { |
| | 0 | 144 | | int start = builder.Count; |
| | | 145 | | |
| | 0 | 146 | | builder.WriteByte((byte)'R'); |
| | 0 | 147 | | builder.WriteByte((byte)'S'); |
| | 0 | 148 | | builder.WriteByte((byte)'D'); |
| | 0 | 149 | | builder.WriteByte((byte)'S'); |
| | | 150 | | |
| | | 151 | | // PDB id: |
| | 0 | 152 | | builder.WriteGuid(pdbGuid); |
| | | 153 | | |
| | | 154 | | // age |
| | 0 | 155 | | builder.WriteInt32(age); |
| | | 156 | | |
| | | 157 | | // UTF-8 encoded zero-terminated path to PDB |
| | 0 | 158 | | builder.WriteUTF8(pdbPath, allowUnpairedSurrogates: true); |
| | 0 | 159 | | builder.WriteByte(0); |
| | | 160 | | |
| | 0 | 161 | | return builder.Count - start; |
| | 0 | 162 | | } |
| | | 163 | | |
| | | 164 | | /// <summary> |
| | | 165 | | /// Adds PDB checksum entry. |
| | | 166 | | /// </summary> |
| | | 167 | | /// <param name="algorithmName">Hash algorithm name (e.g. "SHA256").</param> |
| | | 168 | | /// <param name="checksum">Checksum.</param> |
| | | 169 | | /// <exception cref="ArgumentNullException"><paramref name="algorithmName"/> or <paramref name="checksum"/> is n |
| | | 170 | | /// <exception cref="ArgumentException"><paramref name="algorithmName"/> or <paramref name="checksum"/> is empty |
| | | 171 | | public void AddPdbChecksumEntry(string algorithmName, ImmutableArray<byte> checksum) |
| | 0 | 172 | | { |
| | 0 | 173 | | if (algorithmName is null) |
| | 0 | 174 | | { |
| | 0 | 175 | | Throw.ArgumentNull(nameof(algorithmName)); |
| | | 176 | | } |
| | | 177 | | |
| | 0 | 178 | | if (algorithmName.Length == 0) |
| | 0 | 179 | | { |
| | 0 | 180 | | Throw.ArgumentEmptyString(nameof(algorithmName)); |
| | | 181 | | } |
| | | 182 | | |
| | 0 | 183 | | if (checksum.IsDefault) |
| | 0 | 184 | | { |
| | 0 | 185 | | Throw.ArgumentNull(nameof(checksum)); |
| | | 186 | | } |
| | | 187 | | |
| | 0 | 188 | | if (checksum.Length == 0) |
| | 0 | 189 | | { |
| | 0 | 190 | | Throw.ArgumentEmptyArray(nameof(checksum)); |
| | | 191 | | } |
| | | 192 | | |
| | 0 | 193 | | int dataSize = WritePdbChecksumData(_dataBuilder, algorithmName, checksum); |
| | | 194 | | |
| | 0 | 195 | | AddEntry( |
| | 0 | 196 | | type: DebugDirectoryEntryType.PdbChecksum, |
| | 0 | 197 | | version: 0x00000001, |
| | 0 | 198 | | stamp: 0x00000000, |
| | 0 | 199 | | dataSize); |
| | 0 | 200 | | } |
| | | 201 | | |
| | | 202 | | private static int WritePdbChecksumData(BlobBuilder builder, string algorithmName, ImmutableArray<byte> checksum |
| | 0 | 203 | | { |
| | 0 | 204 | | int start = builder.Count; |
| | | 205 | | |
| | | 206 | | // NUL-terminated algorithm name: |
| | 0 | 207 | | builder.WriteUTF8(algorithmName, allowUnpairedSurrogates: true); |
| | 0 | 208 | | builder.WriteByte(0); |
| | | 209 | | |
| | | 210 | | // checksum: |
| | 0 | 211 | | builder.WriteBytes(checksum); |
| | | 212 | | |
| | 0 | 213 | | return builder.Count - start; |
| | 0 | 214 | | } |
| | | 215 | | |
| | 0 | 216 | | internal int TableSize => DebugDirectoryEntry.Size * _entries.Count; |
| | 0 | 217 | | internal int Size => TableSize + _dataBuilder?.Count ?? 0; |
| | | 218 | | |
| | | 219 | | /// <summary> |
| | | 220 | | /// Serialize the Debug Table and Data. |
| | | 221 | | /// </summary> |
| | | 222 | | /// <param name="builder">Builder.</param> |
| | | 223 | | /// <param name="sectionLocation">The containing PE section location.</param> |
| | | 224 | | /// <param name="sectionOffset">Offset of the table within the containing section.</param> |
| | | 225 | | internal void Serialize(BlobBuilder builder, SectionLocation sectionLocation, int sectionOffset) |
| | 0 | 226 | | { |
| | 0 | 227 | | int dataOffset = sectionOffset + TableSize; |
| | 0 | 228 | | foreach (var entry in _entries) |
| | 0 | 229 | | { |
| | | 230 | | int addressOfRawData; |
| | | 231 | | int pointerToRawData; |
| | 0 | 232 | | if (entry.DataSize > 0) |
| | 0 | 233 | | { |
| | 0 | 234 | | addressOfRawData = sectionLocation.RelativeVirtualAddress + dataOffset; |
| | 0 | 235 | | pointerToRawData = sectionLocation.PointerToRawData + dataOffset; |
| | 0 | 236 | | } |
| | | 237 | | else |
| | 0 | 238 | | { |
| | 0 | 239 | | addressOfRawData = 0; |
| | 0 | 240 | | pointerToRawData = 0; |
| | 0 | 241 | | } |
| | | 242 | | |
| | 0 | 243 | | builder.WriteUInt32(0); // characteristics, always 0 |
| | 0 | 244 | | builder.WriteUInt32(entry.Stamp); |
| | 0 | 245 | | builder.WriteUInt32(entry.Version); |
| | 0 | 246 | | builder.WriteInt32((int)entry.Type); |
| | 0 | 247 | | builder.WriteInt32(entry.DataSize); |
| | 0 | 248 | | builder.WriteInt32(addressOfRawData); |
| | 0 | 249 | | builder.WriteInt32(pointerToRawData); |
| | | 250 | | |
| | 0 | 251 | | dataOffset += entry.DataSize; |
| | 0 | 252 | | } |
| | | 253 | | |
| | 0 | 254 | | builder.LinkSuffix(_dataBuilder); |
| | 0 | 255 | | } |
| | | 256 | | } |
| | | 257 | | } |
| | | 258 | | |