Repository: dotnet/runtime
File: src/coreclr/tools/Common/Compiler/ObjectWriter/PEObjectWriter.cs (C#)

Code before:
                        ResolveRelocations(i, relocationsToResolve, (long)peOptional.ImageBase, stream);
                        stream.Position = 0;
                        stream.CopyTo(outputFileStream);
                    }
                    else
                    {
                        section.Stream.Position = 0;
                        section.Stream.CopyTo(outputFileStream);
                    }
                }
            }

            // Align the output file size with the image (including trailing padding for section and file alignment).
            Debug.Assert(outputFileStream.Position <= sizeOfImage);
            outputFileStream.SetLength(sizeOfImage);
        }

        private void PopulateDataDirectoryForWellKnownSymbolfPresent(OptionalHeaderDataDirectories dataDirs, ImageDirectoryEntry directory, SortableDependencyNode.ObjectNodeOrder wellKnownSymbol)
        {
            if (_wellKnownSymbols.TryGetValue(wellKnownSymbol, out Utf8String symbolName))
            {
                PopulateDataDirectoryForWellKnownSymbolfPresent(dataDirs, directory, symbolName);
            }
        }

        private void PopulateDataDirectoryForWellKnownSymbolfPresent(OptionalHeaderDataDirectories dataDirs, ImageDirectoryEntry directory, Utf8String symbolName)
        {
            if (_definedSymbols.TryGetValue(symbolName, out SymbolDefinition symbol))
            {
                dataDirs.SetIfNonEmpty((int)directory, checked((uint)(_outputSectionLayout[symbol.SectionIndex].VirtualAddress + (ulong)symbol.Value)), (uint)symbol.Size);
            }
        }

        private protected override void EmitChecksumsForObject(Stream outputFileStream, List<ChecksumsToCalculate> checksumRelocations, ReadOnlySpan<byte> originalOutput)
        {
            base.EmitChecksumsForObject(outputFileStream, checksumRelocations, originalOutput);

            if (_coffTimestamp is null)
            {
                // If we were not provided a deterministic timestamp, generate one from a hash of the content.
                outputFileStream.Seek(_coffHeaderOffset + CoffHeader.TimeDateStampOffset(bigObj: false), SeekOrigin.Begin);
                using BinaryWriter writer = new(outputFileStream, Encoding.UTF8, leaveOpen: true);
                writer.Write(BlobContentId.FromHash(SHA256.HashData(originalOutput)).Stamp);
            }
        }

        private unsafe void ResolveRelocations(int sectionIndex, List<SymbolicRelocation> symbolicRelocations, long imageBase, MemoryStream stream)
        {
            foreach (SymbolicRelocation reloc in symbolicRelocations)
            {
                SymbolDefinition definedSymbol = _definedSymbols[reloc.SymbolName];

Review comment (suggestion):
The method name "PopulateDataDirectoryForWellKnownSymbolfPresent" contains a typo. "Symbolf" should be "SymbolIf" (method name should be "PopulateDataDirectoryForWellKnownSymbolIfPresent").
```suggestion
        private void PopulateDataDirectoryForWellKnownSymbolIfPresent(OptionalHeaderDataDirectories dataDirs, ImageDirectoryEntry directory, SortableDependencyNode.ObjectNodeOrder wellKnownSymbol)
        {
            if (_wellKnownSymbols.TryGetValue(wellKnownSymbol, out Utf8String symbolName))
            {
                PopulateDataDirectoryForWellKnownSymbolIfPresent(dataDirs, directory, symbolName);
            }
        }

        private void PopulateDataDirectoryForWellKnownSymbolIfPresent(OptionalHeaderDataDirectories dataDirs, ImageDirectoryEntry directory, Utf8String symbolName)
```

Code after:
                var hdr = _sections[i].Header;
                if (hdr.VirtualSize == 0 && !hdr.SectionCharacteristics.HasFlag(SectionCharacteristics.ContainsUninitializedData))
                {
                    // Don't emit empty sections into the binary
                    continue;
                }

                // The alignment flags aren't valid in PE image files, only in COFF object files.
                hdr.SectionCharacteristics &= ~SectionCharacteristics.AlignMask;
                hdr.Write(outputFileStream, stringTable);
            }

            // Write section content
            for (int i = 0; i < _sections.Count; i++)
            {
                SectionDefinition section = _sections[i];
                if (section.Header.VirtualSize != 0 && !section.Header.SectionCharacteristics.HasFlag(SectionCharacteristics.ContainsUninitializedData))
                {
                    Debug.Assert(outputFileStream.Position <= section.Header.PointerToRawData);
                    outputFileStream.Position = section.Header.PointerToRawData; // Pad to alignment
                    if (_resolvableRelocations.TryGetValue(i, out List<SymbolicRelocation> relocationsToResolve))
                    {
                        // Resolve all relocations we can't represent in a PE as we write out the data.
                        MemoryStream stream = new((int)section.Stream.Length);
                        section.Stream.Position = 0;
                        section.Stream.CopyTo(stream);
                        ResolveRelocations(i, relocationsToResolve, (long)peOptional.ImageBase, stream);
                        stream.Position = 0;
                        stream.CopyTo(outputFileStream);
                    }
                    else
                    {
                        section.Stream.Position = 0;
                        section.Stream.CopyTo(outputFileStream);
                    }
                }
            }

            // Align the output file size with the image (including trailing padding for section and file alignment).
            Debug.Assert(outputFileStream.Position <= sizeOfImage);
            outputFileStream.SetLength(sizeOfImage);
        }

        private void PopulateDataDirectoryForWellKnownSymbolIfPresent(OptionalHeaderDataDirectories dataDirs, ImageDirectoryEntry directory, SortableDependencyNode.ObjectNodeOrder wellKnownSymbol)
        {
            if (_wellKnownSymbols.TryGetValue(wellKnownSymbol, out Utf8String symbolName))
            {
                PopulateDataDirectoryForWellKnownSymbolIfPresent(dataDirs, directory, symbolName);
            }
        }