Esempio n. 1
0
 public override void Render(ImageMapSegment segment, Program program, Formatter formatter)
 {
     // Get the entry that has the segment# for the string table.
     var dynStrtab = loader.GetDynEntries(shdr.sh_offset).Where(d => d.d_tag == DT_STRTAB).FirstOrDefault();
     if (dynStrtab == null)
         return;
     var strtabSection = loader.GetSectionInfoByAddr(dynStrtab.d_ptr);
     foreach (var entry in loader.GetDynEntries(shdr.sh_offset))
     {
         switch (entry.d_tag)
         {
         default:
             formatter.Write("{0,-12} {1:X8}", entry.d_tag, entry.d_val);
             break;
         case DT_DEBUG:
             formatter.Write("{0,-12} {1:X8}", "DT_DEBUG", entry.d_val);
             break;
         case DT_FINI:
             formatter.Write("{0,-12} ", "DT_FINI");
             formatter.WriteHyperlink(string.Format("{0:X8}", entry.d_ptr), Address.Ptr32(entry.d_ptr));
             break;
         case DT_HASH:
             formatter.Write("{0,-12} ", "DT_HASH");
             formatter.WriteHyperlink(string.Format("{0:X8}", entry.d_ptr), Address.Ptr32(entry.d_ptr));
             break;
         case DT_INIT:
             formatter.Write("{0,-12} ", "DT_INIT");
             formatter.WriteHyperlink(string.Format("{0:X8}", entry.d_ptr), Address.Ptr32(entry.d_ptr));
             break;
         case DT_JMPREL:
             formatter.Write("{0,-12} ", "DT_JMPREL");
             formatter.WriteHyperlink(string.Format("{0:X8}", entry.d_ptr), Address.Ptr32(entry.d_ptr));
             break;
         case DT_NEEDED:
             formatter.Write("{0,-12} {1}", "DT_NEEDED", loader.ReadAsciiString(loader.RawImage, strtabSection.sh_offset + entry.d_ptr));
             break;
         case DT_STRSZ:
             formatter.Write("{0,-12} {1:X}", "DT_STRSZ", entry.d_val);
             break;
         case DT_STRTAB:
             formatter.Write("{0,-12} ", "DT_STRTAB");
             formatter.WriteHyperlink(string.Format("{0:X8}", entry.d_ptr), Address.Ptr32(entry.d_ptr));
             break;
         case DT_SYMENT:
             formatter.Write("{0,-12} {1}", "DT_SYMENTTRTAB", entry.d_val);
             break;
         case DT_SYMTAB:
             formatter.Write("{0,-12} ", "DT_SYMTAB");
             formatter.WriteHyperlink(string.Format("{0:X8}", entry.d_ptr), Address.Ptr32(entry.d_ptr));
             break;
         }
         formatter.WriteLine();
     }
 }
Esempio n. 2
0
 public override void Render(ImageMapSegment segment, Program program, Formatter formatter)
 {
     var entries = shdr.sh_size / shdr.sh_entsize;
     var symtab = (int)shdr.sh_link;
     var rdr = loader.CreateReader(shdr.sh_offset);
     for (int i = 0; i < entries; ++i)
     {
         uint iName;
         if (!rdr.TryReadUInt32(out iName))
             return;
         uint value;
         if (!rdr.TryReadUInt32(out value))
             return;
         uint size;
         if (!rdr.TryReadUInt32(out size))
             return;
         byte info;
         if (!rdr.TryReadByte(out info))
             return;
         byte other;
         if (!rdr.TryReadByte(out other))
             return;
         ushort shIndex;
         if (!rdr.TryReadUInt16(out shIndex))
             return;
         string symStr = loader.GetStrPtr(symtab, iName);
         string segName = loader.GetSectionName(shIndex);
         formatter.Write("{0,-40} {1:X8} {2:X8} {3:X2} {4}", symStr, value, size, info & 0xFF, segName);
         formatter.WriteLine();
     }
 }
Esempio n. 3
0
        public override void Render(ImageSegment segment, Program program, Formatter formatter)
        {
            var entries = shdr.Size / shdr.EntrySize;
            var symtab = shdr.LinkedSection;
            var rdr = loader.CreateReader(shdr.FileOffset);
            for (ulong i = 0; i < entries; ++i)
            {
                uint offset;
                if (!rdr.TryReadUInt32(out offset))
                    return;
                uint info;
                if (!rdr.TryReadUInt32(out info))
                    return;

                uint sym = info >> 8;
                string symStr = loader.GetSymbolName(symtab, sym);
                formatter.Write("{0:X8} {1,3} {2:X8} {3}", offset, info & 0xFF, sym, symStr);
                formatter.WriteLine();
            }
        }
