Exemplo n.º 1
0
        public override List <ElfSection> LoadSectionHeaders()
        {
            // Create the sections.
            var inames   = new List <uint>();
            var links    = new List <uint>();
            var infos    = new List <uint>();
            var sections = new List <ElfSection>();
            var rdr      = CreateReader(Header64.e_shoff);

            for (uint i = 0; i < Header64.e_shnum; ++i)
            {
                var shdr    = Elf64_SHdr.Load(rdr);
                var section = new ElfSection
                {
                    Number  = i,
                    Type    = shdr.sh_type,
                    Flags   = shdr.sh_flags,
                    Address = shdr.sh_addr != 0
                        ? platform !.MakeAddressFromLinear(shdr.sh_addr, false)
                        : null !,
                    FileOffset = shdr.sh_offset,
                    Size       = shdr.sh_size,
                    Alignment  = shdr.sh_addralign,
                    EntrySize  = shdr.sh_entsize,
                };
                sections.Add(section);
                inames.Add(shdr.sh_name);
                links.Add(shdr.sh_link);
                infos.Add(shdr.sh_info);
            }

            // Get section names and crosslink sections.

            for (int i = 0; i < sections.Count; ++i)
            {
                var section = sections[i];
                section.Name = ReadSectionName(sections, inames[i]);

                ElfSection?linkSection = null;
                ElfSection?relSection  = null;
                switch (section.Type)
                {
                case SectionHeaderType.SHT_REL:
                case SectionHeaderType.SHT_RELA:
                    linkSection = GetSectionByIndex(sections, links[i]);
                    relSection  = GetSectionByIndex(sections, infos[i]);
                    break;

                case SectionHeaderType.SHT_DYNAMIC:
                case SectionHeaderType.SHT_HASH:
                case SectionHeaderType.SHT_SYMTAB:
                case SectionHeaderType.SHT_DYNSYM:
                    linkSection = GetSectionByIndex(sections, links[i]);
                    break;
                }
                section.LinkedSection    = linkSection;
                section.RelocatedSection = relSection;
            }
            return(sections);
        }
Exemplo n.º 2
0
        public void Render(ulong fileOffset, Formatter formatter)
        {
            // Get the entry that has the segment# for the string table.
            var dynStrtab = loader.GetDynamicEntries(fileOffset).Where(d => d.Tag == DT_STRTAB).FirstOrDefault();

            if (dynStrtab == null)
            {
                return;
            }
            var offStrtab = loader.AddressToFileOffset(dynStrtab.UValue);

            this.strtabSection = ((ElfLoader64)loader).GetSectionInfoByAddr64(dynStrtab.UValue) !;
            foreach (var entry in loader.GetDynamicEntries(fileOffset))
            {
                DtFormat fmt;
                string   entryName;
                if (!machineSpecific.TryGetValue(entry.Tag, out var dser) &&
                    !ElfDynamicEntry.TagInfos.TryGetValue(entry.Tag, out dser))
                {
                    entryName = string.Format("{0:X8}    ", entry.Tag);
                    fmt       = DtFormat.Hexadecimal;
                }
                else
                {
                    entryName = dser.Name !;
                    fmt       = dser.Format;
                }
                RenderEntry(entryName, fmt, entry, formatter);
                formatter.WriteLine();
            }
        }
Exemplo n.º 3
0
 public DynamicSectionRenderer64(ElfLoader loader, ElfSection?shdr, ElfMachine machine)
 {
     this.loader = loader;
     this.shdr   = shdr;
     if (!ElfDynamicEntry.MachineSpecificInfos.TryGetValue(machine, out machineSpecific))
     {
         machineSpecific = new Dictionary <long, ElfDynamicEntry.TagInfo>();
     }
 }
Exemplo n.º 4
0
        public override (Address?, ElfSymbol?) RelocateEntry(Program program, ElfSymbol sym, ElfSection?referringSection, ElfRelocation rela)
        {
            if (loader.Sections.Count <= sym.SectionIndex)
            {
                return(null, null);
            }
            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(null, null);
                }
                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(addr, null);
        }
