Ejemplo n.º 1
0
        private void LoadNotes()
        {
            if (_notes != null)
            {
                return;
            }

            LoadProgramHeaders();
            ElfProgramHeader noteHeader = _programHeaders.SingleOrDefault(ph => ph.Header.Type == ELFProgramHeaderType.Note);

            if (noteHeader == null)
            {
                _notes = new ElfNote[0];
                return;
            }

            List <ElfNote> notes = new List <ElfNote>();

            Reader reader   = new Reader(noteHeader.AddressSpace);
            long   position = 0;

            while (position < reader.DataSource.Length)
            {
                ElfNote note = new ElfNote(reader, position);
                notes.Add(note);

                position += note.TotalSize;
            }

            _notes = notes.ToArray();
        }
Ejemplo n.º 2
0
        private void LoadNotes()
        {
            if (_notes != null)
            {
                return;
            }

            LoadProgramHeaders();

            List <ElfNote> notes = new List <ElfNote>();

            foreach (ElfProgramHeader programHeader in _programHeaders)
            {
                if (programHeader.Header.Type == ElfProgramHeaderType.Note)
                {
                    Reader reader   = new Reader(programHeader.AddressSpace);
                    long   position = 0;
                    while (position < reader.DataSource.Length)
                    {
                        ElfNote note = new ElfNote(reader, position);
                        notes.Add(note);

                        position += note.TotalSize;
                    }
                }
            }

            _notes = notes.ToArray();
        }
Ejemplo n.º 3
0
        private void LoadFileTable()
        {
            if (_loadedImages != null)
            {
                return;
            }

            ElfNote fileNote = GetNotes(ELFNoteType.File).Single();

            long position             = 0;
            ELFFileTableHeader header = fileNote.ReadContents <ELFFileTableHeader>(ref position);

            ELFFileTableEntryPointers[]         fileTable = new ELFFileTableEntryPointers[header.EntryCount.ToInt32()];
            List <ElfLoadedImage>               images    = new List <ElfLoadedImage>(fileTable.Length);
            Dictionary <string, ElfLoadedImage> lookup    = new Dictionary <string, ElfLoadedImage>(fileTable.Length);


            for (int i = 0; i < fileTable.Length; i++)
            {
                fileTable[i] = fileNote.ReadContents <ELFFileTableEntryPointers>(ref position);
            }

            long size = fileNote.Header.ContentSize - position;

            byte[] bytes = fileNote.ReadContents(position, (int)size);
            int    start = 0;

            for (int i = 0; i < fileTable.Length; i++)
            {
                int end = start;
                while (bytes[end] != 0)
                {
                    end++;
                }

                string path = Encoding.ASCII.GetString(bytes, start, end - start);
                start = end + 1;

                if (!lookup.TryGetValue(path, out ElfLoadedImage image))
                {
                    image = lookup[path] = new ElfLoadedImage(path);
                }

                image.AddTableEntryPointers(fileTable[i]);
            }

            _loadedImages = lookup.Values.OrderBy(i => i.BaseAddress).ToArray();
        }
Ejemplo n.º 4
0
        private void LoadAuxvTable()
        {
            if (_auxvEntries.Count != 0)
            {
                return;
            }

            ElfNote auxvNote = GetNotes(ElfNoteType.Aux).SingleOrDefault();

            if (auxvNote is null)
            {
                throw new BadImageFormatException($"No auxv entries in coredump");
            }

            long position = 0;

            while (true)
            {
                ulong type;
                ulong value;
                if (ElfFile.Header.Is64Bit)
                {
                    var elfauxv64 = auxvNote.ReadContents <ElfAuxv64>(ref position);
                    type  = elfauxv64.Type;
                    value = elfauxv64.Value;
                }
                else
                {
                    var elfauxv32 = auxvNote.ReadContents <ElfAuxv32>(ref position);
                    type  = elfauxv32.Type;
                    value = elfauxv32.Value;
                }

                if (type == (ulong)ElfAuxvType.Null)
                {
                    break;
                }

                _auxvEntries.Add(type, value);
            }
        }
