Пример #1
0
            public void LoadData(MethodDefData pMethodDef)
            {
                SectionHeader sectionHeader     = pMethodDef.CLIFile.GetSection(pMethodDef.RVA);
                uint          startOfBodyHeader = sectionHeader.PointerToRawData + (pMethodDef.RVA - sectionHeader.VirtualAddress);
                uint          sizeOfBodyHeader  = 1;

                Flags    = (ushort)(pMethodDef.CLIFile.Data[startOfBodyHeader] & 0x03);
                MaxStack = 8;
                CodeSize = (uint)(pMethodDef.CLIFile.Data[startOfBodyHeader] >> 2);
                if ((Flags & 0x03) == 0x03)
                {
                    Flags             = pMethodDef.CLIFile.Data[startOfBodyHeader];
                    Flags            |= (ushort)((pMethodDef.CLIFile.Data[startOfBodyHeader + 1] & 0x0F) << 8);
                    sizeOfBodyHeader  = (uint)((pMethodDef.CLIFile.Data[startOfBodyHeader + 1] >> 4) * 4);
                    MaxStack          = pMethodDef.CLIFile.Data[startOfBodyHeader + 2];
                    MaxStack         |= (ushort)(pMethodDef.CLIFile.Data[startOfBodyHeader + 3] << 8);
                    CodeSize          = pMethodDef.CLIFile.Data[startOfBodyHeader + 4];
                    CodeSize         |= (uint)(pMethodDef.CLIFile.Data[startOfBodyHeader + 5] << 8);
                    CodeSize         |= (uint)(pMethodDef.CLIFile.Data[startOfBodyHeader + 6] << 16);
                    CodeSize         |= (uint)(pMethodDef.CLIFile.Data[startOfBodyHeader + 7] << 24);
                    LocalVarSigToken  = pMethodDef.CLIFile.Data[startOfBodyHeader + 8];
                    LocalVarSigToken |= (uint)(pMethodDef.CLIFile.Data[startOfBodyHeader + 9] << 8);
                    LocalVarSigToken |= (uint)(pMethodDef.CLIFile.Data[startOfBodyHeader + 10] << 16);
                    LocalVarSigToken |= (uint)(pMethodDef.CLIFile.Data[startOfBodyHeader + 11] << 24);
                }
                CodeRVA = startOfBodyHeader + sizeOfBodyHeader;
                List <MethodDefBodyExceptionData> exceptions = new List <MethodDefBodyExceptionData>();

                if ((Flags & 0x08) == 0x08)
                {
                    uint startOfExceptionTableHeader = CodeRVA + CodeSize;
                    if ((startOfExceptionTableHeader & 0x03) != 0)
                    {
                        startOfExceptionTableHeader += 4 - (startOfExceptionTableHeader & 0x03);
                    }
                    uint startOfCurrentExceptionTableHeader = startOfExceptionTableHeader;
                    bool anotherSection = true;
                    while (anotherSection)
                    {
                        bool fatEntries = (pMethodDef.CLIFile.Data[startOfCurrentExceptionTableHeader] & 0x40) == 0x40;
                        anotherSection = (pMethodDef.CLIFile.Data[startOfCurrentExceptionTableHeader] & 0x80) == 0x80;
                        uint totalSize = pMethodDef.CLIFile.Data[startOfCurrentExceptionTableHeader + 1];
                        uint entrySize = 12;
                        if (fatEntries)
                        {
                            totalSize |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionTableHeader + 2] << 8);
                            totalSize |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionTableHeader + 3] << 16);
                            entrySize  = 24;
                        }
                        uint entryCount = (uint)((totalSize - 4) / entrySize);
                        uint startOfCurrentExceptionEntry = startOfCurrentExceptionTableHeader + 4;
                        for (int index = 0; index < entryCount; ++index, startOfCurrentExceptionEntry += entrySize)
                        {
                            MethodDefBodyExceptionData exception = new MethodDefBodyExceptionData();
                            exception.Flags                     = pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry];
                            exception.Flags                    |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 1] << 8);
                            exception.TryOffset                 = pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 2];
                            exception.TryOffset                |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 3] << 8);
                            exception.TryLength                 = pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 4];
                            exception.HandlerOffset             = pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 5];
                            exception.HandlerOffset            |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 6] << 8);
                            exception.HandlerLength             = pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 7];
                            exception.ClassTokenOrFilterOffset  = pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 8];
                            exception.ClassTokenOrFilterOffset |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 9] << 8);
                            exception.ClassTokenOrFilterOffset |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 10] << 16);
                            exception.ClassTokenOrFilterOffset |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 11] << 24);
                            if (fatEntries)
                            {
                                exception.Flags                     = pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry];
                                exception.Flags                    |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 1] << 8);
                                exception.Flags                    |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 2] << 16);
                                exception.Flags                    |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 3] << 24);
                                exception.TryOffset                 = pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 4];
                                exception.TryOffset                |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 5] << 8);
                                exception.TryOffset                |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 6] << 16);
                                exception.TryOffset                |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 7] << 24);
                                exception.TryLength                 = pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 8];
                                exception.TryLength                |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 9] << 8);
                                exception.TryLength                |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 10] << 16);
                                exception.TryLength                |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 11] << 24);
                                exception.HandlerOffset             = pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 12];
                                exception.HandlerOffset            |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 13] << 8);
                                exception.HandlerOffset            |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 14] << 16);
                                exception.HandlerOffset            |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 15] << 24);
                                exception.HandlerLength             = pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 16];
                                exception.HandlerLength            |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 17] << 8);
                                exception.HandlerLength            |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 18] << 16);
                                exception.HandlerLength            |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 19] << 24);
                                exception.ClassTokenOrFilterOffset  = pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 20];
                                exception.ClassTokenOrFilterOffset |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 21] << 8);
                                exception.ClassTokenOrFilterOffset |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 22] << 16);
                                exception.ClassTokenOrFilterOffset |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 23] << 24);
                            }
                            exceptions.Add(exception);
                        }

                        startOfCurrentExceptionTableHeader += totalSize;
                    }
                }
                Exceptions = exceptions.ToArray();
            }
