public void DumpString() { ElfHDR elfhdr = header; ElfPHDR elfphdr; ElfSHDR elfshdr; // Initialze output file System.IO.BinaryWriter writer = new System.IO.BinaryWriter(System.IO.File.Create("dump.txt")); // Write Elf writer.Write(String.Format("****ELF HEADER****\n{0}\n", elfhdr.ToString())); // Write PHDR writer.Write("****PROGRAM HEADERS****\n"); for (int i = 0; i < elfhdr.ProgramHeaderCount; i++) { writer.Write(String.Format("Program[{0}]\n{1}\n", i, programs[i].Header.ToString())); } // WRITE SHDR writer.Write("****SECTION HEADERS****\n"); for (int i = 0; i < elfhdr.SectionHeaderCount; i++) { writer.Write(String.Format("Section[{0}]\n{1}\n", i, sections[i].Header.ToString())); } // Close writer writer.Close(); writer.Dispose(); }
public void Save(String filename) { // initialize stream FileStream stream = File.Create(filename); // Initialize Elf endianness ElfEndian endian = new ElfEndian(elf.Header.Encoding); // Save Elf header ElfHDR hdr = elf.Header; Byte[] hdrData = new Byte[hdr.DataSize]; elf.Header.Save(endian, ref hdrData); stream.Write(hdrData, 0, hdrData.Length); // Save programs Byte[] phdrData = new Byte[hdr.ProgramHeaderSize]; stream.Seek((Int64)hdr.ProgramHeaderOffset, SeekOrigin.Begin); for (int i = 0; i < hdr.ProgramHeaderCount; i++) { // Save program headers elf.Programs[i].Header.Save(endian, hdr.Class, ref phdrData); stream.Write(phdrData, 0, phdrData.Length); } // Save sections Byte[] shdrData = new Byte[hdr.SectionHeaderSize]; stream.Seek((Int64)hdr.SectionHeaderOffset, SeekOrigin.Begin); for (int i = 0; i < hdr.SectionHeaderCount; i++) { // Save section header ElfSHDR shdr = elf.Sections[i].Header; ElfSectionType type = shdr.Type; shdr.Save(endian, hdr.Class, ref shdrData); stream.Write(shdrData, 0, shdrData.Length); if (type != ElfSectionType.SHT_NULL && type != ElfSectionType.SHT_NOBITS) { Byte[] section = elf.Sections[i].Section; //stream.Write(elf.Sections[i].Section, 0, ; } } for (int i = 0; i < hdr.SectionHeaderCount; i++) { // Save section data ElfSection section = elf.Sections[i]; ElfSectionType type = section.Header.Type; if (type != ElfSectionType.SHT_NULL && type != ElfSectionType.SHT_NOBITS) { stream.Seek((Int64)section.Header.FileOffset, SeekOrigin.Begin); stream.Write(section.Section, 0, (int)section.Header.Size); } } // Close stream stream.Close(); }
public Elf(ElfHDR elfhdr, ElfProgram[] programs, ElfSection[] sections) { // Initialize elf header this.header = elfhdr; // Initialize program headers this.programs = new List <ElfProgram>(programs); // Initialize section headers this.sections = new List <ElfSection>(sections); }
public void DumpString() { ElfHDR elfhdr = header; ElfPHDR elfphdr; ElfSHDR elfshdr; // Initialze output file System.IO.BinaryWriter writer = new System.IO.BinaryWriter(System.IO.File.Create("dump.txt")); // Write Elf writer.Write("****ELF HEADER****\n"); writer.Write(String.Format("ID: {0}\nType: {1}\nMachine: {2}\nVersion: {3}\nEntryPoint: 0x{4:X8}\nPHOffset: 0x{5:X8}\nSHOffset: 0x{6:X8}\nFlags: 0x{7:X4}\nHeaderSize: 0x{8:X2}\nPHSize: 0x{9:X2}\nPHCount: 0x{10:X2}\nSHSize: 0x{11:X2}\nSHCount: 0x{12:X2}\nStringTable: 0x{13:X2}\n\n", BitConverter.ToString(elfhdr.Identification), elfhdr.Type.ToString(), elfhdr.MachineType.ToString(), elfhdr.Version.ToString(), elfhdr.EntryPoint, elfhdr.ProgramHeaderOffset, elfhdr.SectionHeaderOffset, elfhdr.Flags, elfhdr.HeaderSize, elfhdr.ProgramHeaderSize, elfhdr.ProgramHeaderCount, elfhdr.SectionHeaderSize, elfhdr.SectionHeaderCount, elfhdr.StringTable)); // Write PHDR writer.Write("****PROGRAM HEADERS****\n"); for (int i = 0; i < elfhdr.ProgramHeaderCount; i++) { elfphdr = programs[i].Header; writer.Write(String.Format("Program[{10}]\nType = {0}\nFlags = {1}\nOffset = 0x{2:X8} => 0x{8:X8} => 0x{9:X8}\nVaddr = 0x{3:X8}\nPaddr = 0x{4:X8}\nFilesz = 0x{5:X8}\nMemsz = 0x{6:X8}\nAlign = 0x{7:X8}\n\n", elfphdr.Type.ToString(), elfphdr.Flags.ToString(), elfphdr.FileOffset, elfphdr.VirtualAddress, elfphdr.PhysicalAddress, elfphdr.FileSize, elfphdr.MemorySize, elfphdr.Align, elfphdr.FileOffset + elfphdr.FileSize, (elfphdr.FileOffset + elfphdr.FileSize + elfphdr.Align - 1) & ~(elfphdr.Align - 1), i)); } // WRITE SHDR writer.Write("****SECTION HEADERS****\n"); for (int i = 0; i < elfhdr.SectionHeaderCount; i++) { elfshdr = sections[i].Header; writer.Write(String.Format("Section[{12}]\nName: 0x{0:X4}\nType: {1}\nFlags: {2}\nAddress: 0x{3:X8}\nOffset: 0x{4:X8} => 0x{10:X8} => 0x{11:X8}\nSize: 0x{5:X8}\nLink: 0x{6:X4}\nInfo: 0x{7:X4}\nAlign: 0x{8:X8}\nEntries: 0x{9:X8}\n\n", elfshdr.Name, elfshdr.Type.ToString(), elfshdr.Flags.ToString(), elfshdr.Address, elfshdr.FileOffset, elfshdr.Size, elfshdr.Link, elfshdr.Info, elfshdr.Align, elfshdr.EntrySize, elfshdr.FileOffset + elfshdr.Size, (elfshdr.FileOffset + elfshdr.Size + elfshdr.Align - 1) & ~(elfshdr.Align - 1), i)); } // Close writer writer.Close(); writer.Dispose(); }
public void Load(String filename) { // initialize stream FileStream stream = File.OpenRead(filename); // Read Elf ID ElfHDR elfhdr = new ElfHDR(); Byte[] elfid = new Byte[(int)ElfIdent.EI_NIDENT]; stream.Read(elfid, 0, elfid.Length); elfhdr.Identification = elfid; if (!elfhdr.IsElf) { throw new ArgumentException("Given file is not an elf"); } // Initialize Elf endianness ElfEndian endian = new ElfEndian(elfhdr.Encoding); // Read Elf header Byte[] elfhdrData = new Byte[elfhdr.DataSize]; stream.Seek(0, SeekOrigin.Begin); stream.Read(elfhdrData, 0, elfhdr.DataSize); elfhdr.Load(endian, elfhdrData); // Read programs ElfProgram[] elfprogs = new ElfProgram[elfhdr.ProgramHeaderCount]; Byte[] elfphdrData = new Byte[elfhdr.ProgramHeaderSize]; for (int i = 0; i < elfhdr.ProgramHeaderCount; i++) { ElfPHDR phdr = new ElfPHDR(elfhdr.Class); stream.Seek((Int64)elfhdr.ProgramHeaderOffset + i * elfhdr.ProgramHeaderSize, SeekOrigin.Begin); stream.Read(elfphdrData, 0, elfhdr.ProgramHeaderSize); phdr.Load(endian, elfhdr.Class, elfphdrData); elfprogs[i] = new ElfProgram(phdr); } // Read sections ElfSection[] elfsects = new ElfSection[elfhdr.SectionHeaderCount]; Byte[] elfshdrData = new Byte[elfhdr.SectionHeaderSize]; for (int i = 0; i < elfhdr.SectionHeaderCount; i++) { ElfSHDR shdr = new ElfSHDR(elfhdr.Class); stream.Seek((Int64)elfhdr.SectionHeaderOffset + i * elfhdr.SectionHeaderSize, SeekOrigin.Begin); stream.Read(elfshdrData, 0, elfhdr.SectionHeaderSize); shdr.Load(endian, elfhdr.Class, elfshdrData); Byte[] section = null; if (shdr.Type != ElfSectionType.SHT_NOBITS && shdr.Type != ElfSectionType.SHT_NULL) { section = new Byte[shdr.Size]; stream.Seek((Int64)shdr.FileOffset, SeekOrigin.Begin); stream.Read(section, 0, section.Length); } elfsects[i] = new ElfSection(shdr, section); } // Initialize Elf elf = new Elf(elfhdr, elfprogs, elfsects); // Close stream stream.Close(); }