Exemplo n.º 1
0
        public string MangleType(TypeSpec ts)
        {
            StringBuilder sb = new StringBuilder("_Z");
            ManglerState  ms = new ManglerState();

            MangleType(ts, sb, ms);

            return(sb.ToString());
        }
Exemplo n.º 2
0
        public TypeSpec DemangleType(string s)
        {
            ManglerState ms = new ManglerState();

            int i = 0;

            if (!Munch(s, ref i, "_Z"))
            {
                throw new ArgumentException();
            }
            return(DemangleType(s, ref i, ms));
        }
Exemplo n.º 3
0
        public static string MangleMethod(System.Reflection.MethodInfo method)
        {
            StringBuilder sb = new StringBuilder("_Z");
            ManglerState  ms = new ManglerState();

            MangleType(method.DeclaringType as TysosType, sb, ms);
            sb.Append("M_");
            if ((method.CallingConvention & System.Reflection.CallingConventions.VarArgs) == System.Reflection.CallingConventions.VarArgs)
            {
                throw new Exception("Unsupported calling convention (VarArgs)");
            }
            else
            {
                sb.Append("0");
            }

            sb.Append("_");
            AppendStringWithLength(sb, EncodeString(method.Name));

            sb.Append("_R");
            MangleType(method.ReturnType as TysosType, sb, ms);

            sb.Append("_P");
            System.Reflection.ParameterInfo[] ps = method.GetParameters();
            bool has_this = false;

            if ((method.CallingConvention & System.Reflection.CallingConventions.HasThis) == System.Reflection.CallingConventions.HasThis)
            {
                has_this = true;
            }
            if ((method.CallingConvention & System.Reflection.CallingConventions.ExplicitThis) == System.Reflection.CallingConventions.ExplicitThis)
            {
                has_this = false;
            }
            sb.Append(ps.Length + (has_this ? 1 : 0));
            if (has_this)
            {
                sb.Append("u1t");
            }
            foreach (System.Reflection.ParameterInfo p in ps)
            {
                MangleType(p.ParameterType as TysosType, sb, ms);
            }

            // TODO: determine if this is a generic method

            return(sb.ToString());
        }
Exemplo n.º 4
0
        private void MangleTypeSig(ref int type_idx, StringBuilder sb, ManglerState ms)
        {
            uint p_token;
            int  p_type = GetType(ref type_idx, out p_token);

            switch (p_type)
            {
            case 0x1d:
                /* SZARRAY */
                sb.Append("u1Z");
                MangleTypeSig(ref type_idx, sb, ms);
                break;

            default:
                MangleTypeSig(p_type, p_token, sb, ms);
                break;
            }
        }
Exemplo n.º 5
0
        public Spec DemangleObject(string s)
        {
            ManglerState ms = new ManglerState();

            int i = 0;

            if (!Munch(s, ref i, "_Z"))
            {
                throw new ArgumentException();
            }

            var ts = DemangleType(s, ref i, ms);

            if (!Munch(s, ref i, "_"))
            {
                return(ts);
            }

            return(DemangleMethod(s, ref i, ms, ts, s));
        }
Exemplo n.º 6
0
        private MethodSpec DemangleMethod(string s, ref int i, ManglerState ms, TypeSpec ts, string mangled_name)
        {
            MethodSpec ret = new MethodSpec();

            ret.m               = ts.m;
            ret.type            = ts;
            ret.mangle_override = mangled_name;

            ret.name_override = MunchString(s, ref i);

            if (Munch(s, ref i, "_g"))
            {
                var gm_count = MunchNumber(s, ref i);
                ret.gmparams = new TypeSpec[gm_count];
                for (int idx = 0; idx < gm_count; idx++)
                {
                    ret.gmparams[idx] = DemangleType(s, ref i, ms);
                }
            }

            if (!Munch(s, ref i, "_R"))
            {
                throw new ArgumentException();
            }

            var rettype = DemangleType(s, ref i, ms);

            if (!Munch(s, ref i, "_P"))
            {
                throw new ArgumentException();
            }

            var pcount = MunchNumber(s, ref i);
            var ps     = new TypeSpec[pcount];

            for (int idx = 0; idx < pcount; idx++)
            {
                ps[idx] = DemangleType(s, ref i, ms);
            }
            return(ret);
        }
