示例#1
0
        public List <ProgramResource> ReadNameDirectory(EndianImageReader rdr, uint resourceType)
        {
            const uint DIR_MASK     = 0x80000000;
            var        flags        = rdr.ReadUInt32();
            var        date         = rdr.ReadUInt32();
            var        version      = rdr.ReadUInt32();
            var        cNameEntries = rdr.ReadUInt16();
            var        cIdEntries   = rdr.ReadUInt16();
            var        entries      = new List <ProgramResource>();

            for (int i = 0; i < cNameEntries; ++i)
            {
                var rvaName  = rdr.ReadUInt32();
                var rvaEntry = rdr.ReadUInt32();
                var subRdr   = new LeImageReader(imgLoaded, rvaResources + (rvaEntry & ~DIR_MASK));
                if ((rvaEntry & DIR_MASK) == 0)
                {
                    throw new BadImageFormatException();
                }
                var e = new ProgramResourceGroup
                {
                    Name = ReadResourceUtf16leString(rvaResources + (rvaName & ~DIR_MASK)),
                };
                e.Resources.AddRange(ReadLanguageDirectory(subRdr, resourceType, e.Name));
                entries.Add(e);
            }
            for (int i = 0; i < cIdEntries; ++i)
            {
                var id       = rdr.ReadUInt32();
                var rvaEntry = rdr.ReadUInt32();
                var subRdr   = new LeImageReader(imgLoaded, rvaResources + (rvaEntry & ~DIR_MASK));
                if ((rvaEntry & DIR_MASK) == 0)
                {
                    throw new BadImageFormatException();
                }
                var e = new ProgramResourceGroup
                {
                    Name = id.ToString(),
                };
                e.Resources.AddRange(ReadLanguageDirectory(subRdr, resourceType, e.Name));
                entries.Add(e);
            }
            return(entries);
        }
示例#2
0
        public static Elf64_Sym Load(EndianImageReader rdr)
        {
            var sym = new Elf64_Sym();

            sym.st_name  = rdr.ReadUInt32();
            sym.st_info  = rdr.ReadByte();
            sym.st_other = rdr.ReadByte();
            sym.st_shndx = rdr.ReadUInt16();
            sym.st_value = rdr.ReadUInt64();
            sym.st_size  = rdr.ReadUInt64();
            return(sym);
        }
示例#3
0
        public void Sift_16Bit()
        {
            var writer    = arch.CreateImageWriter(mem, mem.BaseAddress);
            int iLastByte = 0;
            int lastLen   = 0;

            while (iLastByte >= 0 && DecrementCount())
            {
                var instr = Dasm();
                processInstr(mem.Bytes, instr);

                if (rdr.Offset != lastLen)
                {
                    iLastByte = (int)rdr.Offset - 1;
                    lastLen   = (int)rdr.Offset;
                }
                if (iLastByte >= maxInstrLength)
                {
                    iLastByte = maxInstrLength - 1;
                }
                rdr.Offset = iLastByte & ~1;
                var val = rdr.ReadUInt16() + 1u;
                while (val >= 0x10000)
                {
                    writer.Position = iLastByte & ~1;
                    writer.WriteUInt16(0);
                    iLastByte -= 2;
                    if (iLastByte < 0)
                    {
                        return;
                    }
                    rdr.Offset = iLastByte & ~1;
                    val        = rdr.ReadUInt16() + 1u;
                }
                writer.Position = iLastByte & ~1;
                writer.WriteUInt16((ushort)val);
            }
        }
