Beispiel #1
0
 public ElfProgram(ElfPHDR header)
 {
     this.header = header;
 }
        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();
        }