Example #1
0
        public ImageSymbol CreateImageSymbol(ElfSymbol sym, bool isExecutable)
        {
            if (!isExecutable && sym.SectionIndex > 0 && sym.SectionIndex >= Sections.Count)
            {
                return(null);
            }
            SymbolType?st = GetSymbolType(sym);

            if (st == null || st.Value == SymbolType.Unknown)
            {
                return(null);
            }
            // If this is a relocatable file, the symbol value is
            // an offset from the section's virtual address.
            // If this is an executable file, the symbol value is
            // the virtual address.
            var addr = isExecutable
                ? platform.MakeAddressFromLinear(sym.Value, true)
                : Sections[(int)sym.SectionIndex].Address + sym.Value;

            var dt     = GetSymbolDataType(sym);
            var imgSym = ImageSymbol.Create(
                st.Value,
                this.Architecture,
                addr,
                sym.Name,
                dt);

            imgSym.ProcessorState = Architecture.CreateProcessorState();
            return(imgSym);
        }
Example #2
0
        public List <ImageSymbol> GetAllSymbols()
        {
            var symbols = new List <ImageSymbol>();

            for (uint i = 0; i < hdr.n_symbols; i++)
            {
                rdr.BaseStream.Seek(syms_offset + Marshal.SizeOf(typeof(SymEntry)) * i, SeekOrigin.Begin);
                SymEntry?s = new StructureReader <SymEntry>(rdr).Read();
                if (s == null)
                {
                    break;
                }

                var sym = s.Value;
                rdr.BaseStream.Seek(sym_names_offset + sym.sym_name_off, SeekOrigin.Begin);
                string sym_name = rdr.ReadNullTerminatedString();

                //$BUG: how do we get the architecture?
                symbols.Add(ImageSymbol.Create(
                                SymbolType.Unknown,
                                null,
                                Address.Ptr32(sym.addr),
                                sym_name,
                                new UnknownType((int)(sym.end - sym.addr))));
            }

            return(symbols);
        }
Example #3
0
        public override ImageSymbol AdjustImageSymbol(ImageSymbol sym)
        {
            if (sym.Type != SymbolType.Code &&
                sym.Type != SymbolType.ExternalProcedure &&
                sym.Type != SymbolType.Procedure)
            {
                return(sym);
            }
            if ((sym.Address.ToLinear() & 1) == 0)
            {
                return(sym);
            }
            if (archThumb == null)
            {
                var cfgSvc = loader.Services.RequireService <IConfigurationService>();
                this.archThumb = cfgSvc.GetArchitecture("arm-thumb");
            }
            var addrNew = sym.Address - 1;
            var symNew  = ImageSymbol.Create(
                sym.Type,
                archThumb,
                addrNew,
                sym.Name,
                sym.DataType,
                !sym.NoDecompile);

            symNew.ProcessorState = sym.ProcessorState;
            return(symNew);
        }
Example #4
0
        public override ImageSymbol AdjustImageSymbol(ImageSymbol sym)
        {
            if (sym.Type != SymbolType.Code &&
                sym.Type != SymbolType.ExternalProcedure &&
                sym.Type != SymbolType.Procedure)
            {
                return(sym);
            }
            if ((sym.Address.ToLinear() & 1) == 0)
            {
                return(sym);
            }
            if (archMips16e == null)
            {
                var cfgSvc = loader.Services.RequireService <IConfigurationService>();
                this.archMips16e = cfgSvc.GetArchitecture(loader.Architecture.Name);
                archMips16e.LoadUserOptions(new Dictionary <string, object>
                {
                    { "decoder", "mips16e" }
                });
            }
            var addrNew = sym.Address - 1;
            var symNew  = ImageSymbol.Create(
                sym.Type,
                archMips16e,
                addrNew,
                sym.Name,
                sym.DataType,
                !sym.NoDecompile);

            symNew.ProcessorState = sym.ProcessorState;
            return(symNew);
        }
