Beispiel #1
0
 public void Load(ElfEndian endian, ElfClass elfclass, Byte[] data)
 {
     if (elfclass == ElfClass.EC_32)
     {
         sh_name      = endian.Convert(BitConverter.ToUInt32(data, 0));
         sh_type      = endian.Convert(BitConverter.ToUInt32(data, 4));
         sh_flags     = endian.Convert(BitConverter.ToUInt32(data, 8));
         sh_addr      = endian.Convert(BitConverter.ToUInt32(data, 0xC));
         sh_offset    = endian.Convert(BitConverter.ToUInt32(data, 0x10));
         sh_size      = endian.Convert(BitConverter.ToUInt32(data, 0x14));
         sh_link      = endian.Convert(BitConverter.ToUInt32(data, 0x18));
         sh_info      = endian.Convert(BitConverter.ToUInt32(data, 0x1C));
         sh_addralign = endian.Convert(BitConverter.ToUInt32(data, 0x20));
         sh_entsize   = endian.Convert(BitConverter.ToUInt32(data, 0x24));
     }
     else
     {
         sh_name      = endian.Convert(BitConverter.ToUInt32(data, 0));
         sh_type      = endian.Convert(BitConverter.ToUInt32(data, 4));
         sh_flags     = endian.Convert(BitConverter.ToUInt64(data, 8));
         sh_addr      = endian.Convert(BitConverter.ToUInt64(data, 0x10));
         sh_offset    = endian.Convert(BitConverter.ToUInt64(data, 0x18));
         sh_size      = endian.Convert(BitConverter.ToUInt64(data, 0x20));
         sh_link      = endian.Convert(BitConverter.ToUInt32(data, 0x28));
         sh_info      = endian.Convert(BitConverter.ToUInt32(data, 0x2C));
         sh_addralign = endian.Convert(BitConverter.ToUInt64(data, 0x30));
         sh_entsize   = endian.Convert(BitConverter.ToUInt64(data, 0x38));
     }
 }
Beispiel #2
0
 public void Load(ElfEndian endian, ElfClass elfclass, Byte[] data)
 {
     if (elfclass == ElfClass.EC_32)
     {
         p_type   = endian.Convert(BitConverter.ToUInt32(data, 0));
         p_offset = endian.Convert(BitConverter.ToUInt32(data, 4));
         p_vaddr  = endian.Convert(BitConverter.ToUInt32(data, 8));
         p_paddr  = endian.Convert(BitConverter.ToUInt32(data, 0xC));
         p_filesz = endian.Convert(BitConverter.ToUInt32(data, 0x10));
         p_memsz  = endian.Convert(BitConverter.ToUInt32(data, 0x14));
         p_flags  = endian.Convert(BitConverter.ToUInt32(data, 0x18));
         p_align  = endian.Convert(BitConverter.ToUInt32(data, 0x1C));
     }
     else
     {
         p_type   = endian.Convert(BitConverter.ToUInt32(data, 0));
         p_flags  = endian.Convert(BitConverter.ToUInt32(data, 4));
         p_offset = endian.Convert(BitConverter.ToUInt64(data, 8));
         p_vaddr  = endian.Convert(BitConverter.ToUInt64(data, 0x10));
         p_paddr  = endian.Convert(BitConverter.ToUInt64(data, 0x18));
         p_filesz = endian.Convert(BitConverter.ToUInt64(data, 0x20));
         p_memsz  = endian.Convert(BitConverter.ToUInt64(data, 0x28));
         p_align  = endian.Convert(BitConverter.ToUInt64(data, 0x30));
     }
 }
