Пример #1
0
        protected bool LoadElf( BinaryReader reader )
        {
            NativeElfEhdr ehdr = new NativeElfEhdr( reader );
            if( ehdr.Magic != NativeElfEhdr.ElfMagic )
            {
                Debug.WriteLine( "ElfFile: elf magic number invalid" );
                return false;
            }
            if( ehdr.Machine != ElfMachineMips )
            {
                Debug.WriteLine( "ElfFile: machine version invalid" );
                return false;
            }

            _programType = ( ElfType )ehdr.Type;
            _entryAddress = ehdr.Entry;

            // Easy test for relocation:
            if( _entryAddress < 0x08000000 )
                _needsRelocation = true;

            reader.BaseStream.Seek( ehdr.Phoff, SeekOrigin.Begin );
            List<NativeElfPhdr> phdrs = new List<NativeElfPhdr>();
            for( int n = 0; n < ehdr.Phnum; n++ )
            {
                NativeElfPhdr phdr = new NativeElfPhdr( reader );
                phdrs.Add( phdr );
            }

            for( int n = 0; n < ehdr.Shnum; n++ )
            {
                reader.BaseStream.Seek( ehdr.Shoff + ( n * ehdr.Shentsize ), SeekOrigin.Begin );
                NativeElfShdr shdr = new NativeElfShdr( reader );

                ElfSection section = new ElfSection();
                section.NameIndex = shdr.Name;
                section.ElfOffset = shdr.Offset;
                section.Flags = ( ElfSectionFlags )shdr.Flags;
                section.LinkInfo = shdr.Info;
                section.SectionType = ( ElfSectionType )shdr.Type;
                section.Address = shdr.Address;
                section.AddressAlignment = shdr.AddressAlignment;
                section.Length = shdr.Size;

                _sections.Add( section );
                if( ( section.Flags & ElfSectionFlags.Alloc ) == ElfSectionFlags.Alloc )
                    _allocSections.Add( section );

                if( ( ( _programType == ElfType.Executable ) &&
                        ( section.SectionType == ElfSectionType.Relocation ) ) ||
                        ( ( _programType == ElfType.Prx ) &&
                        ( section.SectionType == ElfSectionType.PrxReloc ) ) )
                    _relocSections.Add( section );
            }

            uint nameBase = _sections[ ehdr.Shstrndx ].ElfOffset;
            foreach( ElfSection section in _sections )
            {
                reader.BaseStream.Seek( nameBase + section.NameIndex, SeekOrigin.Begin );
                section.Name = ReadString( reader );

                if( ( ( section.SectionType == ElfSectionType.Relocation ) ||
                    ( section.SectionType == ElfSectionType.PrxReloc ) ) &&
                    ( ( _sections[ ( int )section.LinkInfo ].Flags & ElfSectionFlags.Alloc ) != 0 ) )
                {
                    section.Reference = _sections[ ( int )section.LinkInfo ];
                    //_needsRelocation = true;
                }

                if( ( section.Name != null ) &&
                    ( section.Name.Length > 0 ) )
                    _sectionLookup.Add( section.Name, section );
            }

            // Not sure if this is important
            if( _sectionLookup.ContainsKey( ".init" ) == true )
                _initAddress = _sectionLookup[ ".init" ].Address;
            else
                _initAddress = 0x0;

            if( _sectionLookup.ContainsKey( ".symtab" ) == true )
            {
                ElfSection symtab = _sectionLookup[ ".symtab" ];
                ElfSection strtab = _sectionLookup[ ".strtab" ];
                for( int n = 0; n < symtab.Length / 16; n++ )
                {
                    reader.BaseStream.Seek( symtab.ElfOffset + ( 16 * n ), SeekOrigin.Begin );
                    NativeElfSym sym = new NativeElfSym( reader );

                    ElfSymbol symbol = new ElfSymbol();
                    symbol.Index = sym.SectionIndex; // May be ElfAbsoluteSymbol
                    symbol.Size = sym.Size;
                    symbol.Value = sym.Value;
                    symbol.Binding = ( ElfSymbolBinding )( sym.Info >> 4 );
                    symbol.SymbolType = ( ElfSymbolType )( sym.Info & 0xF );

                    reader.BaseStream.Seek( strtab.ElfOffset + sym.Name, SeekOrigin.Begin );
                    symbol.Name = ReadString( reader );

                    _symbols.Add( symbol );

                    if( symbol.Index != ElfAbsoluteSymbol )
                    {
                        ElfSection symbolParent = _sections[ ( int )symbol.Index ];
                        List<ElfSymbol> syms;
                        if( _symbolLookup.ContainsKey( symbolParent ) == true )
                            syms = _symbolLookup[ symbolParent ];
                        else
                        {
                            syms = new List<ElfSymbol>();
                            _symbolLookup.Add( symbolParent, syms );
                        }
                        syms.Add( symbol );
                    }
                }
            }

            //foreach( ElfSection section in _sections )
            //{
            //	Debugger.Break();
            //    if( ( _programType == ElfType.Executable ) &&
            //        ( section.SectionType != ElfSectionType.Relocation ) )
            //        continue;
            //    if( ( _programType == ElfType.Prx ) &&
            //        ( section.SectionType != ElfSectionType.PrxReloc ) )
            //        continue;
            //    for( int n = 0; n < section.Length / 8; n++ )
            //    {
            //        reader.BaseStream.Seek( section.ElfOffset + ( 8 * n ), SeekOrigin.Begin );
            //        NativeElfRel rel = new NativeElfRel( reader );

            //        ElfRelocation relocation = new ElfRelocation();
            //        relocation.Section = section;
            //        relocation.Offset = rel.Offset;
            //        relocation.BaseAddress = ( rel.Info >> 16 ) & 0xFF;
            //        relocation.Symbol = rel.Info >> 8;
            //        relocation.RelocationType = ( ElfRelocationType )( byte )( rel.Info & 0xFF );

            //        _relocations.Add( relocation );
            //    }
            //}

            return true;
        }