Пример #2
0
            public void LoadData(MethodDefData pMethodDef)
            {
                SectionHeader sectionHeader = pMethodDef.CLIFile.GetSection(pMethodDef.RVA);
                uint startOfBodyHeader = sectionHeader.PointerToRawData + (pMethodDef.RVA - sectionHeader.VirtualAddress);
                uint sizeOfBodyHeader = 1;
                Flags = (ushort)(pMethodDef.CLIFile.Data[startOfBodyHeader] & 0x03);
                MaxStack = 8;
                CodeSize = (uint)(pMethodDef.CLIFile.Data[startOfBodyHeader] >> 2);
                if ((Flags & 0x03) == 0x03)
                {
                    Flags = pMethodDef.CLIFile.Data[startOfBodyHeader];
                    Flags |= (ushort)((pMethodDef.CLIFile.Data[startOfBodyHeader + 1] & 0x0F) << 8);
                    sizeOfBodyHeader = (uint)((pMethodDef.CLIFile.Data[startOfBodyHeader + 1] >> 4) * 4);
                    MaxStack = pMethodDef.CLIFile.Data[startOfBodyHeader + 2];
                    MaxStack |= (ushort)(pMethodDef.CLIFile.Data[startOfBodyHeader + 3] << 8);
                    CodeSize = pMethodDef.CLIFile.Data[startOfBodyHeader + 4];
                    CodeSize |= (uint)(pMethodDef.CLIFile.Data[startOfBodyHeader + 5] << 8);
                    CodeSize |= (uint)(pMethodDef.CLIFile.Data[startOfBodyHeader + 6] << 16);
                    CodeSize |= (uint)(pMethodDef.CLIFile.Data[startOfBodyHeader + 7] << 24);
                    LocalVarSigToken = pMethodDef.CLIFile.Data[startOfBodyHeader + 8];
                    LocalVarSigToken |= (uint)(pMethodDef.CLIFile.Data[startOfBodyHeader + 9] << 8);
                    LocalVarSigToken |= (uint)(pMethodDef.CLIFile.Data[startOfBodyHeader + 10] << 16);
                    LocalVarSigToken |= (uint)(pMethodDef.CLIFile.Data[startOfBodyHeader + 11] << 24);
                }
                CodeRVA = startOfBodyHeader + sizeOfBodyHeader;
                List<MethodDefBodyExceptionData> exceptions = new List<MethodDefBodyExceptionData>();
                if ((Flags & 0x08) == 0x08)
                {
                    uint startOfExceptionTableHeader = CodeRVA + CodeSize;
                    if ((startOfExceptionTableHeader & 0x03) != 0) startOfExceptionTableHeader += 4 - (startOfExceptionTableHeader & 0x03);
                    uint startOfCurrentExceptionTableHeader = startOfExceptionTableHeader;
                    bool anotherSection = true;
                    while (anotherSection)
                    {
                        bool fatEntries = (pMethodDef.CLIFile.Data[startOfCurrentExceptionTableHeader] & 0x40) == 0x40;
                        anotherSection = (pMethodDef.CLIFile.Data[startOfCurrentExceptionTableHeader] & 0x80) == 0x80;
                        uint totalSize = pMethodDef.CLIFile.Data[startOfCurrentExceptionTableHeader + 1];
                        uint entrySize = 12;
                        if (fatEntries)
                        {
                            totalSize |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionTableHeader + 2] << 8);
                            totalSize |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionTableHeader + 3] << 16);
                            entrySize = 24;
                        }
                        uint entryCount = (uint)((totalSize - 4) / entrySize);
                        uint startOfCurrentExceptionEntry = startOfCurrentExceptionTableHeader + 4;
                        for (int index = 0; index < entryCount; ++index, startOfCurrentExceptionEntry += entrySize)
                        {
                            MethodDefBodyExceptionData exception = new MethodDefBodyExceptionData();
                            exception.Flags = pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry];
                            exception.Flags |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 1] << 8);
                            exception.TryOffset = pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 2];
                            exception.TryOffset |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 3] << 8);
                            exception.TryLength = pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 4];
                            exception.HandlerOffset = pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 5];
                            exception.HandlerOffset |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 6] << 8);
                            exception.HandlerLength = pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 7];
                            exception.ClassTokenOrFilterOffset = pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 8];
                            exception.ClassTokenOrFilterOffset |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 9] << 8);
                            exception.ClassTokenOrFilterOffset |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 10] << 16);
                            exception.ClassTokenOrFilterOffset |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 11] << 24);
                            if (fatEntries)
                            {
                                exception.Flags = pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry];
                                exception.Flags |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 1] << 8);
                                exception.Flags |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 2] << 16);
                                exception.Flags |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 3] << 24);
                                exception.TryOffset = pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 4];
                                exception.TryOffset |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 5] << 8);
                                exception.TryOffset |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 6] << 16);
                                exception.TryOffset |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 7] << 24);
                                exception.TryLength = pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 8];
                                exception.TryLength |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 9] << 8);
                                exception.TryLength |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 10] << 16);
                                exception.TryLength |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 11] << 24);
                                exception.HandlerOffset = pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 12];
                                exception.HandlerOffset |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 13] << 8);
                                exception.HandlerOffset |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 14] << 16);
                                exception.HandlerOffset |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 15] << 24);
                                exception.HandlerLength = pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 16];
                                exception.HandlerLength |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 17] << 8);
                                exception.HandlerLength |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 18] << 16);
                                exception.HandlerLength |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 19] << 24);
                                exception.ClassTokenOrFilterOffset = pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 20];
                                exception.ClassTokenOrFilterOffset |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 21] << 8);
                                exception.ClassTokenOrFilterOffset |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 22] << 16);
                                exception.ClassTokenOrFilterOffset |= (uint)(pMethodDef.CLIFile.Data[startOfCurrentExceptionEntry + 23] << 24);
                            }
                            exceptions.Add(exception);
                        }

                        startOfCurrentExceptionTableHeader += totalSize;
                    }
                }
                Exceptions = exceptions.ToArray();
            }