Exemplo n.º 7
0
        /** <summary>Returns true if the provided string is a mangled method, false if a type and an exception otherwise</summary> */
        public bool IsMangledMethod(string s)
        {
            ManglerState ms = new ManglerState();

            int i = 0;

            if (!Munch(s, ref i, "_Z"))
            {
                throw new ArgumentException();
            }

            try
            {
                var ts = DemangleType(s, ref i, ms);
            }
            catch (Exception)
            {
                return(false);
            }

            var ret = Munch(s, ref i, "_");

            return(ret);
        }
Exemplo n.º 8
0
        static void MangleType(TysosType t, StringBuilder sb, ManglerState ms)
        {
            bool cont = true;

            while (cont)
            {
                if (t.IsBoxed)
                {
                    sb.Append("B");
                    t = t.GetUnboxedType();
                }
                else if (t.IsManagedPointer)
                {
                    sb.Append("R");
                    t = t.GetUnboxedType();
                }
                else if (t.IsUnmanagedPointer)
                {
                    sb.Append("P");
                    t = t.GetUnboxedType();
                }
                else if (t.IsZeroBasedArray)
                {
                    sb.Append("u1Z");
                    MangleType(t.GetElementType() as TysosType, sb, ms);
                    return;
                }
                else if (t.IsArray)
                {
                    sb.Append("u1A");
                    MangleType(t.GetElementType() as TysosType, sb, ms);
                    sb.Append(t.GetArrayRank());
                    sb.Append("_");
                    // TODO: work out how to extract array lobounds and sizes
                    return;
                }
                else
                {
                    cont = false;
                }
            }

            if (t.IsSimpleType)
            {
                switch (t.SimpleTypeElementType)
                {
                case 1:
                    // void
                    sb.Append("v");
                    break;

                case 2:
                    // bool
                    sb.Append("b");
                    break;

                case 3:
                    // char
                    sb.Append("c");
                    break;

                case 4:
                    // i1
                    sb.Append("a");
                    break;

                case 5:
                    // u1
                    sb.Append("h");
                    break;

                case 6:
                    // i2
                    sb.Append("s");
                    break;

                case 7:
                    // u2
                    sb.Append("t");
                    break;

                case 8:
                    // i4
                    sb.Append("i");
                    break;

                case 9:
                    // u4
                    sb.Append("j");
                    break;

                case 0xa:
                    // i8
                    sb.Append("x");
                    break;

                case 0xb:
                    // u8
                    sb.Append("y");
                    break;

                case 0xc:
                    // r4
                    sb.Append("f");
                    break;

                case 0xd:
                    // r8
                    sb.Append("d");
                    break;

                case 0xe:
                    // string
                    sb.Append("u1S");
                    break;

                case 0x18:
                    // intptr
                    sb.Append("u1I");
                    break;

                case 0x19:
                    // uintptr
                    sb.Append("u1U");
                    break;

                case 0x1c:
                    // object
                    sb.Append("u1O");
                    break;

                case 0x16:
                    // TypedByRef
                    sb.Append("u1T");
                    break;

                case 0xfe:
                    // VirtFtnPtr
                    sb.Append("u1V");
                    break;

                case 0xfd:
                    // UninstantiatedGenericParam
                    sb.Append("u1P");
                    break;

                case 0xfc:
                    // RefGenericParam
                    sb.Append("u1R");
                    break;

                default:
                    throw new NotSupportedException();
                }
            }
            else if (t.IsGenericType)
            {
                MangleType(t.GetGenericTypeDefinition() as TysosType, sb, ms);
                sb.Append("_G");
                sb.Append(t.GetGenericArguments().Length);
                foreach (Type p in t.GetGenericArguments())
                {
                    MangleType(p as TysosType, sb, ms);
                }
            }
            else if (t.IsUninstantiatedGenericTypeParameter)
            {
                sb.Append("u1G");
                sb.Append(t.UgtpIdx);
            }
            else if (t.IsUninstantiatedGenericMethodParameter)
            {
                sb.Append("u1g");
                sb.Append(t.UgmpIdx);
            }
            else
            {
                string mod    = EncodeString(t.Module.Name);
                string nspace = EncodeString(t.Namespace);
                string name   = EncodeString(t.Name);

                if (mod == ms.cur_module)
                {
                    if (nspace == ms.cur_nspace)
                    {
                        sb.Append("V");
                        AppendStringWithLength(sb, name);
                    }
                    else
                    {
                        sb.Append("U");
                        AppendStringWithLength(sb, nspace);
                        AppendStringWithLength(sb, name);
                        ms.cur_nspace = nspace;
                    }
                }
                else if (mod == "mscorlib")
                {
                    sb.Append("W");
                    AppendStringWithLength(sb, nspace);
                    AppendStringWithLength(sb, name);
                    ms.cur_module = mod;
                    ms.cur_nspace = nspace;
                }
                else if ((mod == "libsupcs") && (nspace == "libsupcs"))
                {
                    sb.Append("X");
                    AppendStringWithLength(sb, name);
                    ms.cur_module = mod;
                    ms.cur_nspace = nspace;
                }
                else
                {
                    sb.Append("N");
                    AppendStringWithLength(sb, mod);
                    AppendStringWithLength(sb, nspace);
                    AppendStringWithLength(sb, name);
                    ms.cur_module = mod;
                    ms.cur_nspace = nspace;
                }
            }
        }
