Example #1
0
        public SerializedType VisitRangeType(RangeType rangeType)
        {
            var low   = rangeType.Low.Accept(ceval).ToInt64();
            var hi    = rangeType.High.Accept(ceval).ToInt64();
            var delta = hi - low + 1;

            if (delta < 0)
            {
                throw new NotImplementedException("Range overflow.");
            }
            if (delta < 256)
            {
                return(low < 0
                    ? PrimitiveType_v1.SChar8()
                    : PrimitiveType_v1.UChar8());
            }
            if (delta < 65536)
            {
                return(low <= 0
                    ? PrimitiveType_v1.Int16()
                    : PrimitiveType_v1.UInt16());
            }
            if (delta < (1L << 32))
            {
                return(low <= 0
                    ? PrimitiveType_v1.Int32()
                    : PrimitiveType_v1.UInt32());
            }
            return(PrimitiveType_v1.Int64());
        }
Example #2
0
        public void Tlldr_typedef_forwarded_struct_size()
        {
            Given_ArchitectureStub();

            var typelib = new TypeLibrary();
            var tlldr   = new TypeLibraryDeserializer(platform.Object, true, typelib);

            new SerializedTypedef
            {
                Name     = "_sized_struct",
                DataType = new StructType_v1
                {
                    Name = "sized_struct",
                }
            }.Accept(tlldr);
            new StructType_v1
            {
                Name   = "sized_struct",
                Fields = new StructField_v1[]
                {
                    new StructField_v1 {
                        Name   = "foo",
                        Offset = 0,
                        Type   = PrimitiveType_v1.Int64(),
                    }
                },
                ByteSize = 8,
            }.Accept(tlldr);

            var str      = (StructureType)typelib.Types["_sized_struct"];
            var expected = @"(struct ""sized_struct"" 0008 (0 int64 foo))";

            Assert.AreEqual(expected, str.ToString());
        }
Example #3
0
        private SerializedType Type()
        {
            switch (str[i++])
            {
            case 'b': return(PrimitiveType_v1.Bool());

            case 'c': return(PrimitiveType_v1.Char8());

            case 'h': return(PrimitiveType_v1.UChar8());

            case 's': return(PrimitiveType_v1.Int16());

            case 'r': return(PrimitiveType_v1.UInt16());

            case 'i': return(PrimitiveType_v1.Int32());

            case 'j': return(PrimitiveType_v1.UInt32());

            case 'l': return(PrimitiveType_v1.Int64());

            case 'm': return(PrimitiveType_v1.UInt64());

            case 'w': return(PrimitiveType_v1.WChar16());

            case 'f': return(PrimitiveType_v1.Real32());

            case 'd': return(PrimitiveType_v1.Real64());

            case 'P': return(new PointerType_v1 {
                    DataType = Type(), PointerSize = ptrSize
                });

            case 'R': return(new ReferenceType_v1 {
                    Referent = Type(), Size = ptrSize
                });

            default:
                --i;
                if (char.IsDigit(str[i]))
                {
                    return(new TypeReference_v1
                    {
                        TypeName = SimpleName(),
                    });
                }
                throw new NotImplementedException(string.Format("Unknown GCC type code '{0}'.", str[i]));
            }
        }