Beispiel #3
0
 public void Save(ElfEndian endian, ElfClass elfclass, ref Byte[] data)
 {
     if (elfclass == ElfClass.EC_32)
     {
         Array.Copy(BitConverter.GetBytes(endian.Convert(p_type)), 0, data, 0, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert((UInt32)p_offset)), 0, data, 4, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert((UInt32)p_vaddr)), 0, data, 8, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert((UInt32)p_paddr)), 0, data, 0xC, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert((UInt32)p_filesz)), 0, data, 0x10, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert((UInt32)p_memsz)), 0, data, 0x14, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert(p_flags)), 0, data, 0x18, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert((UInt32)p_align)), 0, data, 0x1C, 4);
     }
     else
     {
         Array.Copy(BitConverter.GetBytes(endian.Convert(p_type)), 0, data, 0, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert(p_flags)), 0, data, 4, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert(p_offset)), 0, data, 8, 8);
         Array.Copy(BitConverter.GetBytes(endian.Convert(p_vaddr)), 0, data, 0x10, 8);
         Array.Copy(BitConverter.GetBytes(endian.Convert(p_paddr)), 0, data, 0x18, 8);
         Array.Copy(BitConverter.GetBytes(endian.Convert(p_filesz)), 0, data, 0x20, 8);
         Array.Copy(BitConverter.GetBytes(endian.Convert(p_memsz)), 0, data, 0x28, 8);
         Array.Copy(BitConverter.GetBytes(endian.Convert(p_align)), 0, data, 0x30, 8);
     }
 }
Beispiel #4
0
 public void Load(ElfEndian endian, Byte[] data)
 {
     if (Class == ElfClass.EC_32)
     {
         e_type      = endian.Convert(BitConverter.ToUInt16(data, 0x10));
         e_machine   = endian.Convert(BitConverter.ToUInt16(data, 0x12));
         e_version   = endian.Convert(BitConverter.ToUInt32(data, 0x14));
         e_entry     = endian.Convert(BitConverter.ToUInt32(data, 0x18));
         e_phoff     = endian.Convert(BitConverter.ToUInt32(data, 0x1C));
         e_shoff     = endian.Convert(BitConverter.ToUInt32(data, 0x20));
         e_flags     = endian.Convert(BitConverter.ToUInt32(data, 0x24));
         e_ehsize    = endian.Convert(BitConverter.ToUInt16(data, 0x28));
         e_phentsize = endian.Convert(BitConverter.ToUInt16(data, 0x2A));
         e_phnum     = endian.Convert(BitConverter.ToUInt16(data, 0x2C));
         e_shentsize = endian.Convert(BitConverter.ToUInt16(data, 0x2E));
         e_shnum     = endian.Convert(BitConverter.ToUInt16(data, 0x30));
         e_shstrndx  = endian.Convert(BitConverter.ToUInt16(data, 0x32));
     }
     else
     {
         e_type      = endian.Convert(BitConverter.ToUInt16(data, 0x10));
         e_machine   = endian.Convert(BitConverter.ToUInt16(data, 0x12));
         e_version   = endian.Convert(BitConverter.ToUInt32(data, 0x14));
         e_entry     = endian.Convert(BitConverter.ToUInt64(data, 0x18));
         e_phoff     = endian.Convert(BitConverter.ToUInt64(data, 0x20));
         e_shoff     = endian.Convert(BitConverter.ToUInt64(data, 0x28));
         e_flags     = endian.Convert(BitConverter.ToUInt32(data, 0x30));
         e_ehsize    = endian.Convert(BitConverter.ToUInt16(data, 0x34));
         e_phentsize = endian.Convert(BitConverter.ToUInt16(data, 0x36));
         e_phnum     = endian.Convert(BitConverter.ToUInt16(data, 0x38));
         e_shentsize = endian.Convert(BitConverter.ToUInt16(data, 0x3A));
         e_shnum     = endian.Convert(BitConverter.ToUInt16(data, 0x3C));
         e_shstrndx  = endian.Convert(BitConverter.ToUInt16(data, 0x3E));
     }
 }