Exemplo n.º 9
0
        private void MangleTypeDef(int td_idx, StringBuilder sb, ManglerState ms)
        {
            // Is this a simple type?
            if (is_corlib && simple_type_idx[td_idx] != -1)
            {
                MangleTypeSig(simple_type_idx[td_idx], 0, sb, ms);
                return;
            }

            // Get module, type name
            string mod = EncodeString(GetStringEntry(tid_Module, 1, 1));

            if (mod == "CommonLanguageRuntimeLibrary")
            {
                /* This is the framework-agnostic .netstandard 2.0 way of referring to
                 * either mscorlib.dll or System.Private.CoreLib.dll
                 *
                 * We rename the latter to the former in the build script, so do the
                 * same here */
                mod = "mscorlib#2Edll";
            }

            // Get namespace of outermost enclosing type
            int outermost = td_idx;

            while (nested_parent[outermost] != 0)
            {
                outermost = nested_parent[outermost];
            }
            string nspace = EncodeString(GetStringEntry(tid_TypeDef, outermost, 2));

            StringBuilder name_sb = new StringBuilder();

            AppendEnclosingType(td_idx, name_sb);
            string name = EncodeString(name_sb.ToString());

            if (mod == ms.cur_module)
            {
                if (nspace == ms.cur_nspace)
                {
                    sb.Append("V");
                    AppendStringWithLength(sb, name);
                }
                else
                {
                    sb.Append("U");
                    AppendStringWithLength(sb, nspace);
                    AppendStringWithLength(sb, name);
                    ms.cur_nspace = nspace;
                }
            }
            else if (mod == "mscorlib#2Edll")
            {
                sb.Append("W");
                AppendStringWithLength(sb, nspace);
                AppendStringWithLength(sb, name);
                ms.cur_module = mod;
                ms.cur_nspace = nspace;
            }
            else if ((mod == "libsupcs#2E") && (nspace == "libsupcs#2E"))
            {
                sb.Append("X");
                AppendStringWithLength(sb, name);
                ms.cur_module = mod;
                ms.cur_nspace = nspace;
            }
            else
            {
                sb.Append("N");
                AppendStringWithLength(sb, mod);
                AppendStringWithLength(sb, nspace);
                AppendStringWithLength(sb, name);
                ms.cur_module = mod;
                ms.cur_nspace = nspace;
            }
        }
Exemplo n.º 10
0
        private void MangleTypeSig(int ret_type, uint ret_token, StringBuilder sb, ManglerState ms)
        {
            switch (ret_type)
            {
            case 1:
                // void
                sb.Append("v");
                break;

            case 2:
                // bool
                sb.Append("b");
                break;

            case 3:
                // char
                sb.Append("c");
                break;

            case 4:
                // i1
                sb.Append("a");
                break;

            case 5:
                // u1
                sb.Append("h");
                break;

            case 6:
                // i2
                sb.Append("s");
                break;

            case 7:
                // u2
                sb.Append("t");
                break;

            case 8:
                // i4
                sb.Append("i");
                break;

            case 9:
                // u4
                sb.Append("j");
                break;

            case 0xa:
                // i8
                sb.Append("x");
                break;

            case 0xb:
                // u8
                sb.Append("y");
                break;

            case 0xc:
                // r4
                sb.Append("f");
                break;

            case 0xd:
                // r8
                sb.Append("d");
                break;

            case 0xe:
                // string
                sb.Append("u1S");
                break;

            case 0x11:
                // ValueType
                sb.Append("u1L");
                break;

            case 0x18:
                // intptr
                sb.Append("u1I");
                break;

            case 0x19:
                // uintptr
                sb.Append("u1U");
                break;

            case 0x1c:
                // object
                sb.Append("u1O");
                break;

            case 0x16:
                // TypedByRef
                sb.Append("u1T");
                break;

            default:
                throw new NotSupportedException();
            }
        }