Exemplo n.º 5
0
        public override (Address?, ElfSymbol?) RelocateEntry(Program program, ElfSymbol symbol, ElfSection?referringSection, ElfRelocation rela)
        {
            if (loader.Sections.Count <= symbol.SectionIndex)
            {
                return(null, null);
            }
            ulong S    = (ulong)symbol.Value;
            ulong A    = (ulong)rela.Addend;
            ulong P    = (ulong)rela.Offset;
            ulong B    = program.SegmentMap.BaseAddress.ToLinear();
            var   addr = Address.Ptr64(P);
            ulong PP   = P;
            var   arch = program.Architecture;
            var   relR = program.CreateImageReader(arch, addr);
            var   relW = program.CreateImageWriter(arch, addr);

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

            switch (rt)
            {
            case Ppc64Rt.R_PPC64_RELATIVE: // B + A
                S = 0;
                P = 0;
                break;

            case Ppc64Rt.R_PPC64_ADDR64:    // S + A
                B = 0;
                P = 0;
                break;

            case Ppc64Rt.R_PPC64_JMP_SLOT:
                return(addr, null);

            default:
                var listener = this.loader.Services.RequireService <DecompilerEventListener>();
                var loc      = listener.CreateAddressNavigator(program, addr);
                listener.Warn(loc, $"Unimplemented PowerPC64 relocation type {rt}.");
                return(addr, null);
            }
            var w = relR.ReadUInt64();

            w += (B + A + S + P);
            relW.WriteUInt64(w);
            return(addr, null);
        }
Exemplo n.º 6
0
        public override (Address?, ElfSymbol?) RelocateEntry(Program program, ElfSymbol symbol, ElfSection?referringSection, ElfRelocation rela)
        {
            var   rt = (RiscVRt)(rela.Info & 0xFF);
            ulong S  = symbol.Value;
            ulong A  = 0;
            ulong B  = 0;

            var addr = referringSection != null
                ? referringSection.Address + rela.Offset
                : loader.CreateAddress(rela.Offset);
            var arch = program.Architecture;
            var relR = program.CreateImageReader(arch, addr);
            var relW = program.CreateImageWriter(arch, addr);

            switch (rt)
            {
            case RiscVRt.R_RISCV_COPY:
                return(addr, null);

            case RiscVRt.R_RISCV_RELATIVE: // B + A
                A = (ulong)rela.Addend;
                B = program.SegmentMap.BaseAddress.ToLinear();
                S = 0;
                break;

            case RiscVRt.R_RISCV_64: // S + A
                A = (ulong)rela.Addend;
                B = 0;
                break;

            case RiscVRt.R_RISCV_JUMP_SLOT: // S
                S = symbol.Value;
                if (S == 0)
                {
                    var gotEntry = relR.PeekLeUInt64(0);
                    var newSym   = CreatePltStubSymbolFromRelocation(symbol, gotEntry, 0x0);
                    return(addr, newSym);
                }
                break;

            default:
                Debug.Print("ELF RiscV: unhandled 32-bit relocation {0}: {1}", rt, rela);
                break;
            }

            var w = relR.ReadLeUInt64();

            w += ((uint)(B + S + A));
            relW.WriteLeUInt64(w);
            return(addr, null);
        }
Exemplo n.º 7
0
        public override (Address?, ElfSymbol?) RelocateEntry(Program program, ElfSymbol symbol, ElfSection?referringSection, ElfRelocation rela)
        {
            var rt = (XtensaRt)(rela.Info & 0xFF);

            ElfImageLoader.trace.Inform("XTensa relocation {0}: {1}", rt, rela);
            switch (rt)
            {
            case XtensaRt.R_XTENSA_32:
                //aspace.make_data(raddr, wordsz);
                //if (value != null) {
                //    data = aspace.get_data(raddr, wordsz);
                //    if (is_exe) {
                //        if (data != value) {
                //            log.Warn("Computed reloc value and value present in fully linked file differ: 0x%x vs 0x%x", value, data)
                //        }
                //    }
                //    else {
                //        data += value;
                //        aspace.set_data(raddr, data, wordsz);
                //        aspace.make_arg_offset(raddr, 0, data);
                //    }
                //} else {
                //    // Undefined symbol
                //    // TODO: This is more or less hacky way to do this. It would be
                //    // better to explicitly assign a symbolic alias to a value at
                //    // particular address, but so far we assume call below to do
                //    // that.
                //    aspace.make_arg_offset(raddr, 0, symname);
                //}
                break;

            case XtensaRt.R_XTENSA_SLOT0_OP:
                //if (is_exe)
                //    break;
                //var opcode = aspace.get_byte(raddr);
                //if ((opcode & 0xf) == 0x5) {
                //    // call
                //    if (value != null) {
                //        p = raddr
                //        value -= ((p & ~0x3) + 4);
                //        Debug.Assert((value & 0x3) == 0);
                //        value = value >> 2;
                //        aspace.set_byte(p, (opcode & ~0xc0) | ((value << 6) & 0xc0));
                //        aspace.set_byte(p + 1, value >> 2);
                //        aspace.set_byte(p + 2, value >> 10);
                //    }
                //}
                //if (opcode & 0xf == 0x1) {
                //    // l32r
                //}
                break;

            default:
                break;
            }
            return(null, null);
        }
