// dnlib: See LICENSE.txt for more info using System.Text; using dnlib.PE; namespace dnlib.DotNet.Writer { /// /// A PE section /// public sealed class PESection : ChunkList { string name; uint characteristics; /// /// Gets the name /// public string Name { get => name; set => name = value; } /// /// Gets the Characteristics /// public uint Characteristics { get => characteristics; set => characteristics = value; } /// /// true if this is a code section /// public bool IsCode => (characteristics & 0x20) != 0; /// /// true if this is an initialized data section /// public bool IsInitializedData => (characteristics & 0x40) != 0; /// /// true if this is an uninitialized data section /// public bool IsUninitializedData => (characteristics & 0x80) != 0; /// /// Constructor /// /// Section name /// Section characteristics public PESection(string name, uint characteristics) { this.name = name; this.characteristics = characteristics; } /// /// Writes the section header to at its current position. /// Returns aligned virtual size (aligned to ) /// /// Writer /// File alignment /// Section alignment /// Current public uint WriteHeaderTo(DataWriter writer, uint fileAlignment, uint sectionAlignment, uint rva) { uint vs = GetVirtualSize(); uint fileLen = GetFileLength(); uint alignedVs = Utils.AlignUp(vs, sectionAlignment); uint rawSize = Utils.AlignUp(fileLen, fileAlignment); uint dataOffset = (uint)FileOffset; writer.WriteBytes(Encoding.UTF8.GetBytes(Name + "\0\0\0\0\0\0\0\0"), 0, 8); writer.WriteUInt32(vs); // VirtualSize writer.WriteUInt32(rva); // VirtualAddress writer.WriteUInt32(rawSize); // SizeOfRawData writer.WriteUInt32(dataOffset); // PointerToRawData writer.WriteInt32(0); // PointerToRelocations writer.WriteInt32(0); // PointerToLinenumbers writer.WriteUInt16(0); // NumberOfRelocations writer.WriteUInt16(0); // NumberOfLinenumbers writer.WriteUInt32(Characteristics); return alignedVs; } } }