Esempio n. 4
0
        public override void Render(ImageMapSegment segment, Program program, Formatter formatter)
        {
            var entries = shdr.sh_size / shdr.sh_entsize;
            var symtab = (int)shdr.sh_link;
            var rdr = loader.CreateReader(shdr.sh_offset);
            for (int i = 0; i < entries; ++i)
            {
                uint offset;
                if (!rdr.TryReadUInt32(out offset))
                    return;
                uint info;
                if (!rdr.TryReadUInt32(out info))
                    return;

                uint sym = info >> 8;
                string symStr = loader.GetSymbol(symtab, (int)sym);
                formatter.Write("{0:X8} {1,3} {2:X8} {3}", offset, info & 0xFF, sym, symStr);
                formatter.WriteLine();
            }
        }
Esempio n. 5
0
        private void WriteGlobalVariable(StructureField field)
        {
            var name = program.NamingPolicy.GlobalName(field);
            var addr = Address.Ptr32((uint)field.Offset);  //$BUG: this is completely wrong; field.Offsets should be as wide as the platform permits.
            var oneLineDeclaration = IsOneLineDeclaration(field.DataType);

            try
            {
                tw.WriteDeclaration(field.DataType, name);
                if (program.SegmentMap.IsValidAddress(addr))
                {
                    formatter.Write(" = ");
                    if (!oneLineDeclaration && showAddressInComment)
                    {
                        formatter.Write("// {0}", addr);
                    }
                    this.rdr = program.CreateImageReader(program.Architecture, addr);
                    field.DataType.Accept(this);
                }
            }
            catch (Exception ex)
            {
                var dc = services.RequireService <DecompilerEventListener>();
                dc.Error(
                    dc.CreateAddressNavigator(program, addr),
                    ex,
                    "Failed to write global variable {0}.", name);
                formatter.Terminate(";");
                return;
            }
            if (oneLineDeclaration && showAddressInComment)
            {
                formatter.Write("; // {0}", addr);
                formatter.Terminate();
            }
            else
            {
                formatter.Terminate(";");
            }
        }
Esempio n. 6
0
        //00 00 00 01 00 00 00 01   needed      ........
        //00 00 00 01 00 00 02 DE   "
        //00 00 00 01 00 00 02 EE   "
        //00 00 00 01 00 00 03 06   "
        //00 00 00 01 00 00 03 10   "
        //00 00 00 01 00 00 03 6B   "
        //00 00 00 01 00 00 03 91   "
        //00 00 00 0C 10 00 29 CC   Init
        //00 00 00 0D 10 03 81 B0   Fini
        //00 00 00 04 10 00 01 A8   Hash
        //6F FF FE F5 10 00 07 E0   ?
        //00 00 00 05 10 00 14 AC   strtab
        //00 00 00 06 10 00 08 3C   symtab
        //00 00 00 0A 00 00 09 90   strsz
        //00 00 00 0B 00 00 00 10   syment
        //00 00 00 15 00 00 00 00   debug
        //00 00 00 03 10 06 60 00   pltgot   -> first byte in linkage table
        //00 00 00 02 00 00 08 D0   pltretsz
        //00 00 00 14 00 00 00 07   pltrel
        //00 00 00 17 10 00 20 FC   jmprel
        //70 00 00 00 10 06 5F F4   loproc
        //00 00 00 07 10 00 20 CC   rela
        //00 00 00 08 00 00 09 00   relasz
        //00 00 00 09 00 00 00 0C   relaent
        //6F FF FF FE 10 00 1F CC  ?
        //6F FF FF FF 00 00 00 04   ?
        //6F FF FF F0 10 00 1E 3C  ?


        protected virtual void RenderEntry(string name, DtFormat format, Elf64_Dyn entry, Formatter formatter)
        {
            formatter.Write("{0,-15} ", name);
            switch (format)
            {
            default:
            case DtFormat.Hexadecimal:
                formatter.Write("{0:X16}", entry.d_val);
                break;
            case DtFormat.Decimal:
                formatter.Write("{0,16}", entry.d_val);
                break;
            case DtFormat.Address:
                formatter.WriteHyperlink(string.Format("{0:X16}", entry.d_ptr), Address.Ptr64(entry.d_ptr));
                break;
            case DtFormat.String:
                formatter.Write(loader.ReadAsciiString(strtabSection.FileOffset + entry.d_ptr));
                break;
            }
        }