Exemplo n.º 8
0
        public override (Address?, ElfSymbol?) RelocateEntry(Program program, ElfSymbol sym, ElfSection?referringSection, ElfRelocation rela)
        {
            if (loader.Sections.Count <= sym.SectionIndex)
            {
                return(null, null);
            }
            uint S    = (uint)sym.Value;
            int  A    = 0;
            int  sh   = 0;
            uint mask = ~0u;
            var  addr = referringSection != null
                ? referringSection.Address !+rela.Offset
                : loader.CreateAddress(rela.Offset);
            uint P    = (uint)addr.ToLinear();
            uint PP   = P;
            uint B    = 0;
            var  arch = program.Architecture;
            var  relR = program.CreateImageReader(arch, addr);
            var  relW = program.CreateImageWriter(arch, addr);
            var  rt   = (i386Rt)(rela.Info & 0xFF);

            switch (rt)
            {
            case i386Rt.R_386_NONE: //  just ignore (common)
                break;

            case i386Rt.R_386_COPY:
                break;

            case i386Rt.R_386_RELATIVE: // B + A
                A = (int)(rela.Addend ?? 0);
                B = program.SegmentMap.BaseAddress.ToUInt32();
                break;

            case i386Rt.R_386_JMP_SLOT:
                if (sym.Value == 0)
                {
                    // Broken GCC compilers generate relocations referring to symbols
                    // whose value is 0 instead of the expected address of the PLT stub.
                    var gotEntry = relR.PeekLeUInt32(0);
                    var symNew   = CreatePltStubSymbolFromRelocation(sym, gotEntry, 6);
                    return(addr, symNew);
                }
                break;

            case i386Rt.R_386_32: // S + A
                                  // Read the symTabIndex'th symbol.
                A = (int)(rela.Addend ?? 0);
                P = 0;
                break;

            case i386Rt.R_386_PC32: // S + A - P
                if (sym.Value == 0)
                {
                    // This means that the symbol doesn't exist in this module, and is not accessed
                    // through the PLT, i.e. it will be statically linked, e.g. strcmp. We have the
                    // name of the symbol right here in the symbol table entry, but the only way
                    // to communicate with the loader is through the target address of the call.
                    // So we use some very improbable addresses (e.g. -1, -2, etc) and give them entries
                    // in the symbol table
                    //S = nextFakeLibAddr--; // Allocate a new fake address
                    //loader.AddSymbol(S, sym.Name);
                    //}
                }
                A = (int)(rela.Addend ?? 0);
                P = ~P + 1;
                break;

            case i386Rt.R_386_GLOB_DAT:
                // This relocation type is used to set a global offset table entry to the address of the
                // specified symbol. The special relocation type allows one to determine the
                // correspondence between symbols and global offset table entries.
                P = 0;
                break;

            default:
                throw new NotImplementedException(
                          $"i386 ELF relocation type {rt} not implemented yet.");
            }
            var w = relR.ReadLeUInt32();

            w += ((uint)(B + S + A + P) >> sh) & mask;
            relW.WriteLeUInt32(w);
            return(addr, sym);
        }
Exemplo n.º 9
0
        public override (Address?, ElfSymbol?) RelocateEntry(Program program, ElfSymbol symbol, ElfSection?referringSection, ElfRelocation rel)
        {
            if (symbol == null ||
                loader.Sections.Count <= symbol.SectionIndex)
            {
                return(null, null);
            }

            Address addr;
            uint    P;

            if (referringSection?.Address != null)
            {
                addr = referringSection.Address + rel.Offset;
                P    = (uint)addr.ToLinear();
            }
            else
            {
                addr = Address.Ptr64(rel.Offset);
                P    = 0;
            }
            //var S = symbol.Value;
            //uint PP = P;
            //var relR = program.CreateImageReader(program.Architecture, addr);
            //var relW = program.CreateImageWriter(program.Architecture, addr);
            //int sh = 0;
            //uint mask = 0;
            //uint A = 0;
            //var rt = (MIPSrt) (rel.Info & 0xFF);
            //switch (rt)
            //{

            //}
            return(addr, null);
        }