Example #5
0
        public List <ImageSymbol> GetAllSymbols()
        {
            //TODO: ask if number of symbols is available and preallocate list
            List <ImageSymbol> symbols = new List <ImageSymbol>();

            IntPtr syms = prv.GetSymbols();
            IntPtr sym;

            for (int off = 0; ; off += Marshal.SizeOf(sym))
            {
                sym = Marshal.ReadIntPtr(syms, off);
                if (sym == IntPtr.Zero)
                {
                    break;
                }

                ulong start = prv.GetSymbolStart(sym);
                ulong end   = prv.GetSymbolEnd(sym);

                symbols.Add(ImageSymbol.Create(
                                SymbolType.Unknown,
                                arch,
                                Address.Ptr32((uint)start),
                                name: prv.GetSymbolName(sym),
                                dataType: new UnknownType((int)(end - start))));
            }

            return(symbols);
        }
Example #6
0
        /// <summary>
        /// Scans the addresses between <paramref name="gotStart"/> and <paramref name="gotEnd"/>,
        /// in the GOT, reading successive pointers. If a pointer coincides with the address of
        /// a symbol, generate a GOT symbol and an import reference.
        /// </summary>
        /// <param name="program"></param>
        /// <param name="symbols"></param>
        /// <param name="gotStart"></param>
        /// <param name="gotEnd"></param>
        public void ConstructGotEntries(Program program, SortedList <Address, ImageSymbol> symbols, Address gotStart, Address gotEnd, bool makeGlobals)
        {
            ElfImageLoader.trace.Verbose("== Constructing GOT entries ==");
            var rdr = program.CreateImageReader(program.Architecture, gotStart);

            while (rdr.Address < gotEnd)
            {
                var addrGot = rdr.Address;
                var addrSym = ReadAddress(rdr);
                if (addrSym == null)
                {
                    break;
                }
                if (symbols.TryGetValue(addrSym, out ImageSymbol symbol))
                {
                    // This GOT entry is a known symbol!
                    if (symbol.Type == SymbolType.Procedure || symbol.Type == SymbolType.ExternalProcedure)
                    {
                        ImageSymbol gotSym = CreateGotSymbol(addrGot, symbol.Name);
                        symbols[addrGot] = gotSym;
                        ElfImageLoader.trace.Verbose("{0}+{1:X4}: Found GOT entry {2}, referring to symbol at {3}",
                                                     gotStart, addrGot - gotStart, gotSym, symbol);
                        if (symbol.Type == SymbolType.ExternalProcedure)
                        {
                            program.ImportReferences.Add(
                                addrGot,
                                new NamedImportReference(
                                    addrGot,
                                    null,
                                    symbol.Name,
                                    symbol.Type));
                        }
                    }
                }
                else if (makeGlobals)
                {
                    // This GOT entry has no corresponding symbol. It's likely a global
                    // variable with no name.
                    ImageSymbol gotDataSym = ImageSymbol.Create(SymbolType.Data, this.Architecture, addrGot);
                    ElfImageLoader.trace.Verbose("{0}+{1:X4}: GOT entry with no symbol, assuming local data {2}",
                                                 gotStart, addrGot - gotStart, addrGot);
                    program.ImportReferences.Add(
                        addrGot,
                        new NamedImportReference(
                            addrGot,
                            null,
                            null,
                            gotDataSym.Type));
                }
            }
        }
Example #7
0
        private SortedList <Address, ImageSymbol> BuildSymbols(Program program)
        {
            Func <string, Address> ParseAddress = sAddr =>
            {
                if (!program.Platform.TryParseAddress(sAddr, out Address addr))
                {
                    return(null);
                }
                return(addr);
            };
            var procs = program.Platform.MemoryMap.Segments
                        .SelectMany(s => s.Procedures)
                        .OfType <Procedure_v1>()
                        .Select(p => ImageSymbol.Create(
                                    SymbolType.Procedure,
                                    program.Architecture,
                                    ParseAddress(p.Address),
                                    p.Name,
                                    decompile: p.Decompile));

            return(procs.ToSortedList(k => k.Address, k => k));
        }