示例#1
0
        private void LoadImportReferencesFromRelaPlt()
        {
            var rela_plt = loader.GetSectionInfoByName(".rela.plt");
            var plt      = loader.GetSectionInfoByName(".plt");

            if (rela_plt == null || plt == null)
            {
                return;
            }
            var relaRdr = loader.CreateReader(rela_plt.FileOffset);

            for (ulong i = 0; i < rela_plt.EntryCount(); ++i)
            {
                // Read the .rela.plt entry
                var rela = Elf64_Rela.Read(relaRdr);

                ulong sym    = rela.r_info >> 32;
                var   symStr = loader.Symbols[rela_plt.LinkedSection.FileOffset][(int)sym];

                var addr = plt.Address + (uint)(i + 1) * plt.EntrySize;
                var st   = ElfLoader.GetSymbolType(symStr);
                if (st.HasValue)
                {
                    importReferences[addr] = new NamedImportReference(addr, null, symStr.Name, st.Value);
                }
            }
        }
示例#2
0
        public void NIR_Compare_No_Module_anywhere()
        {
            var nirNullModule1 = new NamedImportReference(Address.Ptr64(0x123400), null, "malloc");
            var nirNullModule2 = new NamedImportReference(Address.Ptr64(0x123400), null, "malloc");

            Assert.AreEqual(0, nirNullModule1.CompareTo(nirNullModule2));
        }
示例#3
0
        private void LoadImportReferencesFromRelPlt(Dictionary <Address, ImportReference> importReferences)
        {
            var rel_plt = loader.GetSectionInfoByName(".rel.plt");

            if (rel_plt == null)
            {
                return;
            }
            var symtab = rel_plt.LinkedSection !;
            var relRdr = loader.CreateReader(rel_plt.FileOffset);

            uint entries = rel_plt.EntryCount();

            for (uint i = 0; i < entries; ++i)
            {
                if (!relRdr.TryReadUInt32(out uint offset))
                {
                    return;
                }
                if (!relRdr.TryReadUInt32(out uint info))
                {
                    return;
                }

                uint   sym    = info >> 8;
                string symStr = loader.GetSymbolName(symtab, sym);

                //$REFACTOR: use common code here.
                var addr = Address.Ptr32(offset);
                importReferences[addr] = new NamedImportReference(
                    addr, "", symStr, SymbolType.ExternalProcedure);
            }
        }
示例#4
0
        public void NIR_Compare()
        {
            var nir1 = new NamedImportReference(Address.Ptr64(0x123400), "libc.so", "malloc");
            var nir2 = new NamedImportReference(Address.Ptr64(0x123400), "libc.so", "malloc");

            Assert.AreEqual(0, nir1.CompareTo(nir2));
        }
示例#5
0
        public void NIR_Compare_No_Module_2()
        {
            var nirNullModule1 = new NamedImportReference(Address.Ptr64(0x123400), null, "malloc", SymbolType.ExternalProcedure);
            var nirNullModule2 = new NamedImportReference(Address.Ptr64(0x123400), "libc.so", "malloc", SymbolType.ExternalProcedure);

            Assert.AreEqual(-1, nirNullModule1.CompareTo(nirNullModule2));
        }
示例#6
0
        private void LoadImportReferencesFromRelPlt(Dictionary <Address, ImportReference> importReferences)
        {
            var rel_plt = loader.GetSectionInfoByName(".rel.plt");
            var symtab  = rel_plt.LinkedSection;

            var plt    = loader.GetSectionInfoByName(".plt");
            var relRdr = loader.CreateReader(rel_plt.FileOffset);

            uint entries = rel_plt.EntryCount();

            for (uint i = 0; i < entries; ++i)
            {
                uint offset;
                if (!relRdr.TryReadUInt32(out offset))
                {
                    return;
                }
                uint info;
                if (!relRdr.TryReadUInt32(out info))
                {
                    return;
                }

                uint   sym    = info >> 8;
                string symStr = loader.GetSymbolName(symtab, sym);

                var addr = plt.Address + (i + 1) * plt.EntrySize;
                importReferences[addr] = new NamedImportReference(
                    addr, null, symStr);
            }
        }