Exemplo n.º 11
0
        private string MangleMethod(TypeSpec t, string mname, int msig,
                                    TypeSpec[] gtparams        = null, TypeSpec[] gmparams = null,
                                    bool is_spec               = false, bool is_boxed      = false,
                                    bool ret_type_needs_boxing = false)
        {
            StringBuilder sb = new StringBuilder("_Z");
            ManglerState  ms = new ManglerState();

            if (msig == 0)
            {
                throw new Exception("invalid method signature");
            }

            if (gtparams == null)
            {
                gtparams = t.gtparams;
            }

            /* Get declaring type */
            MangleType(t, sb, ms);

            sb.Append("_");

            /* Get method name */
            AppendStringWithLength(sb, mname);

            /* Generic method params */
            if (gmparams != null)
            {
                sb.Append("_g");
                sb.Append(gmparams.Length.ToString());
                foreach (var gmp in gmparams)
                {
                    MangleType(gmp, sb, ms);
                }
            }

            if (is_spec)
            {
                sb.Append("_MS");
            }

            /* Return type */
            sb.Append("_R");
            int ret_idx = GetMethodDefSigRetTypeIndex(msig);
            var ret_ts  = GetTypeSpec(ref ret_idx, gtparams,
                                      gmparams);

            MangleType(ret_ts, sb, ms);

            /* Params */
            sb.Append("_P");
            var pcount     = GetMethodDefSigParamCount(msig);
            var pcountthis = GetMethodDefSigParamCountIncludeThis(msig);

            sb.Append(pcountthis);
            if (pcountthis != pcount)
            {
                sb.Append("u1t");
            }
            for (int i = 0; i < pcount; i++)
            {
                var p_ts = GetTypeSpec(ref ret_idx, gtparams, gmparams);
                MangleType(p_ts, sb, ms);
            }

            if (is_boxed)
            {
                sb.Append("B");
            }
            if (ret_type_needs_boxing)
            {
                sb.Append("b");
            }

            return(sb.ToString());
        }
Exemplo n.º 12
0
        void MangleType(TypeSpec ts, StringBuilder sb,
                        ManglerState ms)
        {
            if (ts == null)
            {
                sb.Append("v");
                return;
            }
            switch (ts.stype)
            {
            case TypeSpec.SpecialType.None:
                ts.m.MangleTypeDef(ts.tdrow, sb, ms);
                if (ts.IsGeneric && ts.gtparams != null)
                {
                    sb.Append("_G");
                    sb.Append(ts.gtparams.Length.ToString());
                    foreach (var gtparam in ts.gtparams)
                    {
                        MangleType(gtparam, sb, ms);
                    }
                }
                break;

            case TypeSpec.SpecialType.SzArray:
                sb.Append("u1Z");
                MangleType(ts.other, sb, ms);
                break;

            case TypeSpec.SpecialType.Ptr:
                sb.Append("P");
                MangleType(ts.other, sb, ms);
                break;

            case TypeSpec.SpecialType.MPtr:
                sb.Append("R");
                MangleType(ts.other, sb, ms);
                break;

            case TypeSpec.SpecialType.Var:
                sb.Append("u1P");
                sb.Append(ts.idx.ToString());
                break;

            case TypeSpec.SpecialType.MVar:
                sb.Append("u1p");
                sb.Append(ts.idx.ToString());
                break;

            case TypeSpec.SpecialType.Boxed:
                MangleType(ts.other, sb, ms);
                break;

            case TypeSpec.SpecialType.Array:
                sb.Append("u1A");
                MangleType(ts.other, sb, ms);
                sb.Append(ts.arr_rank.ToString());
                foreach (var lb in ts.arr_lobounds)
                {
                    sb.Append("_");
                    sb.Append(lb.ToString());
                }
                sb.Append("_");
                foreach (var s in ts.arr_sizes)
                {
                    sb.Append("_");
                    sb.Append(s.ToString());
                }
                break;

            default:
                throw new NotImplementedException();
            }
        }