示例#4
0
        public List <ProgramResource> ReadLanguageDirectory(
            EndianImageReader rdr,
            ResourceType resourceType,
            string resourceId)
        {
            const uint DIR_MASK     = 0x80000000;
            var        flags        = rdr.ReadUInt32();
            var        date         = rdr.ReadUInt32();
            var        version      = rdr.ReadUInt32();
            var        cNameEntries = rdr.ReadUInt16();
            var        cIdEntries   = rdr.ReadUInt16();
            var        entries      = new List <ProgramResource>();

            for (int i = 0; i < cNameEntries; ++i)
            {
                var rvaName  = rdr.ReadUInt32();
                var rvaEntry = rdr.ReadUInt32();
                var subRdr   = new LeImageReader(imgLoaded, rvaResources + (rvaEntry & ~DIR_MASK));
                if ((rvaEntry & DIR_MASK) != 0)
                {
                    throw new BadImageFormatException();
                }
                entries.Add(ReadResourceEntry(subRdr, resourceId, ReadResourceString(rvaName), resourceType));
            }
            for (int i = 0; i < cIdEntries; ++i)
            {
                var id       = rdr.ReadInt32();
                var rvaEntry = rdr.ReadUInt32();
                var subRdr   = new LeImageReader(imgLoaded, rvaResources + (rvaEntry & ~DIR_MASK));
                if ((rvaEntry & DIR_MASK) != 0)
                {
                    throw new BadImageFormatException();
                }
                entries.Add(ReadResourceEntry(subRdr, resourceId, id.ToString(), resourceType));
            }
            return(entries);
        }
示例#5
0
        public ushort e_shstrndx;       // section name string table index

        public static Elf32_EHdr Load(EndianImageReader rdr)
        {
            return(new Elf32_EHdr
            {
                e_type = rdr.ReadUInt16(),
                e_machine = rdr.ReadUInt16(),
                e_version = rdr.ReadUInt32(),
                e_entry = rdr.ReadUInt32(),
                e_phoff = rdr.ReadUInt32(),
                e_shoff = rdr.ReadUInt32(),
                e_flags = rdr.ReadUInt32(),
                e_ehsize = rdr.ReadUInt16(),
                e_phentsize = rdr.ReadUInt16(),
                e_phnum = rdr.ReadUInt16(),
                e_shentsize = rdr.ReadUInt16(),
                e_shnum = rdr.ReadUInt16(),
                e_shstrndx = rdr.ReadUInt16(),
            });
        }
示例#6
0
        public const short IMAGE_REL_MIPS_PAIR      = 0x0025; // This relocation is only valid when it immediately follows a REFHI or SECRELHI relocation. Its SymbolTableIndex contains a displacement and not an index into the symbol table.

        public override void ApplyRelocation(Address baseOfImage, uint page, EndianImageReader rdr, RelocationDictionary relocations)
        {
            ushort  fixup  = rdr.ReadUInt16();
            Address offset = baseOfImage + page + (fixup & 0x0FFFu);
            var     arch   = program.Architecture;
            var     imgR   = program.CreateImageReader(arch, offset);
            var     imgW   = program.CreateImageWriter(arch, offset);
            uint    w      = imgR.ReadUInt32();
            int     s;

            switch (fixup >> 12)
            {
            case IMAGE_REL_MIPS_ABSOLUTE:
                // Used for padding to 4-byte boundary, ignore.
                break;

            case IMAGE_REL_MIPS_REFWORD:
                break;

            case IMAGE_REL_MIPS_JMPADDR:
                break;

            case IMAGE_REL_MIPS_REFHI:
                w = imgR.ReadUInt32();
                //w += (fixup & 0x0FFFu);
                //imgW.WriteUInt32(w);
                s = rdr.ReadInt16();
                w = (uint)(w + s);
                // w points to something.
                break;

            case IMAGE_REL_MIPS_REFLO:
                // w points to something.
                break;

            default:
                dcSvc.Warn(
                    dcSvc.CreateAddressNavigator(program, offset),
                    string.Format(
                        "Unsupported MIPS PE fixup type: {0:X}",
                        fixup >> 12));
                break;
            }
        }