Ejemplo n.º 5
0
        private void LoadFileTable()
        {
            if (_loadedImages != null)
            {
                return;
            }

            ElfNote fileNote = GetNotes(ElfNoteType.File).Single();

            long  position   = 0;
            ulong entryCount = 0;

            if (ElfFile.Header.Is64Bit)
            {
                ElfFileTableHeader64 header = fileNote.ReadContents <ElfFileTableHeader64>(ref position);
                entryCount = header.EntryCount;
            }
            else
            {
                ElfFileTableHeader32 header = fileNote.ReadContents <ElfFileTableHeader32>(ref position);
                entryCount = header.EntryCount;
            }

            ElfFileTableEntryPointers64[]       fileTable = new ElfFileTableEntryPointers64[entryCount];
            List <ElfLoadedImage>               images    = new List <ElfLoadedImage>(fileTable.Length);
            Dictionary <string, ElfLoadedImage> lookup    = new Dictionary <string, ElfLoadedImage>(fileTable.Length);

            for (int i = 0; i < fileTable.Length; i++)
            {
                if (ElfFile.Header.Is64Bit)
                {
                    fileTable[i] = fileNote.ReadContents <ElfFileTableEntryPointers64>(ref position);
                }
                else
                {
                    ElfFileTableEntryPointers32 entry = fileNote.ReadContents <ElfFileTableEntryPointers32>(ref position);
                    fileTable[i].Start      = entry.Start;
                    fileTable[i].Stop       = entry.Stop;
                    fileTable[i].PageOffset = entry.PageOffset;
                }
            }

            long size = fileNote.Header.ContentSize - position;

            byte[] bytes = fileNote.ReadContents(position, (int)size);
            int    start = 0;

            for (int i = 0; i < fileTable.Length; i++)
            {
                int end = start;
                while (bytes[end] != 0)
                {
                    end++;
                }

                string path = Encoding.ASCII.GetString(bytes, start, end - start);
                start = end + 1;

                if (!lookup.TryGetValue(path, out ElfLoadedImage image))
                {
                    image = lookup[path] = new ElfLoadedImage(ElfFile.VirtualAddressReader, ElfFile.Header.Is64Bit, path);
                }

                image.AddTableEntryPointers(fileTable[i]);
            }

            _loadedImages = lookup.Values.OrderBy(i => i.BaseAddress).ToArray();
        }
Ejemplo n.º 6
0
        private ImmutableArray <ElfLoadedImage> LoadFileTable()
        {
            if (!_loadedImages.IsDefault)
            {
                return(_loadedImages);
            }

            ElfNote fileNote = GetNotes(ElfNoteType.File).Single();

            long  position   = 0;
            ulong entryCount = 0;

            if (ElfFile.Header.Is64Bit)
            {
                ElfFileTableHeader64 header = fileNote.ReadContents <ElfFileTableHeader64>(ref position);
                entryCount = header.EntryCount;
            }
            else
            {
                ElfFileTableHeader32 header = fileNote.ReadContents <ElfFileTableHeader32>(ref position);
                entryCount = header.EntryCount;
            }

            ElfFileTableEntryPointers64[]       fileTable = new ElfFileTableEntryPointers64[entryCount];
            Dictionary <string, ElfLoadedImage> lookup    = new Dictionary <string, ElfLoadedImage>(fileTable.Length);

            for (int i = 0; i < fileTable.Length; i++)
            {
                if (ElfFile.Header.Is64Bit)
                {
                    fileTable[i] = fileNote.ReadContents <ElfFileTableEntryPointers64>(ref position);
                }
                else
                {
                    ElfFileTableEntryPointers32 entry = fileNote.ReadContents <ElfFileTableEntryPointers32>(ref position);
                    fileTable[i].Start      = entry.Start;
                    fileTable[i].Stop       = entry.Stop;
                    fileTable[i].PageOffset = entry.PageOffset;
                }
            }

            int size = (int)(fileNote.Header.ContentSize - position);

            byte[] bytes = ArrayPool <byte> .Shared.Rent(size);

            try
            {
                int read  = fileNote.ReadContents(position, bytes);
                int start = 0;
                for (int i = 0; i < fileTable.Length; i++)
                {
                    int end = start;
                    while (bytes[end] != 0)
                    {
                        end++;
                    }

                    string path = Encoding.ASCII.GetString(bytes, start, end - start);
                    start = end + 1;

                    if (!lookup.TryGetValue(path, out ElfLoadedImage? image))
                    {
                        image = lookup[path] = new ElfLoadedImage(ElfFile.VirtualAddressReader, ElfFile.Header.Is64Bit, path);
                    }

                    image.AddTableEntryPointers(fileTable[i]);
                }
            }
            finally
            {
                ArrayPool <byte> .Shared.Return(bytes);
            }

            return(_loadedImages = lookup.Values.OrderBy(i => i.BaseAddress).ToImmutableArray());
        }