Exemplo n.º 1
0
 public ElfEndian(ElfData elfdata)
 {
     if (elfdata == ElfData.ED_MSB)
     {
         if (BitConverter.IsLittleEndian)
         {
             // Reverse if elf is big endian and host little endian
             reverse = true;
         }
     }
     else
     {
         if (!BitConverter.IsLittleEndian)
         {
             // Reverse if elf is little endian and host big endian
             reverse = true;
         }
     }
 }
        public ElfEndian(ElfData elfdata)
        {
            reverse = elfdata == ElfData.ED_LSB ? !BitConverter.IsLittleEndian : elfdata == ElfData.ED_MSB ? BitConverter.IsLittleEndian : false;

            /*if (elfdata == ElfData.ED_MSB)
             * {
             *  if (BitConverter.IsLittleEndian)
             *  {
             *      // Reverse if elf is big endian and host little endian
             *      reverse = true;
             *  }
             * }
             * else
             * {
             *  if (!BitConverter.IsLittleEndian)
             *  {
             *      // Reverse if elf is little endian and host big endian
             *      reverse = true;
             *  }
             * }*/
        }
Exemplo n.º 3
0
        /// <summary>
        /// 用户获得精灵
        /// </summary>
        /// <returns></returns>
        public bool AddElf(int elfid, bool isExperience, long experienceTimeMin)
        {
            if (elfid == 0)
            {
                return(false);
            }

            var elf = ElfList.Find(t => (t.ID == elfid));

            if (elf != null)
            {
                if (!elf.IsExperience)
                {
                    return(false);
                }

                if (isExperience)
                {
                    elf.ExperienceTimeMin += experienceTimeMin;
                }
                else
                {
                    elf.IsExperience = false;
                }
                elf.IsNew = true;
                return(true);
            }
            elf                   = new ElfData();
            elf.ID                = elfid;
            elf.Lv                = 1;
            elf.IsNew             = true;
            elf.IsExperience      = isExperience;
            elf.ExperienceTimeMin = experienceTimeMin;
            ElfList.Add(elf);
            return(true);
        }
Exemplo n.º 4
0
 public override bool TakeAction()
 {
     receipt = GetElf.FindElf(_elfId);
     return(true);
 }
