Ejemplo n.º 1
0
        /// <summary>
        /// Step01 Load
        /// </summary>
        /// <param name="stream">Stream</param>
        public void LoadNef(Stream stream)
        {
            //read all Instruction to listInst
            var listInst = new List <NefInstruction>();
            //read all Address to listAddr
            var mapLabel   = new Dictionary <int, NefLabel>();
            int labelindex = 1;

            NefInstruction inst;

            do
            {
                inst = NefInstruction.ReadFrom(stream);
                if (inst != null)
                {
                    listInst.Add(inst);
                    for (var i = 0; i < inst.AddressCountInData; i++)
                    {
                        // mapping addr to label
                        int addr = inst.GetAddressInData(i) + inst.Offset;
                        if (!mapLabel.ContainsKey(addr))
                        {
                            var labelname = "label" + labelindex.ToString("D06");
                            labelindex++;
                            var label = new NefLabel(labelname, addr);
                            mapLabel.Add(addr, label);
                        }

                        inst.Labels[i] = mapLabel[addr].Name;
                    }
                }
            } while (inst != null);

            //Add Labels
            Items = new List <INefItem>();
            foreach (var instruction in listInst)
            {
                var curOffset = instruction.Offset;
                if (mapLabel.ContainsKey(curOffset))
                {
                    Items.Add(mapLabel[curOffset]);
                }
                Items.Add(instruction);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Step01 Load
        /// </summary>
        /// <param name="stream">Stream</param>
        /// <param name="entryPoints">Entry points</param>
        public void LoadNef(Stream stream, int[] entryPoints)
        {
            // Read all Instruction to listInst.
            var listInst = new List <NefInstruction>();
            // Read all Address to listAddr.
            var mapLabel   = new Dictionary <int, NefLabel>();
            int labelindex = 1;

            // Insert EntryPoint Label.
            for (var i = 0; i < entryPoints.Length; i++)
            {
                if (i > 0 && entryPoints[i - 1] == entryPoints[i])
                {
                    // Same EntryPoints are not allowed
                    throw new Exception("Same EntryPoints are not allowed.");
                }
                if (!mapLabel.ContainsKey(entryPoints[i]))
                {
                    var labelname = "method" + i.ToString("D04");
                    var addr      = entryPoints[i];
                    var label     = new NefLabel(labelname, addr, true);
                    mapLabel.Add(addr, label);
                }
            }
            if (!mapLabel.ContainsKey(0))
            {
                var labelname = "method_zero";
                var label     = new NefLabel(labelname, 0, true);
                mapLabel.Add(0, label);
            }

            NefInstruction inst;

            do
            {
                inst = NefInstruction.ReadFrom(stream);
                if (inst != null)
                {
                    listInst.Add(inst);
                    for (var i = 0; i < inst.AddressCountInData; i++)
                    {
                        // mapping addr to label
                        int addr = inst.GetAddressInData(i) + inst.Offset;
                        if (!mapLabel.ContainsKey(addr))
                        {
                            var labelname = "label" + labelindex.ToString("D06");
                            labelindex++;
                            var label = new NefLabel(labelname, addr);
                            mapLabel.Add(addr, label);
                        }

                        inst.Labels[i] = mapLabel[addr].Name;
                    }
                }
            } while (inst != null);

            // Add Labels and split to Methods
            Methods = new List <NefMethod>();

            var curMethod = new NefMethod();

            if (listInst.Count == 0)//空也要插一个标签
            {
                curMethod.Items.Add(mapLabel[0]);
            }
            else
            {
                foreach (var instruction in listInst)
                {
                    var curOffset = instruction.Offset;
                    if (mapLabel.ContainsKey(curOffset))
                    {
                        var label = mapLabel[curOffset];
                        if (label.IsEntryPoint && curMethod.Items.Count > 0)
                        {
                            Methods.Add(curMethod);
                            curMethod = new NefMethod();
                        }
                        curMethod.Items.Add(mapLabel[curOffset]);
                    }
                    curMethod.Items.Add(instruction);
                }
            }
            Methods.Add(curMethod);
        }