コード例 #1
0
        public static int Execute(tCLIFile *pThis, int argc, /*char**/ byte **argp)
        {
            throw new System.NotImplementedException();

#if NO
            tThread *          pThread;
            /*HEAP_PTR*/ byte *args;
            int i;

            // Create a string array for the program arguments
            // Don't include the argument that is the program name.
            argc--;
            argp++;
            args = new string[argc];
            Heap.MakeUndeletable(args);
            for (i = 0; i < argc; i++)
            {
                /*HEAP_PTR*/ byte *arg = SystemString.FromCharPtrASCII(argp[i]);
                SystemArray.StoreElement(args, i, (byte *)&arg);
            }

            // Create the main application thread
            pThread = Thread();
            Thread.SetEntryPoint(pThread, pThis->pMetaData, pThis->entryPoint, (byte *)&args, sizeof(void *));

            return(Thread.Execute());
#endif
        }
コード例 #2
0
        public static tMetaData *GetMetaDataForLoadedAssembly(byte *pLoadedAssemblyName)
        {
            tFilesLoaded *pFiles = pFilesLoaded;

            while (pFiles != null)
            {
                tCLIFile *    pCLIFile      = pFiles->pCLIFile;
                tMD_Assembly *pThisAssembly = (tMD_Assembly *)MetaData.GetTableRow(pCLIFile->pMetaData, MetaData.MAKE_TABLE_INDEX(0x20, 1));
                if (S.strcmp(pLoadedAssemblyName, pThisAssembly->name) == 0)
                {
                    // Found the correct assembly, so return its meta-data
                    return(pCLIFile->pMetaData);
                }
                pFiles = pFiles->pNext;
            }

            Sys.Crash("Assembly %s is not loaded\n", (PTR)pLoadedAssemblyName);
            return(null);
        }
コード例 #3
0
        public static tMD_TypeDef *FindTypeInAllLoadedAssemblies(/*STRING*/ byte *nameSpace, /*STRING*/ byte *name)
        {
            tFilesLoaded *pFiles = pFilesLoaded;

            while (pFiles != null)
            {
                tCLIFile *pCLIFile = pFiles->pCLIFile;

                tMD_TypeDef *typeDef = MetaData.GetTypeDefFromName(pCLIFile->pMetaData, nameSpace, name, null, /* assertExists */ 0);
                if (typeDef != null)
                {
                    return(typeDef);
                }

                pFiles = pFiles->pNext;
            }

            Sys.Crash("CLIFile_FindTypeInAllLoadedAssemblies(): Cannot find type %s.%s", (PTR)nameSpace, (PTR)name);
            return(null);
        }
コード例 #4
0
        public static int Execute(tCLIFile *pThis, string[] args)
        {
            tThread *          pThread;
            /*HEAP_PTR*/ byte *pArgs;
            int i;

            // Create a string array for the program arguments
            // Don't include the argument that is the program name.
            pArgs = System_Array.NewVector(Type.types[Type.TYPE_SYSTEM_ARRAY_STRING], (uint)(args.Length - 1));
            Heap.MakeUndeletable(pArgs);
            for (i = 1; i < args.Length; i++)
            {
                tSystemString *pArgStr = System_String.FromMonoString(args[i]);
                System_Array.StoreElement(pArgs, (uint)(i - 1), (byte *)&pArgStr);
            }

            // Create the main application thread
            pThread = Thread.New();
            Thread.SetEntryPoint(pThread, pThis->pMetaData, pThis->entryPoint, (byte *)&pArgs, (uint)sizeof(void *));

            return(Thread.Execute());
        }
