示例#1
0
		public ElfRunner(string filename)
		{
			// todo: hack up this baby to take Streams
			_elf = ELFReader.Load<long>(filename);

			var loadsegs = _elf.Segments.Where(s => s.Type == SegmentType.Load);

			long orig_start = loadsegs.Min(s => s.Address);
			orig_start &= ~(Environment.SystemPageSize - 1);
			long orig_end = loadsegs.Max(s => s.Address + s.Size);
			_base = new MemoryBlock(UIntPtr.Zero, orig_end - orig_start);
			_loadoffset = (long)_base.Start - orig_start;

			try
			{
				_base.Set(_base.Start, _base.Size, MemoryBlock.Protection.RW);

				foreach (var seg in loadsegs)
				{
					var data = seg.GetContents();
					Marshal.Copy(data, 0, (IntPtr)(seg.Address + _loadoffset), data.Length);
				}
				RegisterSymbols();
				ProcessRelocations();


				_base.Set(_base.Start, _base.Size, MemoryBlock.Protection.R);

				foreach (var sec in _elf.Sections.Where(s => s.Flags.HasFlag(SectionFlags.Allocatable)))
				{
					if (sec.Flags.HasFlag(SectionFlags.Executable))
						_base.Set((UIntPtr)(sec.LoadAddress + _loadoffset), sec.Size, MemoryBlock.Protection.RX);
					else if (sec.Flags.HasFlag(SectionFlags.Writable))
						_base.Set((UIntPtr)(sec.LoadAddress + _loadoffset), sec.Size, MemoryBlock.Protection.RW);
				}

				//FixupGOT();
				ConnectAllClibPatches();
				Console.WriteLine("Loaded {0}@{1:X16}", filename, (long)_base.Start);
			}
			catch
			{
				_base.Dispose();
				throw;
			}
		}
示例#2
0
		private IntPtr AllocMem(IntPtr size)
		{
			var block = new MemoryBlock((long)size);
			var p = (IntPtr)(long)block.Start;
			_heaps[p] = block;
			block.Set(block.Start, block.Size, MemoryBlock.Protection.RW);
			Console.WriteLine("AllocMem: {0:X8}@{1:X16}", (long)size, (long)block.Start);
			return p;
		}
示例#3
0
		//~ElfRunner()
		//{
		//	Dispose(false);
		//}

		private void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (_base != null)
				{
					_base.Dispose();
					_base = null;
				}
				if (_heaps != null)
				{
					foreach (var b in _heaps.Values)
					{
						b.Dispose();
					}
					_heaps = null;
				}
			}
		}
示例#4
0
 public MemoryViewStream(bool readable, bool writable, long ptr, long length, MemoryBlock owner)
 {
     _readable = readable;
     _writable = writable;
     _ptr = ptr;
     _length = length;
     _owner = owner;
     _pos = 0;
 }
示例#5
0
        public ElfRunner(string filename, long heapsize, long sealedheapsize, long invisibleheapsize)
        {
            using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
            {
                _elfhash = Hash(fs);
            }

            // todo: hack up this baby to take Streams
            _elf = ELFReader.Load<long>(filename);

            var loadsegs = _elf.Segments.Where(s => s.Type == SegmentType.Load);

            long orig_start = loadsegs.Min(s => s.Address);
            orig_start &= ~(Environment.SystemPageSize - 1);
            long orig_end = loadsegs.Max(s => s.Address + s.Size);
            if (HasRelocations())
            {
                _base = new MemoryBlock((ulong)(orig_end - orig_start));
                _loadoffset = (long)_base.Start - orig_start;
                _lockkey = 0;
            }
            else
            {
                _lockkey = (ulong)orig_start;
                _base = new MemoryBlock(_lockkey, (ulong)(orig_end - orig_start));
                _loadoffset = 0;
                Enter();
            }

            try
            {
                _disposeList.Add(_base);
                _memoryBlocks.Add(_base);
                _base.Activate();
                _base.Protect(_base.Start, _base.Size, MemoryBlock.Protection.RW);

                foreach (var seg in loadsegs)
                {
                    var data = seg.GetContents();
                    Marshal.Copy(data, 0, Z.SS(seg.Address + _loadoffset), data.Length);
                }
                RegisterSymbols();
                ProcessRelocations();

                _base.Protect(_base.Start, _base.Size, MemoryBlock.Protection.R);

                foreach (var sec in _elf.Sections.Where(s => (s.Flags & SectionFlags.Allocatable) != 0))
                {
                    if ((sec.Flags & SectionFlags.Executable) != 0)
                        _base.Protect((ulong)(sec.LoadAddress + _loadoffset), (ulong)sec.Size, MemoryBlock.Protection.RX);
                    else if ((sec.Flags & SectionFlags.Writable) != 0)
                        _base.Protect((ulong)(sec.LoadAddress + _loadoffset), (ulong)sec.Size, MemoryBlock.Protection.RW);
                }

                ulong end = _base.End;

                if (heapsize > 0)
                {
                    _heap = new Heap(GetHeapStart(end), (ulong)heapsize, "sbrk-heap");
                    _heap.Memory.Activate();
                    end = _heap.Memory.End;
                    _disposeList.Add(_heap);
                    _memoryBlocks.Add(_heap.Memory);
                }

                if (sealedheapsize > 0)
                {
                    _sealedheap = new Heap(GetHeapStart(end), (ulong)sealedheapsize, "sealed-heap");
                    _sealedheap.Memory.Activate();
                    end = _sealedheap.Memory.End;
                    _disposeList.Add(_sealedheap);
                    _memoryBlocks.Add(_sealedheap.Memory);
                }

                if (invisibleheapsize > 0)
                {
                    _invisibleheap = new Heap(GetHeapStart(end), (ulong)invisibleheapsize, "invisible-heap");
                    _invisibleheap.Memory.Activate();
                    end = _invisibleheap.Memory.End;
                    _disposeList.Add(_invisibleheap);
                    _memoryBlocks.Add(_invisibleheap.Memory);
                }

                ConnectAllClibPatches();
                Console.WriteLine("Loaded {0}@{1:X16}", filename, _base.Start);
                foreach (var sec in _elf.Sections.Where(s => s.LoadAddress != 0))
                {
                    Console.WriteLine("  {0}@{1:X16}, size {2}", sec.Name.PadLeft(20), sec.LoadAddress + _loadoffset, sec.Size.ToString().PadLeft(12));
                }

                PrintTopSavableSymbols();
            }
            catch
            {
                Dispose();
                throw;
            }
            finally
            {
                Exit();
            }
        }
示例#6
0
 public Heap(ulong start, ulong size, string name)
 {
     Memory = new MemoryBlock(start, size);
     Used = 0;
     Name = name;
 }
示例#7
0
 //~ElfRunner()
 //{
 //    Dispose(false);
 //}
 private void Dispose(bool disposing)
 {
     if (disposing)
     {
         foreach (var d in _disposeList)
             d.Dispose();
         _disposeList.Clear();
         _memoryBlocks.Clear();
         _base = null;
         _heap = null;
         _sealedheap = null;
         _invisibleheap = null;
     }
 }