示例#7
0
        public override ElfSymbol RelocateEntry(Program program, ElfSymbol sym, ElfSection referringSection, ElfRelocation rela)
        {
            if (loader.Sections.Count <= sym.SectionIndex)
            {
                return(sym);
            }
            var rt = (SparcRt)(rela.Info & 0xFF);

            if (sym.SectionIndex == 0)
            {
                if (rt == SparcRt.R_SPARC_GLOB_DAT ||
                    rt == SparcRt.R_SPARC_JMP_SLOT)
                {
                    var addrPfn = Address.Ptr32((uint)rela.Offset);
                    Debug.Print("Import reference {0} - {1}", addrPfn, sym.Name);
                    var st = ElfLoader.GetSymbolType(sym);
                    if (st.HasValue)
                    {
                        importReferences[addrPfn] = new NamedImportReference(addrPfn, null, sym.Name, st.Value);
                    }
                    return(sym);
                }
            }

            var     symSection = loader.Sections[(int)sym.SectionIndex];
            uint    S          = (uint)sym.Value + symSection.Address.ToUInt32();
            int     A          = 0;
            int     sh         = 0;
            uint    mask       = ~0u;
            Address addr;

            if (referringSection != null)
            {
                addr = referringSection.Address + rela.Offset;
            }
            else
            {
                addr = Address.Ptr32((uint)rela.Offset);
            }
            uint P  = (uint)addr.ToLinear();
            uint PP = P;
            uint B  = 0;

            Debug.Print("  off:{0:X8} type:{1,-16} add:{3,-20} {4,3} {2} {5}",
                        rela.Offset,
                        (SparcRt)(rela.Info & 0xFF),
                        sym.Name,
                        rela.Addend,
                        (int)(rela.Info >> 8),
                        "section?");

            switch (rt)
            {
            case 0:
                return(sym);

            case SparcRt.R_SPARC_HI22:
                A  = (int)rela.Addend;
                sh = 10;
                P  = 0;
                break;

            case SparcRt.R_SPARC_LO10:
                A    = (int)rela.Addend;
                mask = 0x3FF;
                P    = 0;
                break;

            case SparcRt.R_SPARC_32:
                A    = (int)rela.Addend;
                mask = 0xFFFFFFFF;
                P    = 0;
                break;

            case SparcRt.R_SPARC_WDISP30:
                A  = (int)rela.Addend;
                P  = ~P + 1;
                sh = 2;
                break;

            case SparcRt.R_SPARC_RELATIVE:
                A = (int)rela.Addend;
                B = program.SegmentMap.BaseAddress.ToUInt32();
                break;

            case SparcRt.R_SPARC_COPY:
                Debug.Print("Relocation type {0} not handled yet.", rt);
                return(sym);

            default:
                throw new NotImplementedException(string.Format(
                                                      "SPARC ELF relocation type {0} not implemented yet.",
                                                      rt));
            }
            var arch = program.Architecture;
            var relR = program.CreateImageReader(arch, addr);
            var relW = program.CreateImageWriter(arch, addr);

            var w = relR.ReadBeUInt32();

            w += ((uint)(B + S + A + P) >> sh) & mask;
            relW.WriteBeUInt32(w);

            return(sym);
        }
