Beispiel #1
0
        public void Sm_Creation()
        {
            SegmentMap sm = new SegmentMap(addrBase,
                new ImageSegment("", new MemoryArea(addrBase, img), AccessMode.ReadWriteExecute));

            sm.AddSegment(Address.SegPtr(0x8000, 2), "", AccessMode.ReadWrite, 10);
            sm.AddSegment(Address.SegPtr(0x8000, 3), "", AccessMode.ReadWrite, 10);
            sm.AddSegment(Address.SegPtr(0x8000, 0), "", AccessMode.ReadWrite, 10);

            // Verify
            var sExp = 
@"8000:0000 00000002 rwx 
8000:0002 00000001 rw- 
8000:0003 0000001D rw- ";
            AssertMap(sExp, sm);
        }
Beispiel #2
0
 public void Sm_AddSegment()
 {
     var map = new SegmentMap(addrBase);
     var mem = new MemoryArea(addrBase, new byte[0x4000]);
     var seg = new ImageSegment("8100", Address.SegPtr(0x8100, 0), mem, AccessMode.ReadWriteExecute);
     map.AddSegment(seg);
     Assert.AreEqual(0x3000, seg.Size);
 }
Beispiel #3
0
 public void Sm_AddNamedSegment()
 {
     var mem = new MemoryArea(Address.SegPtr(0x0B00, 0), new byte[0x2000]);
     SegmentMap segmentMap = new SegmentMap(mem.BaseAddress,
         new ImageSegment("base", mem, AccessMode.ReadWriteExecute));
     segmentMap.AddSegment(Address.SegPtr(0xC00, 0), "0C00", AccessMode.ReadWrite, 6000);
     ImageSegment s = segmentMap.Segments.Values.ElementAt(1);
     Assert.AreEqual("0C00", s.Name);
     Assert.AreEqual(0x1000, s.Size);
 }
Beispiel #4
0
 public void Sm_Overlaps()
 {
     SegmentMap im = new SegmentMap(Address.SegPtr(0x8000, 0));
     var mem = new MemoryArea(im.BaseAddress, new byte[40]);
     var seg = new ImageSegment("8000", Address.SegPtr(0x8000, 10), mem, AccessMode.ReadWrite);
     im.AddSegment(seg);
 }
Beispiel #5
0
            void parseSection64(uint protection, SegmentMap segmentMap)
            {
                var abSectname = rdr.ReadBytes(16);
                var abSegname = rdr.ReadBytes(16);
                ulong addr;
                ulong size;
                uint offset;
                uint align;
                uint reloff;
                uint nreloc;
                uint flags;
                uint reserved1;
                uint reserved2;
                uint reserved3;

                if (!rdr.TryReadUInt64(out addr) ||
                    !rdr.TryReadUInt64(out size) ||
                    !rdr.TryReadUInt32(out offset) ||
                    !rdr.TryReadUInt32(out align) ||
                    !rdr.TryReadUInt32(out reloff) ||
                    !rdr.TryReadUInt32(out nreloc) ||
                    !rdr.TryReadUInt32(out flags) ||
                    !rdr.TryReadUInt32(out reserved1) ||
                    !rdr.TryReadUInt32(out reserved2) ||
                    !rdr.TryReadUInt32(out reserved3))
                {
                    throw new BadImageFormatException("Could not read Mach-O section.");
                }

                var sectionName = GetAsciizString(abSectname);
                var segmentName = GetAsciizString(abSegname);

                Debug.Print("Found section '{0}' in segment '{1}, addr = 0x{2:X}, size = 0x{3:X}.",
                        sectionName,
                        segmentName,
                        addr,
                        size);

                AccessMode am = 0;
                if ((protection & VM_PROT_READ) != 0)
                    am |= AccessMode.Read;
                if ((protection & VM_PROT_WRITE) != 0)
                    am |= AccessMode.Write;
                if ((protection & VM_PROT_EXECUTE) != 0)
                    am |= AccessMode.Execute;

                var bytes = rdr.CreateNew(this.ldr.RawImage, offset);
                var mem = new MemoryArea(
                    Address.Ptr64(addr),
                    bytes.ReadBytes((uint)size));
                var imageSection = new ImageSegment(
                    string.Format("{0},{1}", segmentName, sectionName),
                    mem,
                    am);

                //imageSection.setBss((section.flags & SECTION_TYPE) == S_ZEROFILL);

                //if (!imageSection.isBss()) {
                //    auto pos = source_->pos();
                //    if (!source_->seek(section.offset)) {
                //        throw ParseError("Could not seek to the beginning of the section's content.");
                //    }
                //    auto bytes = source_->read(section.size);
                //    if (checked_cast<uint>(bytes.size()) != section.size) {
                //        log_.warning("Could not read all the section's content.");
                //    } else {
                //        imageSection->setContent(std::move(bytes));
                //    }
                //    source_->seek(pos);
                //}

                //sections_.push_back(imageSection.get());
                //image_->addSection(std::move(imageSection));
                segmentMap.AddSegment(imageSection);
            }