コード例 #5
0
        private static tCLIFile *LoadPEFile(byte[] image)
        {
            tCLIFile *pRet = ((tCLIFile *)Mem.malloc((SIZE_T)sizeof(tCLIFile)));

            System.Runtime.InteropServices.GCHandle handle = System.Runtime.InteropServices.GCHandle.Alloc(image, GCHandleType.Pinned);
            byte *pData = (byte *)handle.AddrOfPinnedObject();

            byte *pMSDOSHeader = (byte *)&(((byte *)pData)[0]);
            byte *pPEHeader;
            byte *pPEOptionalHeader;
            byte *pPESectionHeaders;
            byte *pCLIHeader;
            byte *pRawMetaData;

            int    i;
            uint   lfanew;
            ushort machine;
            int    numSections;
            //uint imageBase;
            //int fileAlignment;
            uint cliHeaderRVA;
            //uint cliHeaderSize;
            uint metaDataRVA;
            //uint metaDataSize;
            tMetaData *pMetaData;

            pRet->pRVA      = RVA.New();
            pRet->gcHandle  = (PTR)(System.IntPtr)handle;
            pRet->pMetaData = pMetaData = MetaData.New();

            lfanew            = *(uint *)&(pMSDOSHeader[0x3c]);
            pPEHeader         = pMSDOSHeader + lfanew + 4;
            pPEOptionalHeader = pPEHeader + 20;
            pPESectionHeaders = pPEOptionalHeader + 224;

            machine = *(ushort *)&(pPEHeader[0]);
            if (machine != DOT_NET_MACHINE)
            {
                return(null);
            }
            numSections = *(ushort *)&(pPEHeader[2]);

            //imageBase = *(uint*)&(pPEOptionalHeader[28]);
            //fileAlignment = *(int*)&(pPEOptionalHeader[36]);

            for (i = 0; i < numSections; i++)
            {
                byte *pSection = pPESectionHeaders + i * 40;
                RVA.Create(pRet->pRVA, pData, pSection);
            }

            cliHeaderRVA = *(uint *)&(pPEOptionalHeader[208]);
            //cliHeaderSize = *(uint*)&(pPEOptionalHeader[212]);

            pCLIHeader = (byte *)RVA.FindData(pRet->pRVA, cliHeaderRVA);

            metaDataRVA = *(uint *)&(pCLIHeader[8]);
            //metaDataSize = *(uint*)&(pCLIHeader[12]);
            pRet->entryPoint = *(uint *)&(pCLIHeader[20]);
            pRawMetaData     = (byte *)RVA.FindData(pRet->pRVA, metaDataRVA);

            // Load all metadata
            {
                uint  versionLen = *(uint *)&(pRawMetaData[12]);
                uint  ofs, numberOfStreams;
                void *pTableStream    = null;
                uint  tableStreamSize = 0;
                pRet->pVersion = &(pRawMetaData[16]);
                Sys.log_f(1, "CLI version: %s\n", (PTR)pRet->pVersion);
                ofs             = 16 + versionLen;
                numberOfStreams = *(ushort *)&(pRawMetaData[ofs + 2]);
                ofs            += 4;

                for (i = 0; i < (int)numberOfStreams; i++)
                {
                    uint  streamOffset = *(uint *)&pRawMetaData[ofs];
                    uint  streamSize   = *(uint *)&pRawMetaData[ofs + 4];
                    byte *pStreamName  = &pRawMetaData[ofs + 8];
                    void *pStream      = pRawMetaData + streamOffset;
                    ofs += (uint)((S.strlen(pStreamName) + 4) & (~0x3)) + 8;
                    if (S.strcasecmp(pStreamName, "#Strings") == 0)
                    {
                        MetaData.LoadStrings(pMetaData, pStream, streamSize);
                    }
                    else if (S.strcasecmp(pStreamName, "#US") == 0)
                    {
                        MetaData.LoadUserStrings(pMetaData, pStream, streamSize);
                    }
                    else if (S.strcasecmp(pStreamName, "#Blob") == 0)
                    {
                        MetaData.LoadBlobs(pMetaData, pStream, streamSize);
                    }
                    else if (S.strcasecmp(pStreamName, "#GUID") == 0)
                    {
                        MetaData.LoadGUIDs(pMetaData, pStream, streamSize);
                    }
                    else if (S.strcasecmp(pStreamName, "#~") == 0)
                    {
                        pTableStream    = pStream;
                        tableStreamSize = streamSize;
                    }
                }
                // Must load tables last
                if (pTableStream != null)
                {
                    MetaData.LoadTables(pMetaData, pRet->pRVA, pTableStream, (uint)tableStreamSize);
                }
            }

            // Mark all generic definition type and methods as such
            for (i = (int)pMetaData->tables.numRows[MetaDataTable.MD_TABLE_GENERICPARAM]; i > 0; i--)
            {
                tMD_GenericParam * pGenericParam;
                /*IDX_TABLE*/ uint ownerIdx;

                pGenericParam = (tMD_GenericParam *)MetaData.GetTableRow
                                    (pMetaData, MetaData.MAKE_TABLE_INDEX(MetaDataTable.MD_TABLE_GENERICPARAM, (uint)i));
                ownerIdx = pGenericParam->owner;
                switch (MetaData.TABLE_ID(ownerIdx))
                {
                case MetaDataTable.MD_TABLE_TYPEDEF:
                {
                    tMD_TypeDef *pTypeDef = (tMD_TypeDef *)MetaData.GetTableRow(pMetaData, ownerIdx);
                    pTypeDef->isGenericDefinition = 1;
                }
                break;

                case MetaDataTable.MD_TABLE_METHODDEF:
                {
                    tMD_MethodDef *pMethodDef = (tMD_MethodDef *)MetaData.GetTableRow(pMetaData, ownerIdx);
                    pMethodDef->isGenericDefinition = 1;
                }
                break;

                default:
                    Sys.Crash("Wrong generic parameter owner: 0x%08x", ownerIdx);
                    break;
                }
            }

            // Mark all nested classes as such
            for (i = (int)pMetaData->tables.numRows[MetaDataTable.MD_TABLE_NESTEDCLASS]; i > 0; i--)
            {
                tMD_NestedClass *pNested;
                tMD_TypeDef *    pParent;
                tMD_TypeDef *    pChild;

                pNested           = (tMD_NestedClass *)MetaData.GetTableRow(pMetaData, MetaData.MAKE_TABLE_INDEX(MetaDataTable.MD_TABLE_NESTEDCLASS, (uint)i));
                pParent           = (tMD_TypeDef *)MetaData.GetTableRow(pMetaData, pNested->enclosingClass);
                pChild            = (tMD_TypeDef *)MetaData.GetTableRow(pMetaData, pNested->nestedClass);
                pChild->pNestedIn = pParent;
            }

            return(pRet);
        }