Exemplo n.º 13
0
        private TypeSpec DemangleType(string s, ref int i, ManglerState ms)
        {
            string module = ms.cur_module, nspace = ms.cur_nspace, name;

            if (Munch(s, ref i, "P"))
            {
                return(new TypeSpec
                {
                    stype = TypeSpec.SpecialType.Ptr,
                    other = DemangleType(s, ref i, ms)
                });
            }
            else if (Munch(s, ref i, "R"))
            {
                return(new TypeSpec
                {
                    stype = TypeSpec.SpecialType.MPtr,
                    other = DemangleType(s, ref i, ms)
                });
            }
            else if (Munch(s, ref i, "u1Z"))
            {
                return(new TypeSpec
                {
                    stype = TypeSpec.SpecialType.SzArray,
                    other = DemangleType(s, ref i, ms)
                });
            }
            else if (Munch(s, ref i, "u1A"))
            {
                throw new NotImplementedException();
            }
            else if (Munch(s, ref i, "v"))
            {
                return(SystemVoid);
            }
            else if (Munch(s, ref i, "c"))
            {
                return(SystemChar);
            }
            else if (Munch(s, ref i, "b"))
            {
                throw new NotImplementedException(); //bool
            }
            else if (Munch(s, ref i, "a"))
            {
                return(SystemInt8);
            }
            else if (Munch(s, ref i, "h"))
            {
                return(SystemByte);
            }
            else if (Munch(s, ref i, "s"))
            {
                return(SystemInt16);
            }
            else if (Munch(s, ref i, "t"))
            {
                return(SystemUInt16);
            }
            else if (Munch(s, ref i, "i"))
            {
                return(SystemInt32);
            }
            else if (Munch(s, ref i, "j"))
            {
                return(SystemUInt32);
            }
            else if (Munch(s, ref i, "x"))
            {
                return(SystemInt64);
            }
            else if (Munch(s, ref i, "y"))
            {
                return(SystemUInt64);
            }
            else if (Munch(s, ref i, "f"))
            {
                throw new NotImplementedException(); //r4
            }
            else if (Munch(s, ref i, "d"))
            {
                throw new NotImplementedException(); //r8
            }
            else if (Munch(s, ref i, "u1I"))
            {
                return(SystemIntPtr);
            }
            else if (Munch(s, ref i, "u1U"))
            {
                throw new NotImplementedException(); //uintptr
            }
            else if (Munch(s, ref i, "u1S"))
            {
                return(SystemString);
            }
            else if (Munch(s, ref i, "u1O"))
            {
                return(SystemObject);
            }
            else if (Munch(s, ref i, "u1L"))
            {
                return(SystemValueType);
            }
            else if (Munch(s, ref i, "u1T"))
            {
                return(SystemTypedByRef);
            }
            else if (Munch(s, ref i, "N"))
            {
                module = MunchString(s, ref i);
                nspace = MunchString(s, ref i);
                name   = MunchString(s, ref i);
            }
            else if (Munch(s, ref i, "U"))
            {
                nspace = MunchString(s, ref i);
                name   = MunchString(s, ref i);
            }
            else if (Munch(s, ref i, "V"))
            {
                name = MunchString(s, ref i);
            }
            else if (Munch(s, ref i, "W"))
            {
                module = "mscorlib";
                nspace = MunchString(s, ref i);
                name   = MunchString(s, ref i);
            }
            else if (Munch(s, ref i, "X"))
            {
                module = "libsupcs";
                nspace = "libsupcs";
                name   = MunchString(s, ref i);
            }
            else if (Munch(s, ref i, "u1t"))
            {
                return(null);
            }
            else
            {
                throw new NotImplementedException();
            }

            if (Munch(s, ref i, "_G"))
            {
                throw new NotImplementedException();
            }

            ms.cur_module = module;
            ms.cur_nspace = nspace;

            var mod = al.GetAssembly(module);

            if (mod == null)
            {
                throw new DllNotFoundException(module);
            }
            return(mod.GetTypeSpec(nspace, name));
        }