Example #4
0
        private SerializedType Type()
        {
            var qual = CvQualifier();

            switch (str[i++])
            {
            case 'v': return(new VoidType_v1());

            case 'b': return(Qualify(PrimitiveType_v1.Bool(), qual));

            case 'c': return(Qualify(PrimitiveType_v1.Char8(), qual));

            case 'h': return(Qualify(PrimitiveType_v1.UChar8(), qual));

            case 's': return(Qualify(PrimitiveType_v1.Int16(), qual));

            case 't': return(Qualify(PrimitiveType_v1.UInt16(), qual));

            case 'i': return(Qualify(PrimitiveType_v1.Int32(), qual));

            case 'j': return(Qualify(PrimitiveType_v1.UInt32(), qual));

            case 'l': return(Qualify(PrimitiveType_v1.Int64(), qual));

            case 'm': return(Qualify(PrimitiveType_v1.UInt64(), qual));

            case 'w': return(Qualify(PrimitiveType_v1.WChar16(), qual));

            case 'f': return(Qualify(PrimitiveType_v1.Real32(), qual));

            case 'd': return(Qualify(PrimitiveType_v1.Real64(), qual));

            case 'F': --i; return(FunctionType());

            case 'N': --i; return(CreateTypeReference(NestedName()));

            case 'P':
                var ptr = new PointerType_v1 {
                    DataType = Type(), PointerSize = ptrSize
                };
                AddSubstitution(ptr);
                return(Qualify(ptr, qual));

            case 'R':
                qual = CvQualifier();
                var r = new ReferenceType_v1 {
                    Referent = Type(), Size = ptrSize
                };
                return(Qualify(r, qual));

            case 'S':
                switch (str[i++])
                {
                case 't':
                    return(Qualify(new TypeReference_v1
                    {
                        Scope = new[] { "std" },
                        TypeName = Type().ToString(),
                    }, qual));

                case 's':
                    return(Qualify(new TypeReference_v1
                    {
                        Scope = new[] { "std" },
                        TypeName = "string"
                    }, qual));

                default:
                    int iStart = --i;
                    while (str[i] != '_')
                    {
                        ++i;
                    }
                    ++i;
                    var sub = str.Substring(iStart, i - iStart);
                    Debug.Print(sub);
                    return(Qualify((SerializedType)substitutions[sub], qual));
                }
                throw new NotImplementedException();

            default:
                --i;
                if (char.IsDigit(str[i]))
                {
                    var tref = new TypeReference_v1
                    {
                        TypeName = UnqualifiedName()
                    };
                    AddSubstitution(tref);
                    return(Qualify(tref, qual));
                }
                throw new NotImplementedException(string.Format("Unknown GCC type code '{0}' ({1}).", str[i], str.Substring(i)));
            }
        }
Example #5
0
        private bool LoadParameter(SerializedSignature ssig, List <Argument_v1> args)
        {
            if (PeekAndDiscard(TokenType.NUMBER))
            {
                return(true);
            }
            Token tok = Peek();

            if (tok.Type != TokenType.ID)
            {
                return(false);
            }
            Get();
            SerializedType type = null;

            switch (tok.Value)
            {
            case "word":
            case "s_word":
                type = new PrimitiveType_v1(PrimitiveType.Word16.Domain, 2);
                break;

            case "long":
                type = new PrimitiveType_v1(PrimitiveType.Word32.Domain, 4);
                break;

            //$TODO: need SegmentedPointerType
            case "segptr":
            case "segstr":
                type = new PrimitiveType_v1(Domain.SegPointer, 4);
                break;

            case "ptr":
                type = new PrimitiveType_v1(Domain.Pointer, 4);
                break;

            case "str":
                type = PointerType_v1.Create(PrimitiveType_v1.Char8(), 4);
                break;

            case "wstr":
                type = PointerType_v1.Create(PrimitiveType_v1.WChar16(), 4);
                break;

            case "uint16":
                type = PrimitiveType_v1.UInt16();
                break;

            case "uint32":
                type = PrimitiveType_v1.UInt32();
                break;

            case "uint64":
                type = PrimitiveType_v1.UInt64();
                break;

            case "int16":
                type = PrimitiveType_v1.Int16();
                break;

            case "int32":
                type = PrimitiveType_v1.Int32();
                break;

            case "int64":
                type = PrimitiveType_v1.Int64();
                break;

            default: throw new Exception("Unknown: " + tok.Value);
            }
            args.Add(new Argument_v1 {
                Type = type
            });
            return(true);
        }
