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

Code before:
                DllCharacteristics.DynamicBase |
                DllCharacteristics.NxCompatible |
                DllCharacteristics.TerminalServerAware;
            if (isPE32Plus)
            {
                dllCharacteristics |= DllCharacteristics.HighEntropyVirtualAddressSpace;
            }
            else
            {
                dllCharacteristics |= DllCharacteristics.NoSeh;
            }

            peOptional.DllCharacteristics = dllCharacteristics;
            peOptional.SectionAlignment = _peSectionAlignment;
            peOptional.FileAlignment = _peFileAlignment;

            // Create data directories object and pass it to the optional header writer.
            // Entries are zeroed by default; callers may populate particular directories
            // before writing if needed.
            var dataDirs = new OptionalHeaderDataDirectories();
            // Populate data directories if present.
            PopulateDataDirectoryForWellKnownSymbolfPresent(dataDirs, ImageDirectoryEntry.Resource, SortableDependencyNode.ObjectNodeOrder.Win32ResourcesNode);
            PopulateDataDirectoryForWellKnownSymbolfPresent(dataDirs, ImageDirectoryEntry.Debug, SortableDependencyNode.ObjectNodeOrder.DebugDirectoryNode);
            PopulateDataDirectoryForWellKnownSymbolfPresent(dataDirs, ImageDirectoryEntry.CLRRuntimeHeader, SortableDependencyNode.ObjectNodeOrder.CorHeaderNode);
            PopulateDataDirectoryForWellKnownSymbolfPresent(dataDirs, ImageDirectoryEntry.Exception, SortableDependencyNode.ObjectNodeOrder.RuntimeFunctionsTableNode);
            PopulateDataDirectoryForWellKnownSymbolfPresent(dataDirs, ImageDirectoryEntry.Export, ExportDirectorySymbol);

            if (_baseRelocSectionIndex != NoSectionIndex)
            {
                dataDirs.SetIfNonEmpty((int)ImageDirectoryEntry.BaseRelocation, (uint)_outputSectionLayout[_baseRelocSectionIndex].VirtualAddress, (uint)_outputSectionLayout[_baseRelocSectionIndex].Length);
            }

            peOptional.Write(outputFileStream, dataDirs);

            CoffStringTable stringTable = new();

            // Emit headers for each section in the order they appear in _sections
            for (int i = 0; i < _sections.Count; i++)
            {
                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);
            }


Review comment (bug):
The method name "PopulateDataDirectoryForWellKnownSymbolfPresent" contains a typo. "Symbolf" should be "SymbolIf" (method name should be "PopulateDataDirectoryForWellKnownSymbolIfPresent").
```suggestion
            PopulateDataDirectoryForWellKnownSymbolIfPresent(dataDirs, ImageDirectoryEntry.Resource, SortableDependencyNode.ObjectNodeOrder.Win32ResourcesNode);
            PopulateDataDirectoryForWellKnownSymbolIfPresent(dataDirs, ImageDirectoryEntry.Debug, SortableDependencyNode.ObjectNodeOrder.DebugDirectoryNode);
            PopulateDataDirectoryForWellKnownSymbolIfPresent(dataDirs, ImageDirectoryEntry.CLRRuntimeHeader, SortableDependencyNode.ObjectNodeOrder.CorHeaderNode);
            PopulateDataDirectoryForWellKnownSymbolIfPresent(dataDirs, ImageDirectoryEntry.Exception, SortableDependencyNode.ObjectNodeOrder.RuntimeFunctionsTableNode);
            PopulateDataDirectoryForWellKnownSymbolIfPresent(dataDirs, ImageDirectoryEntry.Export, ExportDirectorySymbol);
```

Code after:
                NumberOfSections = (uint)numberOfSections,
                TimeDateStamp = (uint)(_coffTimestamp ?? 0),
                PointerToSymbolTable = 0,
                NumberOfSymbols = 0,
                SizeOfOptionalHeader = sizeOfOptionalHeader,
                Characteristics = characteristics,
            };

            _coffHeaderOffset = outputFileStream.Position;

            coffHeader.Write(outputFileStream);

            var peOptional = new PEOptionalHeader(isPE32Plus)
            {
                SizeOfCode = sizeOfCode,
                SizeOfInitializedData = sizeOfInitializedData,
                AddressOfEntryPoint = 0u,
                BaseOfCode = 0u,
                BaseOfData = 0u,
                SizeOfImage = sizeOfImage,
                SizeOfHeaders = sizeOfHeaders,
                NumberOfRvaAndSizes = 16u,
            };

            // Set DLL characteristics similar to PEHeaderProvider.Create
            DllCharacteristics dllCharacteristics =
                DllCharacteristics.DynamicBase |
                DllCharacteristics.NxCompatible |
                DllCharacteristics.TerminalServerAware;
            if (isPE32Plus)
            {
                dllCharacteristics |= DllCharacteristics.HighEntropyVirtualAddressSpace;
            }
            else
            {
                dllCharacteristics |= DllCharacteristics.NoSeh;
            }

            peOptional.DllCharacteristics = dllCharacteristics;
            peOptional.SectionAlignment = _peSectionAlignment;
            peOptional.FileAlignment = _peFileAlignment;

            // Create data directories object and pass it to the optional header writer.
            // Entries are zeroed by default; callers may populate particular directories
            // before writing if needed.
            var dataDirs = new OptionalHeaderDataDirectories();
            // Populate data directories if present.
            PopulateDataDirectoryForWellKnownSymbolIfPresent(dataDirs, ImageDirectoryEntry.Resource, SortableDependencyNode.ObjectNodeOrder.Win32ResourcesNode);
            PopulateDataDirectoryForWellKnownSymbolIfPresent(dataDirs, ImageDirectoryEntry.Debug, SortableDependencyNode.ObjectNodeOrder.DebugDirectoryNode);
            PopulateDataDirectoryForWellKnownSymbolIfPresent(dataDirs, ImageDirectoryEntry.CLRRuntimeHeader, SortableDependencyNode.ObjectNodeOrder.CorHeaderNode);
            PopulateDataDirectoryForWellKnownSymbolIfPresent(dataDirs, ImageDirectoryEntry.Exception, SortableDependencyNode.ObjectNodeOrder.RuntimeFunctionsTableNode);