From 93329b1724d28d5f4d7f7e3c660ea973a48c1075 Mon Sep 17 00:00:00 2001 From: Muhammad Rizwan Date: Wed, 25 Oct 2023 08:34:25 +0300 Subject: [PATCH] Fix: ELF File --- SharpRISCV.Core/Elf/Compile.cs | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/SharpRISCV.Core/Elf/Compile.cs b/SharpRISCV.Core/Elf/Compile.cs index 04fb453..b037724 100644 --- a/SharpRISCV.Core/Elf/Compile.cs +++ b/SharpRISCV.Core/Elf/Compile.cs @@ -44,7 +44,7 @@ public List bytes() List dataSectBytes = new List(); dataSectBytes.AddRange(DataSection.DataDirective); - var entryPoint = 0x40 + 0x38 + Address.EntryPoint; + var entryPoint = Address.EntryPoint; finalBytes.AddRange(new byte[] { 0x7F, (byte)'E', (byte)'L', (byte)'F' }); finalBytes.Add(2); // class (64-bit) @@ -54,30 +54,41 @@ public List bytes() finalBytes.AddRange(new byte[] { 2, 0 }); // type (executable) finalBytes.AddRange(new byte[] { 0xF3, 0 }); // machine (RISC-V) finalBytes.AddRange(new byte[] { 1, 0, 0, 0 }); // version - finalBytes.AddRange(BitConverter.GetBytes((Int64)entryPoint)); // entry point + finalBytes.AddRange(BitConverter.GetBytes((ulong)(0x10000 + 0x40 + 0x38+ 0x38 + entryPoint))); // entry point finalBytes.AddRange(BitConverter.GetBytes((Int64)0x40)); // program header offset finalBytes.AddRange(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }); // section header offset - finalBytes.AddRange(new byte[] { 0, 0, 0, 0 }); // flags + finalBytes.AddRange(new byte[] { 0x4, 0, 0, 0 }); // flags finalBytes.AddRange(BitConverter.GetBytes((Int16)0x40)); // ELF header size finalBytes.AddRange(BitConverter.GetBytes((Int16)0x38)); // program header size - finalBytes.AddRange(BitConverter.GetBytes((Int16)1)); // program header entry size + finalBytes.AddRange(BitConverter.GetBytes((Int16)2)); // program header entry size finalBytes.AddRange(new byte[] { 0, 0 }); // section header size finalBytes.AddRange(new byte[] { 0, 0 }); // section header entry size finalBytes.AddRange(new byte[] { 0, 0 }); // section header string index - var size = 96 + sizeof(byte) * opcodes.Count + sizeof(byte) * dataSectBytes.Count; + var size = 0x40 + 0x38 + 0x38 + opcodes.Count; //// Program header finalBytes.AddRange(new byte[] { 1, 0, 0, 0 }); // type (load) - finalBytes.AddRange(new byte[] { 0, 0, 0, 0 }); // offset - finalBytes.AddRange(BitConverter.GetBytes((Int64)0x40)); // virtual address - finalBytes.AddRange(BitConverter.GetBytes((Int64)entryPoint)); // virtual address - finalBytes.AddRange(BitConverter.GetBytes((Int64)entryPoint)); // physical address + finalBytes.AddRange(new byte[] { 0x5, 0, 0, 0 }); // offset + finalBytes.AddRange(BitConverter.GetBytes((Int64)0x00000)); // Virtual address of the segment in memory. + finalBytes.AddRange(BitConverter.GetBytes((Int64)0x10000)); // virtual address + finalBytes.AddRange(BitConverter.GetBytes((Int64)0x10000 + entryPoint)); // physical address finalBytes.AddRange(BitConverter.GetBytes((Int64)size)); // file size finalBytes.AddRange(BitConverter.GetBytes((Int64)size)); // memory size - finalBytes.AddRange(new byte[] { 5, 0, 0, 0 }); // flags (execute and read) - finalBytes.AddRange(new byte[] { 0, 0, 0, 0, 0, 0,0,0 }); // alignment + finalBytes.AddRange(new byte[] { 0, 0x10, 0, 0 }); // flags (execute and read) + finalBytes.AddRange(new byte[] { 00,00,00,00 }); // alignment + + //// Program header + finalBytes.AddRange(new byte[] { 1, 0, 0, 0 }); // type (load) + finalBytes.AddRange(new byte[] { 06, 0, 0, 0 }); // offset + finalBytes.AddRange(BitConverter.GetBytes((Int64)0x00000 + size)); + finalBytes.AddRange(BitConverter.GetBytes((Int64)0x11000 + size)); // virtual address + finalBytes.AddRange(BitConverter.GetBytes((Int64)0x11000 + size)); // physical address + finalBytes.AddRange(BitConverter.GetBytes((Int64)dataSectBytes.Count)); // file size + finalBytes.AddRange(BitConverter.GetBytes((Int64)dataSectBytes.Count)); // memory size + finalBytes.AddRange(new byte[] { 0, 0x10, 0, 0 }); // flags (execute and read) + finalBytes.AddRange(new byte[] { 00, 00, 00, 00}); // alignment finalBytes.AddRange(opcodes);