From 84c9d6e852ad10fedcf36da85b79258c7035bfe5 Mon Sep 17 00:00:00 2001 From: Mark Topley-Drake Date: Mon, 21 Jul 2025 15:41:22 +0100 Subject: [PATCH] Fixes to ElfFile to be able to save a core file. --- src/LibObjectFile/Elf/ElfFile.cs | 4 ++-- src/LibObjectFile/Elf/ElfProgramHeaderTable.cs | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/LibObjectFile/Elf/ElfFile.cs b/src/LibObjectFile/Elf/ElfFile.cs index 98bc339..9d435f0 100644 --- a/src/LibObjectFile/Elf/ElfFile.cs +++ b/src/LibObjectFile/Elf/ElfFile.cs @@ -33,14 +33,14 @@ public sealed partial class ElfFile : ElfObject, IEnumerable /// /// Creates a new instance with the default sections (null and a shadow program header table). /// - public ElfFile(ElfArch arch) : this(arch, ElfFileClass.None, ElfEncoding.None) + public ElfFile(ElfArch arch, bool addDefaultSections=true) : this(arch, ElfFileClass.None, ElfEncoding.None, addDefaultSections) { } /// /// Creates a new instance with the default sections (null and a shadow program header table). /// - public ElfFile(ElfArch arch, ElfFileClass fileClass, ElfEncoding encoding) : this(true) + public ElfFile(ElfArch arch, ElfFileClass fileClass, ElfEncoding encoding,bool addDefaultSections=true) : this(addDefaultSections) { Arch = arch; switch (arch) diff --git a/src/LibObjectFile/Elf/ElfProgramHeaderTable.cs b/src/LibObjectFile/Elf/ElfProgramHeaderTable.cs index cd88c80..24a7c95 100644 --- a/src/LibObjectFile/Elf/ElfProgramHeaderTable.cs +++ b/src/LibObjectFile/Elf/ElfProgramHeaderTable.cs @@ -59,6 +59,11 @@ public override void Verify(ElfVisitorContext context) protected override unsafe void UpdateLayoutCore(ElfVisitorContext context) { + var elf = Parent; + if (elf != null) + { + _is32 = elf.FileClass == ElfFileClass.Is32; + } Size = (ulong)(Parent!.Segments.Count * (AdditionalEntrySize + (_is32 ? sizeof(ElfNative.Elf32_Phdr) : sizeof(ElfNative.Elf64_Phdr)))); }