Exemplo n.º 10
0
        public override (Address?, ElfSymbol?) RelocateEntry(Program program, ElfSymbol symbol, ElfSection?referringSection, ElfRelocation rel)
        {
            if (symbol == null || loader.Sections.Count <= symbol.SectionIndex)
            {
                return(null, null);
            }
            Address addr;
            ulong   P;

            if (referringSection?.Address != null)
            {
                addr = referringSection.Address + rel.Offset;
                P    = addr.ToLinear();
            }
            else
            {
                addr = Address.Ptr64(rel.Offset);
                P    = 0;
            }
            if (addr.ToLinear() == 0)
            {
                return(addr, symbol);
            }
            var uAddrSection = (symbol.SectionIndex != 0)
                ? loader.Sections[(int)symbol.SectionIndex].Address?.ToLinear() ?? 0
                : 0;
            uint S = (uint)(symbol.Value + uAddrSection);

            ulong PP   = P;
            var   relR = program.CreateImageReader(program.Architecture, addr);
            var   relW = program.CreateImageWriter(program.Architecture, addr);
            int   sh   = 0;
            uint  mask = 0;

            if (!relR.TryPeekBeUInt32(0, out uint w))
            {
                return(null, null);
            }
            uint A = (rel.Addend.HasValue)
                ? (uint)rel.Addend.Value
                : w;
            uint ww = w;

            switch ((Mips64Rt)rel.Info)
            {
            case Mips64Rt.R_MIPS_NONE:
                return(null, null);

            case Mips64Rt.R_MIPS_64:
                ulong A64;
                if (rel.Addend.HasValue)
                {
                    A64 = (ulong)rel.Addend.Value;
                }
                else if (!relR.TryPeekUInt64(0, out A64))
                {
                    return(null, null);
                }
                relW.WriteUInt64(S + A64);
                return(addr, symbol);

            case Mips64Rt.R_MIPS_26:
                long sA = targ26.ReadSigned(A) << 2;
                uint n  = (uint)((S + (ulong)sA) >> 2);
                ww = (w & 0xFC000000u) | n;
                break;

            default:
                ElfImageLoader.trace.Warn("Unimplemented MIPS64 relocation type: {0}", RelocationTypeToString((uint)rel.Info));
                return(addr, symbol);
            }
            relW.WriteUInt32(ww);
            return(addr, symbol);
        }
Exemplo n.º 11
0
        public override (Address?, ElfSymbol?) RelocateEntry(Program program, ElfSymbol symbol, ElfSection?referringSection, ElfRelocation rel)
        {
            if (symbol == null || loader.Sections.Count <= symbol.SectionIndex)
            {
                return(null, null);
            }
            Address addr;
            uint    P;

            if (referringSection?.Address != null)
            {
                addr = referringSection.Address + rel.Offset;
                P    = (uint)addr.ToLinear();
            }
            else
            {
                addr = Address.Ptr64(rel.Offset);
                P    = 0;
            }
            var  S    = symbol.Value;
            uint PP   = P;
            int  sh   = 0;
            uint mask = 0;
            uint A    = 0;

            switch ((MIPSrt)(rel.Info & 0xFF))
            {
            case MIPSrt.R_MIPS_NONE: return(addr, null);

            case MIPSrt.R_MIPS_REL32:
                break;

            default:
                mask = 0;
                break;

                /*
                 * R_MIPS_NONE      0  none     local    none
                 * R_MIPS_16        1  V–half16 external S + sign–extend(A)
                 *               1  V–half16 local    S + sign–extend(A)
                 * R_MIPS_32        2  T–word32 external S + A
                 *               2  T–word32 local    S + A
                 * R_MIPS_REL32     3  T–word32 external A – EA + S
                 * R_MIPS_REL32     3  T–word32 local    A – EA + S
                 * R_MIPS_26        4  T–targ26 local    (((A << 2) | \
                 *                                    (P & 0xf0000000) + S) >> 2
                 *               4  T–targ26 external (sign–extend(A < 2) + S) >> 2
                 * R_MIPS_HI16      5  T–hi16   external ((AHL + S) – \
                 *                                      (short)(AHL + S)) >> 16
                 *               5  T–hi16   local    ((AHL + S) – \
                 *                                      (short)(AHL + S)) >> 16
                 *               5  V–hi16   _gp_disp (AHL + GP – P) – (short) \
                 *                                      (AHL + GP – P)) >> 16
                 * R_MIPS_LO16      6  T–lo16   external AHL + S
                 *               6  T–lo16   local    AHL + S
                 *               6  V–lo16   _gp_disp AHL + GP – P + 4
                 * R_MIPS_GPREL16   7  V–rel16  external sign–extend(A) + S + GP
                 *               7  V–rel16  local    sign–extend(A) + S + GP0 – GP
                 * R_MIPS_LITERAL   8  V–lit16  local    sign–extend(A) + L
                 * R_MIPS_GOT16     9  V–rel16  external G
                 *               9  V–rel16  local    see below
                 * R_MIPS_PC16      10 V–pc16   external sign–extend(A) + S – P
                 * R_MIPS_CALL16    11 V–rel16  external G
                 * R_MIPS_GPREL32   12 T–word32 local    A + S + GP0 – GP
                 * R_MIPS_GOTHI16   21 T-hi16   external (G - (short)G) >> 16 + A
                 * R_MIPS_GOTLO16   22 T-lo16   external G & 0xffff
                 * R_MIPS_CALLHI16  30 T-hi16   external (G - (short)G) >> 16 + A
                 * R_MIPS_CALLLO16  31 T-lo16   external G & 0xffff */
            }
            var relR = program.CreateImageReader(program.Architecture, addr);
            var relW = program.CreateImageWriter(program.Architecture, addr);
            var w    = relR.ReadUInt32();

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

            return(addr, null);
        }