Example #6
0
        private SerializedType Type()
        {
            switch (str[i++])
            {
            case 'v': return(new VoidType_v1());

            case 'b': return(PrimitiveType_v1.Bool());

            case 'c': return(PrimitiveType_v1.Char8());

            case 'h': return(PrimitiveType_v1.UChar8());

            case 's': return(PrimitiveType_v1.Int16());

            case 't': return(PrimitiveType_v1.UInt16());

            case 'i': return(PrimitiveType_v1.Int32());

            case 'j': return(PrimitiveType_v1.UInt32());

            case 'l': return(PrimitiveType_v1.Int64());

            case 'm': return(PrimitiveType_v1.UInt64());

            case 'w': return(PrimitiveType_v1.WChar16());

            case 'f': return(PrimitiveType_v1.Real32());

            case 'd': return(PrimitiveType_v1.Real64());

            case 'F': --i; return(FunctionType());

            case 'N': --i; return(CreateTypeReference(NestedName()));

            case 'P':
                var ptr = new PointerType_v1 {
                    DataType = Type(), PointerSize = ptrSize
                };
                AddSubstitution(ptr);
                return(ptr);

            case 'R':
                //$TODO: Reko doesn't have a concept of 'const' or 'volatile'.
                // Needs to be implemented for completeness, but should not affect
                // quality of decompilation.
                var qual = CvQualifier();
                return(new ReferenceType_v1 {
                    Referent = Type(), Size = ptrSize
                });

            case 'S':
                switch (str[i++])
                {
                case 't':
                    return(new TypeReference_v1
                    {
                        Scope = new[] { "std" },
                        TypeName = Type().ToString(),
                    });

                case 's':
                    return(new TypeReference_v1
                    {
                        Scope = new[] { "std" },
                        TypeName = "string"
                    });

                default:
                    int iStart = --i;
                    while (str[i] != '_')
                    {
                        ++i;
                    }
                    ++i;
                    var sub = str.Substring(iStart, i - iStart);
                    Debug.Print(sub);
                    return((SerializedType)substitutions[sub]);
                }
                throw new NotImplementedException();

            default:
                --i;
                if (char.IsDigit(str[i]))
                {
                    var tref = new TypeReference_v1
                    {
                        TypeName = UnqualifiedName()
                    };
                    AddSubstitution(tref);
                    return(tref);
                }
                throw new NotImplementedException(string.Format("Unknown GCC type code '{0}' ({1}).", str[i], str.Substring(i)));
            }
        }
Example #7
0
        private SerializedType Type()
        {
            switch (str[i++])
            {
            case 'b': return(PrimitiveType_v1.Bool());

            case 'c': return(PrimitiveType_v1.Char8());

            case 'h': return(PrimitiveType_v1.UChar8());

            case 's': return(PrimitiveType_v1.Int16());

            case 'r': return(PrimitiveType_v1.UInt16());

            case 'i': return(PrimitiveType_v1.Int32());

            case 'j': return(PrimitiveType_v1.UInt32());

            case 'l': return(PrimitiveType_v1.Int64());

            case 'm': return(PrimitiveType_v1.UInt64());

            case 'w': return(PrimitiveType_v1.WChar16());

            case 'f': return(PrimitiveType_v1.Real32());

            case 'd': return(PrimitiveType_v1.Real64());

            case 'P': return(new PointerType_v1 {
                    DataType = Type(), PointerSize = ptrSize
                });

            case 'R':
                //$TODO: Reko doesn't have a concept of 'const' or 'volatile'.
                // Needs to be implemented for completeness, but should not affect
                // quality of decompilation.
                var qual = CvQualifier();
                return(new ReferenceType_v1 {
                    Referent = Type(), Size = ptrSize
                });

            case 'S':
                switch (str[i++])
                {
                case 't':
                    return(new TypeReference_v1
                    {
                        Scope = new[] { "std" },
                        TypeName = Type().ToString(),
                    });

                case 's':
                    return(new TypeReference_v1
                    {
                        Scope = new[] { "std" },
                        TypeName = "string"
                    });
                }
                throw new NotImplementedException();

            default:
                --i;
                if (char.IsDigit(str[i]))
                {
                    return(new TypeReference_v1
                    {
                        TypeName = UnqualifiedName(),
                    });
                }
                throw new NotImplementedException(string.Format("Unknown GCC type code '{0}'.", str[i]));
            }
        }