Beispiel #1
0
        public LoaderResult64 LoadImage()
        {
            if (!stream.CanRead)
            {
                throw new ArgumentException("Stream is not available for reading", nameof(stream));
            }
            if (!stream.CanSeek)
            {
                throw new ArgumentException("Stream is not available for seeking", nameof(stream));
            }

            var elfFileHeader = new Header64();

            elfFileHeader.Read(stream);

            stream.Seek((long)elfFileHeader.E_PHOFF, SeekOrigin.Begin);
            var programHeader = new ProgramHeader64();

            programHeader.Read(stream);

            var    image       = new byte[(int)programHeader.P_FILESZ - elfFileHeader.E_EHSIZE - (elfFileHeader.E_PHNUM * elfFileHeader.E_PHENTSIZE)];
            UInt64 imageOffset =
                elfFileHeader.E_EHSIZE
                + (UInt64)elfFileHeader.E_EHSIZE.CalculateRoundUpTo16Pad()
                + (UInt64)(elfFileHeader.E_PHNUM * (elfFileHeader.E_PHENTSIZE + elfFileHeader.E_PHENTSIZE.CalculateRoundUpTo16Pad()));

            stream.Seek((long)imageOffset, SeekOrigin.Begin);
            stream.Read(image, 0, image.Length);

            return(new LoaderResult64(elfFileHeader.E_ENTRY - (ulong)imageOffset, image,
                                      metadata: new object[] { elfFileHeader, programHeader }));
        }
Beispiel #2
0
 public static bool TryRead(Stream stream, out Header64 header)
 {
     try
     {
         header = new Header64();
         header.Read(stream);
         return(true);
     }
     catch (Exception ex)
     {
         header = default(Header64);
         Console.Error.WriteLine(ex);
         return(false);
     }
 }
Beispiel #3
0
        public ImmutableList <object> LoadMetadata()
        {
            var metadata = new List <object>();

            if (!stream.CanRead)
            {
                throw new ArgumentException("Stream is not available for reading", nameof(stream));
            }
            if (!stream.CanSeek)
            {
                throw new ArgumentException("Stream is not available for seeking", nameof(stream));
            }

            var elfFileHeader = new Header64();

            elfFileHeader.Read(stream);
            metadata.Add(elfFileHeader);

            if (elfFileHeader.E_PHNUM > 0)
            {
                for (var i = 0L; i < elfFileHeader.E_PHNUM; i++)
                {
                    var phOffset = (long)elfFileHeader.E_PHOFF + (i * elfFileHeader.E_PHENTSIZE);
                    stream.Seek(phOffset, SeekOrigin.Begin);
                    var programHeader = new ProgramHeader64();
                    programHeader.Read(stream);
                    metadata.Add(programHeader);
                }
            }

            if (elfFileHeader.E_SHNUM > 0)
            {
                for (var i = 0L; i < elfFileHeader.E_SHNUM; i++)
                {
                    var shOffset = (long)elfFileHeader.E_SHOFF + (i * elfFileHeader.E_SHENTSIZE);
                    stream.Seek(shOffset, SeekOrigin.Begin);
                    var sectionHeader = new SectionHeader64();
                    sectionHeader.Read(stream);
                    metadata.Add(sectionHeader);
                }
            }

            return(metadata.ToImmutableList());
        }
Beispiel #4
0
        public static bool IsFileType(Stream stream)
        {
            if (!stream.CanRead)
            {
                throw new ArgumentException("Stream is not available for reading", nameof(stream));
            }
            if (!stream.CanSeek)
            {
                throw new ArgumentException("Stream is not available for seeking", nameof(stream));
            }

            if (stream.Position != 0)
            {
                stream.Seek(0, SeekOrigin.Begin);
            }

            var magicBuffer = new byte[MAGIC.Length];
            var bytesRead   = stream.Read(magicBuffer, 0, MAGIC.Length);

            if (bytesRead != MAGIC.Length)
            {
                return(false);
            }
            var magicMatch = bytesRead == MAGIC.Length && Enumerable.SequenceEqual(MAGIC, magicBuffer);

            if (!magicMatch)
            {
                return(false);
            }

            stream.Seek(0, SeekOrigin.Begin);
            Header64 potentialHeader;

            if (!Header64.TryRead(stream, out potentialHeader))
            {
                return(false);
            }
            return(potentialHeader.EI_CLASS == HeaderIdentityClass.ELFCLASS64);
        }