Exemplo n.º 12
0
 public override (Address, ElfSymbol) RelocateEntry(Program program, ElfSymbol symbol, ElfSection?referringSection, ElfRelocation rela)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 13
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
Exemplo n.º 14
0
        public override (Address?, ElfSymbol?) RelocateEntry(Program program, ElfSymbol symbol, ElfSection?referringSection, ElfRelocation rel)
        {
            if (symbol == null || loader.Sections.Count <= symbol.SectionIndex)
            {
                return(null, null);
            }
            Address addr;
            ulong   P;

            if (referringSection?.Address != null)
            {
                addr = referringSection.Address + rel.Offset;
                P    = addr.ToLinear();
            }
            else
            {
                addr = Address.Ptr64(rel.Offset);
                P    = 0;
            }
            if (addr.ToLinear() == 0)
            {
                return(addr, symbol);
            }
            var   S    = symbol.Value;
            ulong PP   = P;
            var   relR = program.CreateImageReader(program.Architecture, addr);
            var   relW = program.CreateImageWriter(program.Architecture, addr);
            int   sh   = 0;
            uint  mask = 0;

            if (!relR.TryPeekBeUInt32(0, out uint w))
            {
                return(null, null);
            }
            uint A = (rel.Addend.HasValue)
                ? (uint)rel.Addend.Value
                : w;
            uint ww = w;

            switch ((Mips64Rt)rel.Info)
            {
            case Mips64Rt.R_MIPS_NONE:
                return(null, null);

            case Mips64Rt.R_MIPS_64:
                ulong A64;
                if (rel.Addend.HasValue)
                {
                    A64 = (ulong)rel.Addend.Value;
                }
                else if (!relR.TryPeekUInt64(0, out A64))
                {
                    return(null, null);
                }
                Debug.Print("    Reloc at {0} is now {1:16}", addr, S + A64);
                relW.WriteUInt64(S + A64);
                return(addr, symbol);

            case Mips64Rt.R_MIPS_26:
                uint n;
                if (symbol.Bind == ElfSymbolBinding.STB_LOCAL)
                {
                    n = (uint)(((A << 2) | (addr.ToLinear() & 0xF0000000u)) + S) >> 2;
                }
                else
                {
                    long sA = targ26.ReadSigned(A) << 2;
                    n = (uint)((S + (ulong)sA) >> 2);
                }
                ww = (w & 0xFC000000u) | n;
                relW.WriteUInt32(w);
                break;

            default:
                ElfImageLoader.trace.Warn("Unimplemented MIPS64 relocation type: {0}", RelocationTypeToString((uint)rel.Info));
                return(addr, symbol);
            }
            relW.WriteUInt32(ww);
            Debug.Print("    Reloc at {0} was {1:X8}, is now {2:X8}", addr, w, ww);
            return(addr, symbol);
        }