示例#8
0
        public override (Address?, ElfSymbol?) RelocateEntry(Program program, ElfSymbol sym, ElfSection?referringSection, ElfRelocation rela)
        {
            if (loader.Sections.Count <= sym.SectionIndex)
            {
                return(null, null);
            }
            var rt = (SparcRt)(rela.Info & 0xFF);

            var addr = referringSection != null
                 ? referringSection.Address !+rela.Offset
                 : loader.CreateAddress(rela.Offset);

            if (sym.SectionIndex == 0)
            {
                if (rt == SparcRt.R_SPARC_GLOB_DAT ||
                    rt == SparcRt.R_SPARC_JMP_SLOT)
                {
                    var addrPfn = Address.Ptr32((uint)rela.Offset);
                    ElfImageLoader.trace.Verbose("Import reference {0} - {1}", addrPfn, sym.Name);
                    var st = ElfLoader.GetSymbolType(sym);
                    if (st.HasValue)
                    {
                        importReferences[addrPfn] = new NamedImportReference(addrPfn, null, sym.Name, st.Value);
                    }
                    return(addrPfn, null);
                }
            }

            var   symSection = loader.Sections[(int)sym.SectionIndex];
            ulong S          = 0;
            int   A          = 0;
            int   sh         = 0;
            uint  mask       = ~0u;

            if (referringSection != null)
            {
                addr = referringSection.Address !+rela.Offset;
            }
            else
            {
                addr = Address.Ptr64(rela.Offset);
            }
            ulong P  = addr.ToLinear();
            ulong PP = P;
            ulong B  = 0;

            ElfImageLoader.trace.Verbose("  off:{0:X8} type:{1,-16} add:{3,-20} {4,3} {2} {5}",
                                         rela.Offset,
                                         (SparcRt)(rela.Info & 0xFF),
                                         sym.Name,
                                         rela.Addend.HasValue ? rela.Addend.Value : 0,
                                         (int)(rela.Info >> 8),
                                         "section?");

            switch (rt)
            {
            case 0:
                return(null, null);

            case SparcRt.R_SPARC_HI22:
                A    = (int)rela.Addend !.Value;
                sh   = 10;
                P    = 0;
                mask = 0x3FFFFF;
                return(Relocate32(program, sym, addr, S, A, sh, mask, P, B));

            case SparcRt.R_SPARC_LM22:
                A    = (int)rela.Addend !.Value;
                S    = sym.Value;
                sh   = 10;
                P    = 0;
                mask = 0x3FFFFF;
                return(Relocate32(program, sym, addr, S, A, sh, mask, P, B));

            case SparcRt.R_SPARC_LO10:
                A    = (int)rela.Addend !.Value;
                S    = sym.Value;
                mask = 0x3FF;
                P    = 0;
                return(Relocate32(program, sym, addr, S, A, sh, mask, P, B));

            case SparcRt.R_SPARC_32:
                A    = (int)rela.Addend !.Value;
                S    = sym.Value;
                mask = 0xFFFFFFFF;
                P    = 0;
                return(Relocate32(program, sym, addr, S, A, sh, mask, P, B));

            case SparcRt.R_SPARC_WDISP30:
                A  = (int)rela.Addend !.Value;
                P  = ~P + 1;
                sh = 2;
                return(Relocate32(program, sym, addr, S, A, sh, mask, P, B));

            case SparcRt.R_SPARC_RELATIVE:
                A = (int)rela.Addend !.Value;
                B = program.SegmentMap.BaseAddress.ToLinear();
                P = 0;
                return(Relocate64(program, sym, addr, S, A, sh, mask, P, B));

            case SparcRt.R_SPARC_COPY:
                ElfImageLoader.trace.Warn("Relocation type {0} not handled yet.", rt);
                return(addr, null);

            default:
                ElfImageLoader.trace.Error(
                    "SPARC ELF relocation type {0} not implemented yet.",
                    rt);
                return(addr, null);
            }
        }
示例#9
0
        public override (Address?, ElfSymbol?) RelocateEntry(Program program, ElfSymbol sym, ElfSection?referringSection, ElfRelocation rela)
        {
            if (loader.Sections.Count <= sym.SectionIndex)
            {
                return(null, null);
            }
            var rt = (SparcRt)(rela.Info & 0xFF);

            if (sym.SectionIndex == 0)
            {
                if (rt == SparcRt.R_SPARC_GLOB_DAT ||
                    rt == SparcRt.R_SPARC_JMP_SLOT)
                {
                    var addrPfn = Address.Ptr32((uint)rela.Offset);
                    Debug.Print("Import reference {0} - {1}", addrPfn, sym.Name);
                    var st = ElfLoader.GetSymbolType(sym);
                    if (st.HasValue)
                    {
                        importReferences[addrPfn] = new NamedImportReference(addrPfn, null, sym.Name, st.Value);
                    }
                    return(addrPfn, null);
                }
            }

            var     symSection = loader.Sections[(int)sym.SectionIndex];
            uint    S          = (uint)sym.Value + symSection.Address !.ToUInt32();
            int     A          = 0;
            int     sh         = 0;
            uint    mask       = ~0u;
            Address addr;

            if (referringSection != null)
            {
                addr = referringSection.Address !+rela.Offset;
            }
            else
            {
                addr = Address.Ptr32((uint)rela.Offset);
            }
            uint P  = (uint)addr.ToLinear();
            uint PP = P;
            uint B  = 0;

            Debug.Print("  off:{0:X8} type:{1,-16} add:{3,-20} {4,3} {2} {5}",
                        rela.Offset,
                        (SparcRt)(rela.Info & 0xFF),
                        sym.Name,
                        rela.Addend,
                        (int)(rela.Info >> 8),
                        "section?");

            switch (rt)
            {
            case 0:
                return(addr, null);

            case SparcRt.R_SPARC_HI22:
                A  = (int)rela.Addend;
                sh = 10;
                P  = 0;
                break;

            case SparcRt.R_SPARC_LO10:
                A    = (int)rela.Addend;
                mask = 0x3FF;
                P    = 0;
                break;

            case SparcRt.R_SPARC_32:
                A    = (int)rela.Addend;
                mask = 0xFFFFFFFF;
                P    = 0;
                break;

            case SparcRt.R_SPARC_WDISP30:
                A  = (int)re