コード例 #6
0
        public static tMetaData *GetMetaDataForAssembly(byte *pAssemblyName)
        {
            tFilesLoaded *pFiles;
            int           monoAssembly = 0;
            tCLIFile *    pCLIFile = null;
            tMD_Assembly *pThisAssembly = null;
            tMetaData **  ppChildMetaData = null;
            int           i, j, childCount;

            // Check corlib assemblies
            i = 0;
            while (dnaCorlibAssemblies[i] != null)
            {
                if (S.strcmp(pAssemblyName, dnaCorlibAssemblies[i]) == 0)
                {
                    pAssemblyName = scCorLib;
                    break;
                }
                i++;
            }

            // Look in already-loaded files first
            pFiles = pFilesLoaded;
            while (pFiles != null)
            {
                pCLIFile = pFiles->pCLIFile;
                if (S.strcmp(pAssemblyName, pCLIFile->assemblyName) == 0)
                {
                    // Found the correct assembly, so return its meta-data
                    return(pCLIFile->pMetaData);
                }
                pFiles = pFiles->pNext;
            }

            // Mono/Unity assemblies only load metadata, no code
            if (monoAssemblies != null)
            {
                i = 0;
                while (monoAssemblies[i] != null)
                {
                    if (S.strcmp(pAssemblyName, monoAssemblies[i]) == 0)
                    {
                        if (i == 0)
                        {
                            // Handle "UnityEngine" assemblies
                            j          = 0;
                            childCount = 0;
                            while (unityModuleAssemblies[j] != null)
                            {
                                childCount++;
                                j++;
                            }
                            ppChildMetaData = (tMetaData **)Mem.malloc((SIZE_T)((childCount + 1) * sizeof(tMetaData *)));
                            Mem.memset(ppChildMetaData, 0, (SIZE_T)((childCount + 1) * sizeof(tMetaData *)));
                            j = 0;
                            while (unityModuleAssemblies[j] != null)
                            {
                                ppChildMetaData[j] = GetMetaDataForAssembly(unityModuleAssemblies[j]);
                                j++;
                            }
                        }
                        monoAssembly = 1;
                        break;
                    }
                    i++;
                }
            }

            // Assembly not loaded, so load it if possible
            if (monoAssembly != 0)
            {
                pCLIFile = CLIFile.WrapMonoAssembly(pAssemblyName);
                if (pCLIFile == null)
                {
                    Sys.Crash("Cannot load required mono assembly file: %s.dll", (PTR)pAssemblyName);
                }
            }
            else
            {
                byte *fileName = stackalloc byte[256];
                S.snprintf(fileName, 256, "%s.dll", (PTR)pAssemblyName);
                pCLIFile = CLIFile.LoadAssembly(fileName);
                if (pCLIFile == null)
                {
                    Sys.Crash("Cannot load required assembly file: %s.dll", (PTR)pAssemblyName);
                }
            }

            pCLIFile->pMetaData->ppChildMetaData = ppChildMetaData;

            return(pCLIFile->pMetaData);
        }