Exemplo n.º 15
0
 /// <summary>
 /// Perform the relocation specified by <paramref name="rela"/>, using the <paramref name="symbol"/> as a
 /// reference.
 /// </summary>
 /// <param name="program">The program image being loaded</param>
 /// <param name="symbol">The <see cref="ElfSymbol"/> associated with this relocation.</param>
 /// <param name="referringSection">The section in which the relocation is.</param>
 /// <param name="rela">The relocation information.</param>
 /// <returns>The address of the entry in the GOT where the relocation was performed, and optionally
 /// a symbol for the PLT entry that refers to that GOT entry.</returns>
 public abstract (Address?, ElfSymbol?) RelocateEntry(Program program, ElfSymbol symbol, ElfSection?referringSection, ElfRelocation rela);
Exemplo n.º 16
0
        public override (Address?, ElfSymbol?) RelocateEntry(Program program, ElfSymbol symbol, ElfSection?referringSection, ElfRelocation rela)
        {
            var   addr = Address.Ptr64(rela.Offset);
            var   rt   = (Aarch64Rt)(rela.Info & 0xFFFF);
            ulong A    = (ulong)rela.Addend;
            ulong S    = symbol.Value;

            switch (rt)
            {
            case Aarch64Rt.R_AARCH64_RELATIVE:
                A = S = 0;
                break;

            case Aarch64Rt.R_AARCH64_JUMP_SLOT: // A + S
            case Aarch64Rt.R_AARCH64_GLOB_DAT:  // A + S
                break;

            default:
                var listener = this.loader.Services.RequireService <DecompilerEventListener>();
                var loc      = listener.CreateAddressNavigator(program, addr);
                listener.Warn(loc, $"Unimplemented PowerPC64 relocation type {rt}.");
                return(addr, null);
            }
            var arch = program.Architecture;
            var relR = program.CreateImageReader(arch, addr);
            var relW = program.CreateImageWriter(arch, addr);

            var w = relR.ReadUInt64();

            w += (S + A);
            relW.WriteUInt64(w);

            return(addr, null);
        }
Exemplo n.º 17
0
        public override (Address?, ElfSymbol?) RelocateEntry(Program program, ElfSymbol symbol, ElfSection?referringSection, ElfRelocation rela)
        {
            var rt = (SuperHrt)(byte)rela.Info;

            if (rt == SuperHrt.R_SH_GLOB_DAT ||
                rt == SuperHrt.R_SH_JMP_SLOT)
            {
                var addrPfn = Address.Ptr32((uint)rela.Offset);
                Debug.Print("Import reference {0} - {1}", addrPfn, symbol.Name);
                var st = ElfLoader.GetSymbolType(symbol);
                if (st.HasValue)
                {
                    program.ImportReferences[addrPfn] = new NamedImportReference(addrPfn, null, symbol.Name, st.Value);
                }
                return(addrPfn, null);
            }
            return(null, null);
        }
Exemplo n.º 18
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 !.Value;
                sh = 10;
                P  = 0;
                break;

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

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

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

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

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

            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(addr, null);
        }
Exemplo n.º 19
0
        public override (Address?, ElfSymbol?) RelocateEntry(Program program, ElfSymbol symbol, ElfSection?referringSection, ElfRelocation rela)
        {
            ulong S    = (ulong)symbol.Value;
            ulong A    = 0;
            ulong B    = 0;
            var   rt   = (zSeriesRt)(rela.Info & 0xFF);
            var   addr = referringSection != null
                ? referringSection.Address + rela.Offset
                : loader.CreateAddress(rela.Offset);
            var arch = program.Architecture;
            var relR = program.CreateImageReader(arch, addr);
            var relW = program.CreateImageWriter(arch, addr);

            switch (rt)
            {
            case zSeriesRt.R_390_RELATIVE:  // B + A
                A = (ulong)rela.Addend;
                B = program.SegmentMap.BaseAddress.ToLinear();
                S = 0;
                break;

            case zSeriesRt.R_390_GLOB_DAT:  // S + A
                A = (ulong)rela.Addend;
                break;

            case zSeriesRt.R_390_JMP_SLOT:
                if (symbol.Value == 0)
                {
                    // Broken GCC compilers generate relocations referring to symbols
                    // whose value is 0 instead of the expected address of the PLT stub.
                    var gotEntry = relR.PeekBeUInt64(0);
                    var symNew   = CreatePltStubSymbolFromRelocation(symbol, gotEntry, 0xE);
                    return(addr, symNew);
                }
                break;

            default:
                Debug.Print("Unhandled relocation {0}: {1}", rt, rela);
                break;
            }

            var w = relR.ReadBeUInt64();

            w += ((uint)(B + S + A));
            relW.WriteBeUInt64(w);

            return(addr, null);
        }