Beispiel #5
0
 public void Save(ElfEndian endian, ElfClass elfclass, ref Byte[] data)
 {
     if (elfclass == ElfClass.EC_32)
     {
         Array.Copy(BitConverter.GetBytes(endian.Convert(sh_name)), 0, data, 0, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert(sh_type)), 0, data, 4, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert((UInt32)sh_flags)), 0, data, 8, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert((UInt32)sh_addr)), 0, data, 0xC, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert((UInt32)sh_offset)), 0, data, 0x10, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert((UInt32)sh_size)), 0, data, 0x14, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert(sh_link)), 0, data, 0x18, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert(sh_info)), 0, data, 0x1C, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert((UInt32)sh_addralign)), 0, data, 0x20, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert((UInt32)sh_entsize)), 0, data, 0x24, 4);
     }
     else
     {
         Array.Copy(BitConverter.GetBytes(endian.Convert(sh_name)), 0, data, 0, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert(sh_type)), 0, data, 4, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert(sh_flags)), 0, data, 8, 8);
         Array.Copy(BitConverter.GetBytes(endian.Convert(sh_addr)), 0, data, 0x10, 8);
         Array.Copy(BitConverter.GetBytes(endian.Convert(sh_offset)), 0, data, 0x18, 8);
         Array.Copy(BitConverter.GetBytes(endian.Convert(sh_size)), 0, data, 0x20, 8);
         Array.Copy(BitConverter.GetBytes(endian.Convert(sh_link)), 0, data, 0x28, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert(sh_info)), 0, data, 0x2C, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert(sh_addralign)), 0, data, 0x30, 8);
         Array.Copy(BitConverter.GetBytes(endian.Convert(sh_entsize)), 0, data, 0x38, 8);
     }
 }
        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();
        }
Beispiel #7
0
 public void Save(ElfEndian endian, ref Byte[] data)
 {
     Array.Copy(e_ident, 0, data, 0, (int)ElfIdent.EI_NIDENT);
     if (Class == ElfClass.EC_32)
     {
         Array.Copy(BitConverter.GetBytes(endian.Convert(e_type)), 0, data, 0x10, 2);
         Array.Copy(BitConverter.GetBytes(endian.Convert(e_machine)), 0, data, 0x12, 2);
         Array.Copy(BitConverter.GetBytes(endian.Convert(e_version)), 0, data, 0x14, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert((UInt32)e_entry)), 0, data, 0x18, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert((UInt32)e_phoff)), 0, data, 0x1C, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert((UInt32)e_shoff)), 0, data, 0x20, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert(e_flags)), 0, data, 0x24, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert(e_ehsize)), 0, data, 0x28, 2);
         Array.Copy(BitConverter.GetBytes(endian.Convert(e_phentsize)), 0, data, 0x2A, 2);
         Array.Copy(BitConverter.GetBytes(endian.Convert(e_phnum)), 0, data, 0x2C, 2);
         Array.Copy(BitConverter.GetBytes(endian.Convert(e_shentsize)), 0, data, 0x2E, 2);
         Array.Copy(BitConverter.GetBytes(endian.Convert(e_shnum)), 0, data, 0x30, 2);
         Array.Copy(BitConverter.GetBytes(endian.Convert(e_shstrndx)), 0, data, 0x32, 2);
     }
     else
     {
         Array.Copy(BitConverter.GetBytes(endian.Convert(e_type)), 0, data, 0x10, 2);
         Array.Copy(BitConverter.GetBytes(endian.Convert(e_machine)), 0, data, 0x12, 2);
         Array.Copy(BitConverter.GetBytes(endian.Convert(e_version)), 0, data, 0x14, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert(e_entry)), 0, data, 0x18, 8);
         Array.Copy(BitConverter.GetBytes(endian.Convert(e_phoff)), 0, data, 0x20, 8);
         Array.Copy(BitConverter.GetBytes(endian.Convert(e_shoff)), 0, data, 0x28, 8);
         Array.Copy(BitConverter.GetBytes(endian.Convert(e_flags)), 0, data, 0x30, 4);
         Array.Copy(BitConverter.GetBytes(endian.Convert(e_ehsize)), 0, data, 0x34, 2);
         Array.Copy(BitConverter.GetBytes(endian.Convert(e_phentsize)), 0, data, 0x36, 2);
         Array.Copy(BitConverter.GetBytes(endian.Convert(e_phnum)), 0, data, 0x38, 2);
         Array.Copy(BitConverter.GetBytes(endian.Convert(e_shentsize)), 0, data, 0x3A, 2);
         Array.Copy(BitConverter.GetBytes(endian.Convert(e_shnum)), 0, data, 0x3C, 2);
         Array.Copy(BitConverter.GetBytes(endian.Convert(e_shstrndx)), 0, data, 0x3E, 2);
     }
 }
        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();
        }