Beispiel #1
0
        public DebugInfo(byte[] image, int offset, Machine machine, bool normalize)
        {
            _machine   = machine;
            _normalize = normalize;

            // Get the id of the runtime function from the NativeArray
            uint lookback        = 0;
            uint debugInfoOffset = NativeReader.DecodeUnsigned(image, (uint)offset, ref lookback);

            if (lookback != 0)
            {
                System.Diagnostics.Debug.Assert(0 < lookback && lookback < offset);
                debugInfoOffset = (uint)offset - lookback;
            }

            NibbleReader reader             = new NibbleReader(image, (int)debugInfoOffset);
            uint         boundsByteCount    = reader.ReadUInt();
            uint         variablesByteCount = reader.ReadUInt();
            int          boundsOffset       = reader.GetNextByteOffset();
            int          variablesOffset    = (int)(boundsOffset + boundsByteCount);

            if (boundsByteCount > 0)
            {
                ParseBounds(image, boundsOffset);
            }

            if (variablesByteCount > 0)
            {
                ParseNativeVarInfo(image, variablesOffset);
            }
        }
Beispiel #2
0
        private int ReadEncodedStackOffset(NibbleReader reader)
        {
            int offset = reader.ReadInt();

            if (_machine == Machine.I386)
            {
                offset *= 4; // sizeof(DWORD)
            }

            return(offset);
        }
Beispiel #3
0
        private FixupCell[] DecodeFixupCells(int offset)
        {
            List <FixupCell> cells  = new List <FixupCell>();
            NibbleReader     reader = new NibbleReader(Image, offset);

            // The following algorithm has been loosely ported from CoreCLR,
            // src\vm\ceeload.inl, BOOL Module::FixupDelayListAux
            uint curTableIndex = reader.ReadUInt();

            while (true)
            {
                uint fixupIndex = reader.ReadUInt(); // Accumulate the real rva from the delta encoded rva

                while (true)
                {
                    R2RImportSection importSection            = ImportSections[(int)curTableIndex];
                    R2RImportSection.ImportSectionEntry entry = importSection.Entries[(int)fixupIndex];
                    cells.Add(new FixupCell(cells.Count, curTableIndex, fixupIndex, entry.Signature));

                    uint delta = reader.ReadUInt();

                    // Delta of 0 means end of entries in this table
                    if (delta == 0)
                    {
                        break;
                    }

                    fixupIndex += delta;
                }

                uint tableIndex = reader.ReadUInt();

                if (tableIndex == 0)
                {
                    break;
                }

                curTableIndex = curTableIndex + tableIndex;
            } // Done with all entries in this table

            return(cells.ToArray());
        }
Beispiel #4
0
        private void ParseBounds(byte[] image, int offset)
        {
            // Bounds info contains (Native Offset, IL Offset, flags)
            // - Sorted by native offset (so use a delta encoding for that).
            // - IL offsets aren't sorted, but they should be close to each other (so a signed delta encoding)
            //   They may also include a sentinel value from MappingTypes.
            // - flags is 3 indepedent bits.
            NibbleReader reader           = new NibbleReader(image, offset);
            uint         boundsEntryCount = reader.ReadUInt();

            Debug.Assert(boundsEntryCount > 0);

            uint previousNativeOffset = 0;

            for (int i = 0; i < boundsEntryCount; ++i)
            {
                var entry = new DebugInfoBoundsEntry();
                previousNativeOffset += reader.ReadUInt();
                entry.NativeOffset    = previousNativeOffset;
                entry.ILOffset        = (uint)(reader.ReadUInt() + (int)MappingTypes.MaxMappingValue);
                entry.SourceTypes     = (SourceTypes)reader.ReadUInt();
                _boundsList.Add(entry);
            }
        }
Beispiel #5
0
        private void ParseNativeVarInfo(byte[] image, int offset)
        {
            // Each Varinfo has a:
            // - native start +End offset. We can use a delta for the end offset.
            // - Il variable number. These are usually small.
            // - VarLoc information. This is a tagged variant.
            // The entries aren't sorted in any particular order.
            NibbleReader reader         = new NibbleReader(image, offset);
            uint         nativeVarCount = reader.ReadUInt();

            for (int i = 0; i < nativeVarCount; ++i)
            {
                var entry = new NativeVarInfo();
                entry.StartOffset    = reader.ReadUInt();
                entry.EndOffset      = entry.StartOffset + reader.ReadUInt();
                entry.VariableNumber = (uint)(reader.ReadUInt() + (int)ImplicitILArguments.Max);

                var varLoc = new VarLoc();
                varLoc.VarLocType = (VarLocType)reader.ReadUInt();
                switch (varLoc.VarLocType)
                {
                case VarLocType.VLT_REG:
                case VarLocType.VLT_REG_FP:
                case VarLocType.VLT_REG_BYREF:
                    varLoc.Data1 = (int)reader.ReadUInt();
                    break;

                case VarLocType.VLT_STK:
                case VarLocType.VLT_STK_BYREF:
                    varLoc.Data1 = (int)reader.ReadUInt();
                    varLoc.Data2 = ReadEncodedStackOffset(reader);
                    break;

                case VarLocType.VLT_REG_REG:
                    varLoc.Data1 = (int)reader.ReadUInt();
                    varLoc.Data2 = (int)reader.ReadUInt();
                    break;

                case VarLocType.VLT_REG_STK:
                    varLoc.Data1 = (int)reader.ReadUInt();
                    varLoc.Data2 = (int)reader.ReadUInt();
                    varLoc.Data3 = ReadEncodedStackOffset(reader);
                    break;

                case VarLocType.VLT_STK_REG:
                    varLoc.Data1 = ReadEncodedStackOffset(reader);
                    varLoc.Data2 = (int)reader.ReadUInt();
                    varLoc.Data3 = (int)reader.ReadUInt();
                    break;

                case VarLocType.VLT_STK2:
                    varLoc.Data1 = (int)reader.ReadUInt();
                    varLoc.Data2 = ReadEncodedStackOffset(reader);
                    break;

                case VarLocType.VLT_FPSTK:
                    varLoc.Data1 = (int)reader.ReadUInt();
                    break;

                case VarLocType.VLT_FIXED_VA:
                    varLoc.Data1 = (int)reader.ReadUInt();
                    break;

                default:
                    throw new BadImageFormatException("Unexpected var loc type");
                }

                entry.VariableLocation = varLoc;
                _variablesList.Add(entry);
            }

            if (_normalize)
            {
                _variablesList.Sort(CompareNativeVarInfo);
            }
        }