Пример #2
0
        protected bool LoadElf(BinaryReader reader)
        {
            NativeElfEhdr ehdr = new NativeElfEhdr(reader);

            if (ehdr.Magic != NativeElfEhdr.ElfMagic)
            {
                Debug.WriteLine("ElfFile: elf magic number invalid");
                return(false);
            }
            if (ehdr.Machine != ElfMachineMips)
            {
                Debug.WriteLine("ElfFile: machine version invalid");
                return(false);
            }

            _programType  = ( ElfType )ehdr.Type;
            _entryAddress = ehdr.Entry;

            // Easy test for relocation:
            if (_entryAddress < 0x08000000)
            {
                _needsRelocation = true;
            }

            reader.BaseStream.Seek(ehdr.Phoff, SeekOrigin.Begin);
            List <NativeElfPhdr> phdrs = new List <NativeElfPhdr>();

            for (int n = 0; n < ehdr.Phnum; n++)
            {
                NativeElfPhdr phdr = new NativeElfPhdr(reader);
                phdrs.Add(phdr);
            }

            for (int n = 0; n < ehdr.Shnum; n++)
            {
                reader.BaseStream.Seek(ehdr.Shoff + (n * ehdr.Shentsize), SeekOrigin.Begin);
                NativeElfShdr shdr = new NativeElfShdr(reader);

                ElfSection section = new ElfSection();
                section.NameIndex        = shdr.Name;
                section.ElfOffset        = shdr.Offset;
                section.Flags            = ( ElfSectionFlags )shdr.Flags;
                section.LinkInfo         = shdr.Info;
                section.SectionType      = ( ElfSectionType )shdr.Type;
                section.Address          = shdr.Address;
                section.AddressAlignment = shdr.AddressAlignment;
                section.Length           = shdr.Size;

                _sections.Add(section);
                if ((section.Flags & ElfSectionFlags.Alloc) == ElfSectionFlags.Alloc)
                {
                    _allocSections.Add(section);
                }

                if (((_programType == ElfType.Executable) &&
                     (section.SectionType == ElfSectionType.Relocation)) ||
                    ((_programType == ElfType.Prx) &&
                     (section.SectionType == ElfSectionType.PrxReloc)))
                {
                    _relocSections.Add(section);
                }
            }

            uint nameBase = _sections[ehdr.Shstrndx].ElfOffset;

            foreach (ElfSection section in _sections)
            {
                reader.BaseStream.Seek(nameBase + section.NameIndex, SeekOrigin.Begin);
                section.Name = ReadString(reader);

                if (((section.SectionType == ElfSectionType.Relocation) ||
                     (section.SectionType == ElfSectionType.PrxReloc)) &&
                    ((_sections[( int )section.LinkInfo].Flags & ElfSectionFlags.Alloc) != 0))
                {
                    section.Reference = _sections[( int )section.LinkInfo];
                    //_needsRelocation = true;
                }

                if ((section.Name != null) &&
                    (section.Name.Length > 0))
                {
                    _sectionLookup.Add(section.Name, section);
                }
            }

            // Not sure if this is important
            if (_sectionLookup.ContainsKey(".init") == true)
            {
                _initAddress = _sectionLookup[".init"].Address;
            }
            else
            {
                _initAddress = 0x0;
            }

            if (_sectionLookup.ContainsKey(".symtab") == true)
            {
                ElfSection symtab = _sectionLookup[".symtab"];
                ElfSection strtab = _sectionLookup[".strtab"];
                for (int n = 0; n < symtab.Length / 16; n++)
                {
                    reader.BaseStream.Seek(symtab.ElfOffset + (16 * n), SeekOrigin.Begin);
                    NativeElfSym sym = new NativeElfSym(reader);

                    ElfSymbol symbol = new ElfSymbol();
                    symbol.Index      = sym.SectionIndex;                // May be ElfAbsoluteSymbol
                    symbol.Size       = sym.Size;
                    symbol.Value      = sym.Value;
                    symbol.Binding    = ( ElfSymbolBinding )(sym.Info >> 4);
                    symbol.SymbolType = ( ElfSymbolType )(sym.Info & 0xF);

                    reader.BaseStream.Seek(strtab.ElfOffset + sym.Name, SeekOrigin.Begin);
                    symbol.Name = ReadString(reader);

                    _symbols.Add(symbol);

                    if (symbol.Index != ElfAbsoluteSymbol)
                    {
                        ElfSection       symbolParent = _sections[( int )symbol.Index];
                        List <ElfSymbol> syms;
                        if (_symbolLookup.ContainsKey(symbolParent) == true)
                        {
                            syms = _symbolLookup[symbolParent];
                        }
                        else
                        {
                            syms = new List <ElfSymbol>();
                            _symbolLookup.Add(symbolParent, syms);
                        }
                        syms.Add(symbol);
                    }
                }
            }

            //foreach( ElfSection section in _sections )
            //{
            //	Debugger.Break();
            //    if( ( _programType == ElfType.Executable ) &&
            //        ( section.SectionType != ElfSectionType.Relocation ) )
            //        continue;
            //    if( ( _programType == ElfType.Prx ) &&
            //        ( section.SectionType != ElfSectionType.PrxReloc ) )
            //        continue;
            //    for( int n = 0; n < section.Length / 8; n++ )
            //    {
            //        reader.BaseStream.Seek( section.ElfOffset + ( 8 * n ), SeekOrigin.Begin );
            //        NativeElfRel rel = new NativeElfRel( reader );

            //        ElfRelocation relocation = new ElfRelocation();
            //        relocation.Section = section;
            //        relocation.Offset = rel.Offset;
            //        relocation.BaseAddress = ( rel.Info >> 16 ) & 0xFF;
            //        relocation.Symbol = rel.Info >> 8;
            //        relocation.RelocationType = ( ElfRelocationType )( byte )( rel.Info & 0xFF );

            //        _relocations.Add( relocation );
            //    }
            //}

            return(true);
        }