Esempio n. 7
0
        public void Dump(Program program, Formatter formatter)
        {
            var map = program.SegmentMap;
            ImageSegment segment = null;
            foreach (ImageMapItem i in program.ImageMap.Items.Values)
            {
                if (!map.IsValidAddress(i.Address))
                    continue;
                ImageSegment seg;
                if (!map.TryFindSegment(i.Address, out seg))
                    continue;
                if (seg != segment)
                {
                    segment = seg;
                    formatter.WriteLine(";;; Segment {0} ({1})", seg.Name, seg.Address);
                }

                ImageMapBlock block = i as ImageMapBlock;
                if (block != null)
                {
                    formatter.WriteLine();
                    Procedure proc;
                    if (program.Procedures.TryGetValue(block.Address, out proc))
                    {
                        formatter.WriteComment(string.Format(
                            ";; {0}: {1}", proc.Name, block.Address));
                        formatter.WriteLine();

                        formatter.Write(proc.Name);
                        formatter.Write(" ");
                        formatter.Write("proc");
                        formatter.WriteLine();
                    }
                    else
                    {
                        formatter.Write(block.Block.Name);
                        formatter.Write(":");
                        formatter.WriteLine();
                    }
                    DumpAssembler(program.SegmentMap, block.Address, block.Address + block.Size, formatter);
                    continue;
                }

                ImageMapVectorTable table = i as ImageMapVectorTable;
                if (table != null)
                {
                    formatter.WriteLine(";; Code vector at {0} ({1} bytes)",
                        table.Address, table.Size);
                    foreach (Address addr in table.Addresses)
                    {
                        formatter.WriteLine("\t{0}", addr != null ? addr.ToString() : "-- null --");
                    }
                    DumpData(program.SegmentMap, i.Address, i.Size, formatter);
                }
                else
                {
                    var segLast = segment.Address + segment.Size;
                    var size = segLast - i.Address;
                    size = Math.Min(i.Size, size);
                    if (i.DataType == null || i.DataType is UnknownType ||
                        i.DataType is CodeType)
                    {
                        DumpData(program.SegmentMap, i.Address, size, formatter);
                    }
                    else
                    {
                        DumpTypedData(program.SegmentMap, i, formatter);
                    }
                }
            }
        }
Esempio n. 8
0
 void CvQualifier(string cv)
 {
     fmt.Write(' ');
     fmt.Write(cv);
 }
Esempio n. 9
0
        public void WriteEntry(TypeVariable tv, Formatter writer)
        {
            writer.Write(tv.Name);
            writer.Write(":");
            WriteExpressionOf(tv, writer);
            writer.WriteLine();

            writer.Write("  Class: ");
            writer.WriteLine(tv.Class.Name);

            writer.Write("  DataType: ");
            writer.WriteLine(tv.DataType);

            writer.Write("  OrigDataType: ");
            writer.WriteLine(tv.OriginalDataType);
        }
Esempio n. 10
0
 private void WriteEntry(TypeVariable tv, DataType dt, Formatter writer)
 {
     writer.Write("{0}: ", tv);
     if (dt != null)
     {
         dt.Accept(new TypeGraphWriter(writer));
         WriteExpressionOf(tv, writer);
     }
     writer.WriteLine();
 }
Esempio n. 11
0
 private void ResetPresedence(int precedenceOld)
 {
     if (precedenceOld < precedenceCur ||
         (forceParensIfSamePrecedence && precedenceCur == precedenceOld))
     {
         writer.Write(")");
     }
     precedenceCur = precedenceOld;
 }
Esempio n. 12
0
        private void DumpTypedData(SegmentMap map, ImageMapItem item, Formatter stm)
        {
            ImageSegment segment;
            if (!map.TryFindSegment(item.Address, out segment) || segment.MemoryArea == null)
                return;
            stm.Write(Block.GenerateName(item.Address));
            stm.Write("\t");

            ImageReader rdr = arch.CreateImageReader(segment.MemoryArea, item.Address);
            item.DataType.Accept(new TypedDataDumper(rdr, item.Size, stm));
        }