Beispiel #6
0
        public void Im_RemoveItem_DoNotMergeDisjointItems()
        {
            var mem = new MemoryArea(addrBase, new byte[0x0100]);
            var segmentMap = new SegmentMap(addrBase,
                new ImageSegment("", mem, AccessMode.ReadWriteExecute));
            var codeAddr = addrBase;
            var dataAddr = addrBase + 0x1000;
            var textAddr = addrBase + 0x2000;
            segmentMap.AddSegment(codeAddr, "code", AccessMode.ReadWrite, 0x100);
            segmentMap.AddSegment(dataAddr, "data", AccessMode.ReadWrite, 0x4);
            segmentMap.AddSegment(textAddr, "text", AccessMode.ReadWrite, 0x100);

            var map = segmentMap.CreateImageMap();

            CreateImageMapItem(map, PrimitiveType.Int32, addrBase + 0x1000);

            map.Dump();

            CheckImageMapTypes(map, "<unknown>", "int32", "<unknown>");
            CheckImageMapAddresses(map, "8000:0000", "8000:1000", "8000:2000");
            CheckImageMapSizes(map, 0x100, 0x4, 0x100);

            map.RemoveItem(codeAddr);
            CheckImageMapTypes(map, "<unknown>", "int32", "<unknown>");
            CheckImageMapAddresses(map, "8000:0000", "8000:1000", "8000:2000");
            CheckImageMapSizes(map, 0x100, 0x4, 0x100);

            map.RemoveItem(dataAddr);
            CheckImageMapTypes(map, "<unknown>", "<unknown>", "<unknown>");
            CheckImageMapAddresses(map, "8000:0000", "8000:1000", "8000:2000");
            CheckImageMapSizes(map, 0x100, 0x4, 0x100);

            map.RemoveItem(textAddr);
            CheckImageMapTypes(map, "<unknown>", "<unknown>", "<unknown>");
            CheckImageMapAddresses(map, "8000:0000", "8000:1000", "8000:2000");
            CheckImageMapSizes(map, 0x100, 0x4, 0x100);
        }
Beispiel #7
0
        public override SegmentMap LoadImageBytes(IPlatform platform, byte[] rawImage, Address addrPreferred)
        {
            var segMap = AllocateMemoryAreas(
                ProgramHeaders64
                    .Where(p => IsLoadable(p.p_vaddr, p.p_type))
                    .Select(p => Tuple.Create(
                        platform.MakeAddressFromLinear(p.p_vaddr),
                        (uint)p.p_pmemsz)));
            foreach (var ph in ProgramHeaders64)
            {
                Debug.Print("ph: addr {0:X8} filesize {0:X8} memsize {0:X8}", ph.p_vaddr, ph.p_filesz, ph.p_pmemsz);
                if (!IsLoadable(ph.p_vaddr, ph.p_type))
                    continue;
                var vaddr = platform.MakeAddressFromLinear(ph.p_vaddr);
                MemoryArea mem;
                segMap.TryGetLowerBound(vaddr, out mem);
                if (ph.p_filesz > 0)
                    Array.Copy(
                        rawImage,
                        (long)ph.p_offset, mem.Bytes,
                        vaddr - mem.BaseAddress, (long)ph.p_filesz);
            }
            var segmentMap = new SegmentMap(addrPreferred);
            foreach (var section in Sections)
            {
                if (section.Name == null || section.Address == null)
                    continue;
                MemoryArea mem;
                if (segMap.TryGetLowerBound(section.Address, out mem) &&
                    section.Address < mem.EndAddress)
                {
                    AccessMode mode = AccessModeOf(section.Flags);
                    var seg = segmentMap.AddSegment(new ImageSegment(
                        section.Name,
                        section.Address,
                        mem, mode)
                    {
                        Size = (uint)section.Size
                    });
                    seg.Designer = CreateRenderer64(section);
                }
                else
                {
                    //$TODO: warn
                }
            }
            segmentMap.DumpSections();
            return segmentMap;

        }