Exemplo n.º 5
0
        protected override void Read(System.IO.BinaryReader r)
        {
            Init();

            // Read e_ident
            byte[] e_ident = r.ReadBytes(16);
            if ((e_ident[0] != 0x7f) || (e_ident[1] != (byte)'E') || (e_ident[2] != (byte)'L') || (e_ident[3] != (byte)'F'))
                throw new Exception("Not an ELF file");
            ec = (ElfClass)e_ident[4];
            ed = (ElfData)e_ident[5];

            if ((ec != ElfClass.ELFCLASS32) && (ec != ElfClass.ELFCLASS64))
                throw new Exception("Invalid ELF class: " + e_ident[4].ToString());
            if (ed != ElfData.ELFDATA2LSB)
                throw new Exception("Invalid ELF data type: " + e_ident[5].ToString());

            if (e_ident[6] != 1)
                throw new Exception("Invalid ELF version: " + e_ident[6].ToString());

            // Read the rest of the file header
            switch (ec)
            {
                case ElfClass.ELFCLASS32:
                    ReadElf32FileHeader(r);
                    break;

                case ElfClass.ELFCLASS64:
                    ReadElf64FileHeader(r);
                    break;
            }

            // Identify the arch, OS and machine type
            os = "none";
            binary_type = BinaryTypes[ec];
            architecture = MachineTypes[e_machine];

            // First iterate through and identify the offsets of each section
            List<long> sect_offsets = new List<long>();
            for (int i = 0; i < e_shnum; i++)
            {
                long sh_start = e_shoff + i * e_shentsize;
                r.BaseStream.Seek(sh_start, System.IO.SeekOrigin.Begin);

                switch (ec)
                {
                    case ElfClass.ELFCLASS32:
                        r.BaseStream.Seek(16, System.IO.SeekOrigin.Current);
                        sect_offsets.Add(r.ReadInt32());
                        break;
                    case ElfClass.ELFCLASS64:
                        r.BaseStream.Seek(24, System.IO.SeekOrigin.Current);
                        sect_offsets.Add(r.ReadInt64());
                        break;
                }
            }

            // Now load the section data
            List<SectionHeader> sect_headers = new List<SectionHeader>();
            for (int i = 0; i < e_shnum; i++)
            {
                // First load the section header
                long sh_start = e_shoff + i * e_shentsize;
                r.BaseStream.Seek(sh_start, System.IO.SeekOrigin.Begin);
                SectionHeader sh = null;
                switch (ec)
                {
                    case ElfClass.ELFCLASS32:
                        sh = ReadElf32SectionHeader(r);
                        break;
                    case ElfClass.ELFCLASS64:
                        sh = ReadElf64SectionHeader(r);
                        break;
                }
                sect_headers.Add(sh);

                // Now get the name
                string name = "unknown";
                if (e_shstrndx != 0)
                {
                    long name_offset = sect_offsets[e_shstrndx] +
                        sh.sh_name;
                    name = ReadString(r, name_offset);
                }

                // Now load the actual section
                // Decide on the type of section
                ISection sect = null;
                switch (sh.sh_type)
                {
                    case SectionHeader.SHT_NULL:
                        break;
                    case SectionHeader.SHT_PROGBITS:
                    case SectionHeader.SHT_REL:
                    case SectionHeader.SHT_RELA:
                    case SectionHeader.SHT_HASH:
                    case SectionHeader.SHT_DYNAMIC:
                    case SectionHeader.SHT_NOTE:
                    case SectionHeader.SHT_STRTAB:
                        sect = new ContentsSection(this);
                        break;
                    case SectionHeader.SHT_SYMTAB:
                    case SectionHeader.SHT_DYNSYM:
                        sect = new ElfSymbolSection(this);
                        break;
                    case SectionHeader.SHT_NOBITS:
                        sect = new BssSection(this);
                        break;
                }

                if (sect != null)
                {
                    // Interpret the section type
                    sect.IsWriteable = ((sh.sh_flags & SectionHeader.SHF_WRITE) != 0);
                    sect.IsAlloc = ((sh.sh_flags & SectionHeader.SHF_ALLOC) != 0);
                    sect.IsExecutable = ((sh.sh_flags & SectionHeader.SHF_EXECINSTR) != 0);

                    sect.LoadAddress = sh.sh_addr;
                    sect.Name = name;
                    sect.AddrAlign = sh.sh_addralign;

                    // Load the contents if it has any
                    sect.Length = sh.sh_size;
                    if (sect.HasData)
                    {
                        r.BaseStream.Seek(sh.sh_offset, System.IO.SeekOrigin.Begin);
                        for (long l = 0; l < sh.sh_size; l++)
                            sect.Data[(int)l] = r.ReadByte();
                    }


                }
                sections.Add(sect);
            }

            // Interpret symbols
            for (int i = 0; i < e_shnum; i++)
            {
                SectionHeader sh = sect_headers[i];
                ISection sect = sections[i];

                if ((sh.sh_type == SectionHeader.SHT_SYMTAB) && sect.HasData)
                {
                    int cur_sym = 0;
                    for (long p = 0; p < sect.Length; p += sh.sh_entsize)
                    {
                        ElfSymbol s = null;

                        switch (ec)
                        {
                            case ElfClass.ELFCLASS32:
                                s = ReadElf32Symbol(sect.Data, (int)p);
                                break;
                            case ElfClass.ELFCLASS64:
                                s = ReadElf64Symbol(sect.Data, (int)p);
                                break;
                        }

                        if (s != null)
                        {
                            // Load up the name of the symbol
                            if (sh.sh_link != 0)
                                s.Name = ReadString(sections[sh.sh_link].Data, s.st_name);

                            // Offset
                            s.Offset = s.st_value;

                            // Length
                            s.Size = s.st_size;

                            // Type
                            switch (s.st_bind)
                            {
                                case ElfSymbol.STB_GLOBAL:
                                    s.Type = SymbolType.Global;
                                    break;
                                case ElfSymbol.STB_LOCAL:
                                    s.Type = SymbolType.Local;
                                    break;
                                case ElfSymbol.STB_WEAK:
                                    s.Type = SymbolType.Weak;
                                    break;
                            }
                            switch(s.st_type)
                            {
                                case ElfSymbol.STT_FUNC:
                                    s.ObjectType = SymbolObjectType.Function;
                                    break;
                                case ElfSymbol.STT_OBJECT:
                                    s.ObjectType = SymbolObjectType.Object;
                                    break;
                                default:
                                    s.ObjectType = SymbolObjectType.Unknown;
                                    break;                                    
                            }

                            // DefinedIn
                            if (s.st_shndx == 0)
                            {
                                s.DefinedIn = null;
                                s.Type = SymbolType.Undefined;
                            }
                            else if (s.st_shndx == -15)
                            {
                                // SHN_ABS
                                s.DefinedIn = AbsSection;
                            }
                            else if (s.st_shndx == -14)
                            {
                                // SHN_COMMON
                                s.DefinedIn = CommonSection;
                            }
                            else
                                s.DefinedIn = sections[s.st_shndx];
                        }

                        if(!symbols.Contains(s))
                            symbols.Add(s);
                        ((ElfSymbolSection)sect).elf_syms[cur_sym++] = s;
                    }
                }
            }

            // Interpret relocations
            for (int i = 0; i < e_shnum; i++)
            {
                SectionHeader sh = sect_headers[i];
                ISection sect = sections[i];

                switch (sh.sh_type)
                {
                    case SectionHeader.SHT_REL:
                        ReadRelocationSection(sh, sect, sections, false);
                        break;
                    case SectionHeader.SHT_RELA:
                        ReadRelocationSection(sh, sect, sections, true);
                        break;
                }
            }
        }