示例#7
0
        public void VisitPrimitive(PrimitiveType pt)
        {
            switch (pt.Size)
            {
            case 1:
                fmt.WriteKeyword("db");
                fmt.Write("\t");
                fmt.Write(string.Format("0x{0:X2}", rdr.ReadByte()));
                fmt.WriteLine();
                return;

            case 2:
                fmt.WriteKeyword("dw");
                fmt.Write("\t");
                fmt.Write(string.Format("0x{0:X4}", rdr.ReadUInt16()));
                fmt.WriteLine();
                return;

            case 4:
                fmt.WriteKeyword("dd");
                fmt.Write("\t");
                fmt.Write(string.Format("0x{0:X8}", rdr.ReadUInt32()));
                fmt.WriteLine();
                return;

            case 8:
                fmt.WriteKeyword("dq");
                fmt.Write("\t");
                fmt.Write(string.Format("0x{0:X16}", rdr.ReadUInt64()));
                fmt.WriteLine();
                return;

            default:
                DumpBytes(pt.Size);
                break;
            }
        }
示例#8
0
 public override void ApplyRelocation(Address baseOfImage, uint page, EndianImageReader rdr, RelocationDictionary relocations)
 {
     rdr.ReadUInt16();
     return;
 }
示例#9
0
        public override ElfSymbol RelocateEntry(Program program, ElfSymbol sym, ElfSection referringSection, ElfRelocation rela)
        {
            if (loader.Sections.Count <= sym.SectionIndex)
            {
                return(sym);
            }
            if (sym.SectionIndex == 0)
            {
                return(sym);
            }
            var  symSection = loader.Sections[(int)sym.SectionIndex];
            uint S          = (uint)sym.Value;
            uint A          = (uint)rela.Addend;
            uint P          = (uint)rela.Offset;
            var  addr       = Address.Ptr32(P);
            uint PP         = P;
            var  arch       = program.Architecture;
            var  relR       = program.CreateImageReader(arch, addr);
            var  relW       = program.CreateImageWriter(arch, addr);

            var rt = (PpcRt)(rela.Info & 0xFF);

            switch (rt)
            {
            case PpcRt.R_PPC_GLOB_DAT:
            case PpcRt.R_PPC_COPY:
            case PpcRt.R_PPC_JMP_SLOT:
                break;

            case PpcRt.R_PPC_ADDR32:
                relW.WriteUInt32(S + A);
                break;

            case PpcRt.R_PPC_REL24:
                uint wInstr = relR.ReadUInt32();
                // 24 bit relocation where bits 3-29 are used for relocations
                uint value = (S + A - P);
                wInstr = (wInstr & 0xFC000003) | (value & 0x03FFFFFC);
                relW.WriteUInt32(wInstr);
                break;

            case PpcRt.R_PPC_ADDR16_HI:
            case PpcRt.R_PPC_ADDR16_HA:
                // Wait for the following R_PPC_ADDR16_LO relocation.
                prevPpcHi16 = rela;
                prevRelR    = relR;
                prevRelW    = relW;
                break;

            case PpcRt.R_PPC_ADDR16_LO:
                if (prevPpcHi16 == null)
                {
                    return(sym);
                }
                uint valueHi = prevRelR.ReadUInt16();
                uint valueLo = relR.ReadUInt16();

                if ((PpcRt)(prevPpcHi16.Info & 0xFF) == PpcRt.R_PPC_ADDR16_HA)
                {
                    valueHi += (valueHi & 0x8000u) != 0 ? 1u : 0u;
                }

                value  = (valueHi << 16) | valueLo;
                value += S;

                valueHi = (value >> 16) & 0xFFFF;
                valueLo = value & 0xFFFF;
                prevRelW.WriteBeUInt16((ushort)valueHi);
                relW.WriteBeUInt16((ushort)valueLo);
                break;

            default:
                if (!missedRelocations.TryGetValue(rt, out var count))
                {
                    count = 0;
                }
                missedRelocations[rt] = count + 1;
                break;
            }
            return(sym);
        }