Esempio n. 13
0
        public void Emit(string fnName, EmitFlags f, Formatter fmt, CodeFormatter w, TypeFormatter t)
        {
            bool emitStorage = (f & EmitFlags.ArgumentKind) == EmitFlags.ArgumentKind;
            if (emitStorage)
            {
                if (ReturnValue != null)
                {
                    w.WriteFormalArgumentType(ReturnValue, emitStorage);
                    fmt.Write(" ");
                }
                else
                {
                    fmt.Write("void ");
                }
                fmt.Write("{0}(", fnName);
            }
            else
            {
                if (ReturnValue == null)
                    fmt.Write("void {0}", fnName);
                else
                {
                    t.Write(ReturnValue.DataType, fnName);           //$TODO: won't work with fn's that return pointers to functions or arrays.
                }
                fmt.Write("(");
            }
            var sep = "";
            if (Parameters != null)
            {
                for (int i = 0; i < Parameters.Length; ++i)
                {
                    fmt.Write(sep);
                    sep = ", ";
                    w.WriteFormalArgument(Parameters[i], emitStorage, t);
                }
            }
            fmt.Write(")");

            if ((f & EmitFlags.LowLevelInfo) == EmitFlags.LowLevelInfo)
            {
                fmt.WriteLine();
                fmt.Write("// stackDelta: {0}; fpuStackDelta: {1}; fpuMaxParam: {2}", StackDelta, FpuStackDelta, FpuStackArgumentMax);
                fmt.WriteLine();
            }
        }
Esempio n. 14
0
 public override Formatter Write(char ch)
 {
     WritePrefix();
     return(w.Write(ch));
 }
Esempio n. 15
0
 public void Tab()
 {
     ++chars;
     formatter.Write("\t");
 }
Esempio n. 16
0
        public override void Render(ImageMapSegment segment, Program program, Formatter formatter)
        {
            var entries = shdr.sh_size / shdr.sh_entsize;
            var symtab = (int)shdr.sh_link;
            var rdr = loader.CreateReader(shdr.sh_offset);
            for (ulong i = 0; i < entries; ++i)
            {
                ulong offset;
                if (!rdr.TryReadUInt64(out offset))
                    return;
                ulong info;
                if (!rdr.TryReadUInt64(out info))
                    return;
                ulong addend;
                if (!rdr.TryReadUInt64(out addend))
                    return;

                ulong sym = info >> 32;
                string symStr = loader.GetSymbol64(symtab, (int)sym);
                formatter.Write("{0:X8} {1,3} {2:X8} {3:X16} {4} ({5})", offset, info & 0xFFFFFFFF, sym, addend, symStr, sym);
                formatter.WriteLine();
            }
        }
Esempio n. 17
0
        public void VisitPointer(Pointer ptr)
        {
            switch (ptr.Size)
            {
            case 2:
                fmt.WriteKeyword("dw");
                fmt.Write("\t");
                fmt.Write(string.Format("0x{0:X4}", rdr.ReadByte()));
                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;
            }
        }
Esempio n. 18
0
 public void WriteExpressionOf(TypeVariable tvMember, Formatter writer)
 {
     Expression e;
     if (tvSources.TryGetValue(tvMember, out e) && e != null)
     {
         writer.Write(" (in {0}", e);
         if (e.DataType != null)
         {
             writer.Write(" : ");
             writer.Write(e.DataType);
         }
         writer.Write(")");
     }
 }
Esempio n. 19
0
		public void DumpData(SegmentMap map, Address address, long cbBytes, Formatter stm)
		{
			ulong cSkip = address.ToLinear() & 0x0F;
            ImageSegment segment;
            if (!map.TryFindSegment(address, out segment) || segment.MemoryArea == null)
                return;
			ImageReader rdr = arch.CreateImageReader(segment.MemoryArea, address);
			while (cbBytes > 0)
			{
				StringBuilder sb = new StringBuilder(0x12);
				try 
				{
					stm.Write("{0} ", rdr.Address);
					for (int i = 0; i < 16; ++i)
					{
						if (cbBytes > 0 && cSkip == 0)
						{
							byte b = rdr.ReadByte();
							stm.Write("{0:X2} ", b);
							sb.Append(0x20 <= b && b < 0x7F
								? (char) b
								: '.');
							--cbBytes;
						}
						else
						{
							stm.Write("   ");
							if (cSkip > 0)
								sb.Append(' ');
							--cSkip;
						}
					}
				} 
				catch
				{
					stm.WriteLine();
					stm.WriteLine(";;; ...end of image");
					return;
				}
				stm.WriteLine(sb.ToString());
			}
		}