Exemplo n.º 20
0
 public override (Address?, ElfSymbol?) RelocateEntry(Program program, ElfSymbol symbol, ElfSection?referringSection, ElfRelocation rela)
 {
     //throw new NotImplementedException();
     return(null, null);
 }
Exemplo n.º 21
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);
            }
        }
Exemplo n.º 22
0
 public override (Address?, ElfSymbol?) RelocateEntry(Program program, ElfSymbol symbol, ElfSection?referringSection, ElfRelocation rela)
 {
     return(null, null);
 }
Exemplo n.º 23
0
        public override (Address?, ElfSymbol?) RelocateEntry(Program program, ElfSymbol symbol, ElfSection?referringSection, ElfRelocation rela)
        {
            /*
             * S (when used on its own) is the address of the symbol
             * A is the addend for the relocation.
             * P is the address of the place being relocated (derived from r_offset).
             * Pa is the adjusted address of the place being reloc
             * ated, defined as (P & 0xFFFFFFFC).
             * T is 1 if the target symbol S has type STT_FUNC and the symbol addresses a Thumb instruction; it is 0
             * otherwise.
             * B(S) is the addressing origin of the output segment defining the symbol S. The origin is not required to be
             * the base address of the segment. This value must always be word-aligned.
             *
             * GOT_ORG is the addressing origin of the Global Offset Table (the indirection table for imported data
             * addresses).  This value must always be word-aligned.  See §4.6.1.8, Proxy generating relocations.
             * GOT(S) is the address of the GOT entry for the symbol S.
             * Table
             * 0   R_ARM_NONE      Static      Miscellaneous
             * 1   R_ARM_PC24      Deprecated  ARM             ((S + A) | T) - P
             * 2   R_ARM_ABS32     Static      Data            ((S + A) | T)
             * 3   R_ARM_REL32     Static      Data            ((S + A) | T) – P
             * 4   R_ARM_LDR_PC_G0 Static      ARM             S + A – P
             * 5   R_ARM_ABS16     Static      Data            S + A
             * 6   R_ARM_ABS12     Static      ARM             S + A
             * 7   R_ARM_THM_ABS5  Static      Thumb16         S + A
             * 8   R_ARM_ABS8      Static      Data            S + A
             * 9   R_ARM_SBREL32   Static      Data            ((S + A) | T) – B(S)
             * 20  R_ARM_COPY                                  S
             * 21  R_ARM_GLOB_DAT Dynamic      Data            (S + A) | T
             * 22  R_ARM_JUMP_SLOT Dynamic     Data            (S + A) | T
             * 23  R_ARM_RELATIVE Dynamic      Data            B(S) + A  [Note: see Table 4-18]
             */
            var  A    = rela.Addend.HasValue ? rela.Addend.Value : 0;
            uint S    = (uint)symbol.Value;
            uint mask = ~0u;
            int  sh   = 0;
            var  addr = referringSection != null
                ? referringSection.Address !+rela.Offset
                : loader.CreateAddress(rela.Offset);
            var rt = (Arm32Rt)(rela.Info & 0xFF);

            switch (rt)
            {
            case Arm32Rt.R_ARM_NONE:
                return(addr, null);

            case Arm32Rt.R_ARM_COPY:
                A = S = 0;
                break;

            case Arm32Rt.R_ARM_ABS32:
            case Arm32Rt.R_ARM_GLOB_DAT:
            case Arm32Rt.R_ARM_JUMP_SLOT:
                // Add sym + rel.a
                break;

            case Arm32Rt.R_ARM_RELATIVE:
                // From the docs:
                //
                // (S ≠ 0) B(S) resolves to the difference between the address
                // at which the segment defining the symbol S was loaded and
                // the address at which it was linked.
                // (S = 0) B(S) resolves to the difference between the address
                // at which the segment being relocated was loaded and the
                // address at which it was linked.
                //
                // Reko always loads objects at their specified physical address,
                // so this relocation is a no-op;
                A = S = 0;
                break;

            case Arm32Rt.R_ARM_TLS_TPOFF32:
                // Allocates a 32 bit TLS slot
                //$REVIEW: the documentation is unreadable, but this is a
                // guess.
                uint tlsSlotOffset = AllocateTlsSlot();
                A += tlsSlotOffset;
                break;

            case Arm32Rt.R_ARM_TLS_DTPMOD32:
                //$REVIEW: this seems to refer to the modules
                // used when creating the binary. My guess is
                // that it wont be necessary for a fruitful
                // decompilation -jkl
                A = S = 0;
                break;

            case Arm32Rt.R_ARM_TLS_DTPOFF32:
                //$NYI
                break;

            case Arm32Rt.R_ARM_CALL:
            case Arm32Rt.R_ARM_PC24:
            case Arm32Rt.R_ARM_JUMP24:
            {
                if ((symbol.Value & 0b11) != 0)
                {
                    var eventListener = loader.Services.RequireService <DecompilerEventListener>();
                    var loc           = eventListener.CreateAddressNavigator(program, addr);
                    eventListener.Warn(loc, "Section {0}: unsupported interworking call (ARM -> Thumb)", referringSection?.Name ?? "<null>");
                    return(addr, null);
                }

                var relInstr = program.CreateImageReader(program.Architecture, addr);
                var uInstr   = relInstr.ReadUInt32();
                var offset   = uInstr;
                offset = (offset & 0x00ffffff) << 2;
                if ((offset & 0x02000000) != 0)
                {
                    offset -= 0x04000000;
                }

                offset += (uint)symbol.Value - addr.ToUInt32() + program.SegmentMap.BaseAddress.ToUInt32();

#if NOT_YET
                /*
                 * Route through a PLT entry if 'offset' exceeds the
                 * supported range. Note that 'offset + loc + 8'
                 * contains the absolute jump target, i.e.,
                 * @sym + addend, corrected for the +8 PC bias.
                 */
                if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS) &&
                    (offset <= (s32)0xfe000000 ||
                     offset >= (s32)0x02000000))
                {
                    offset = get_module_plt(module, loc,
                                            offset + loc + 8)
                             - loc - 8;
                }
#endif

                if ((int)offset <= -0x02000000 || (int)offset >= 0x02000000)
                {
                    var eventListener = loader.Services.RequireService <DecompilerEventListener>();
                    var loc           = eventListener.CreateAddressNavigator(program, addr);
                    eventListener.Warn(loc, "section {0} relocation at {1} out of range", referringSection?.Name ?? "<null>", addr);
                    return(addr, null);
                }

                offset >>= 2;
                offset  &= 0x00ffffff;

                uInstr &= 0xFF000000;
                uInstr |= offset;

                var relWriter = program.CreateImageWriter(program.Architecture, addr);
                relWriter.WriteUInt32(uInstr);

                return(addr, null);
            }

            case Arm32Rt.R_ARM_MOVW_ABS_NC:
            case Arm32Rt.R_ARM_MOVT_ABS:
            {
                //var instr = program.CreateDisassembler(program.Architecture, addr).First();

                var relInstr = program.CreateImageReader(program.Architecture, addr);
                var uInstr   = relInstr.ReadUInt32();
                var offset   = uInstr;
                offset = ((offset & 0xf0000) >> 4) | (offset & 0xfff);
                offset = (offset ^ 0x8000) - 0x8000;

                offset += (uint)symbol.Value;
                if (rt == Arm32Rt.R_ARM_MOVT_ABS)
                {
                    offset >>= 16;
                }

                var tmp = uInstr & 0xfff0f000;
                tmp |= ((offset & 0xf000) << 4) |
                       (offset & 0x0fff);

                var relWriter = program.CreateImageWriter(program.Architecture, addr);
                relWriter.WriteUInt32(tmp);
                return(addr, null);
            }

            default:
                throw new NotImplementedException($"AArch32 relocation type {rt} is not implemented yet.");
            }

            var arch = program.Architecture;
            var relR = program.CreateImageReader(arch, addr);
            var relW = program.CreateImageWriter(arch, addr);

            var w = relR.ReadLeUInt32();
            w += ((uint)(S + A) >> sh) & mask;
            relW.WriteLeUInt32(w);

            return(addr, null);
        }
Exemplo n.º 24
0
        public override (Address?, ElfSymbol?) RelocateEntry(Program program, ElfSymbol symbol, ElfSection?referringSection, ElfRelocation rela)
        {
            var rt = (RiscVRt)(rela.Info & 0xFF);

            return(null, symbol);
        }
Exemplo n.º 25
0
        public override (Address?, ElfSymbol?) RelocateEntry(Program program, ElfSymbol symbol, ElfSection?referringSection, ElfRelocation rela)
        {
            switch ((Mips64Rt)rela.Info)
            {
            case Mips64Rt.R_MIPS_NONE:
                return(null, null);

            default:
                ElfImageLoader.trace.Warn("Unimplemented MIPS64 relocation type: {0}", RelocationTypeToString((uint)rela.Info));
                break;
            }
            return(null, null);
        }