예제 #1
0
파일: ELFProcess.cs 프로젝트: kztao/FlingOS
 public uint GetSymbolAddress(ELFDynamicSymbolTableSection.Symbol theSymbol, FOS_System.String theSymbolName)
 {
     uint address = 0;
     uint size = 0;
     GetSymbolAddressAndSize(theSymbol, theSymbolName, ref address, ref size);
     return address;
 }
예제 #2
0
파일: ELFProcess.cs 프로젝트: kztao/FlingOS
        public bool GetSymbolAddressAndSize(ELFDynamicSymbolTableSection.Symbol theSymbol, FOS_System.String theSymbolName, ref uint address, ref uint size)
        {
            //BasicConsole.WriteLine("Searching for symbol...");
            //BasicConsole.Write("     - Name : ");
            //BasicConsole.WriteLine(theSymbolName);

            //BasicConsole.WriteLine("     Searching executable's symbols...");
            for (int i = 0; i < theFile.Sections.Count; i++)
            {
                ELFSection aSection = (ELFSection)theFile.Sections[i];
                if (aSection is ELFSymbolTableSection)
                {
                    ELFSymbolTableSection symTabSection = (ELFSymbolTableSection)aSection;
                    ELFStringTableSection strTabSection = (ELFStringTableSection)theFile.Sections[symTabSection.StringsSectionIndex];

                    for (int j = 0; j < symTabSection.Symbols.Count; j++)
                    {
                        ELFSymbolTableSection.Symbol aSymbol = (ELFSymbolTableSection.Symbol)symTabSection.Symbols[j];

                        if (aSymbol.Type == theSymbol.Type &&
                            aSymbol.Binding == ELFSymbolTableSection.SymbolBinding.Global &&
                            aSymbol.SectionIndex > 0)
                        {
                            if (strTabSection.IsMatch(aSymbol.NameIdx, theSymbolName))
                            {
                                //BasicConsole.WriteLine("     Found symbol.");
                                //BasicConsole.Write("     aSymbol Address : ");
                                uint result = ((uint)aSymbol.Value - theFile.BaseAddress) + BaseAddress;
                                //BasicConsole.WriteLine(result);

                                address = result;
                                size = aSymbol.Size;
                                return true;
                            }
                        }
                    }
                }
            }
            for (int k = 0; k < SharedObjectDependencies.Count; k++)
            {
                //BasicConsole.WriteLine("     Searching shared object's symbols...");

                ELFSharedObject SO = (ELFSharedObject)SharedObjectDependencies[k];
                for (int i = 0; i < SO.TheFile.Sections.Count; i++)
                {
                    ELFSection aSection = (ELFSection)SO.TheFile.Sections[i];
                    if (aSection is ELFSymbolTableSection)
                    {
                        ELFSymbolTableSection symTabSection = (ELFSymbolTableSection)aSection;
                        ELFStringTableSection strTabSection = (ELFStringTableSection)SO.TheFile.Sections[symTabSection.StringsSectionIndex];
                        for (int j = 0; j < symTabSection.Symbols.Count; j++)
                        {
                            ELFSymbolTableSection.Symbol aSymbol = (ELFSymbolTableSection.Symbol)symTabSection.Symbols[j];
                            if (aSymbol.Type == theSymbol.Type &&
                                aSymbol.Binding == ELFSymbolTableSection.SymbolBinding.Global &&
                                aSymbol.SectionIndex > 0)
                            {
                                if (strTabSection.IsMatch(aSymbol.NameIdx, theSymbolName))
                                {
                                    //BasicConsole.WriteLine("     Found symbol.");
                                    //BasicConsole.Write("     aSymbol Address : ");
                                    uint result = ((uint)aSymbol.Value - SO.TheFile.BaseAddress) + SO.BaseAddress;
                                    //BasicConsole.WriteLine(result);

                                    address = result;
                                    size = aSymbol.Size;
                                    return true;
                                }
                            }
                        }
                    }
                }
            }

            return false;
        }
예제 #3
0
파일: ELFFile.cs 프로젝트: kztao/FlingOS
        public void ReadSectionHeaders()
        {
            byte[] sectionsData = new byte[header.SecHeaderEntrySize * header.SecHeaderNumEntries];
            theStream.Position = header.SecHeaderTableOffset;
            int bytesRead = theStream.Read(sectionsData, 0, sectionsData.Length);

            if (bytesRead == sectionsData.Length)
            {
                uint offset = 0;
                while (offset < sectionsData.Length)
                {
                    ELFSectionHeader newHeader = new ELFSectionHeader(sectionsData, ref offset);
                    ELFSection newSection = ELFSection.GetSection(newHeader);

                    if (Sections.Count == header.SecHeaderIdxForSecNameStrings)
                    {
                        if (!(newSection is ELFStringTableSection))
                        {
                            ExceptionMethods.Throw(new FOS_System.Exception("Expected Strings Table section was not a strings table section!"));
                        }
                        NamesTable = (ELFStringTableSection)newSection;
                    }

                    newSection.Read(theStream);

                    if (newSection is ELFDynamicSection)
                    {
                        DynamicSection = (ELFDynamicSection)newSection;
                    }
                    else if (newSection is ELFDynamicSymbolTableSection)
                    {
                        DynamicSymbolsSection = (ELFDynamicSymbolTableSection)newSection;
                    }

                    Sections.Add(newSection);
                }

                //BasicConsole.WriteLine();

                #region Sections Output

                //for (int i = 0; i < Sections.Count; i++)
                //{
                //    ELFSection theSection = (ELFSection)Sections[i];
                //    ELFSectionHeader theHeader = theSection.Header;
                //    BasicConsole.WriteLine("ELF section: ");
                //    BasicConsole.Write(" - Name index : ");
                //    BasicConsole.WriteLine(theHeader.NameIndex);
                //    BasicConsole.Write(" - Name : ");
                //    BasicConsole.WriteLine(NamesTable[theHeader.NameIndex]);
                //    BasicConsole.Write(" - Type : ");
                //    BasicConsole.WriteLine((uint)theHeader.SectionType);
                //    BasicConsole.Write(" - Flags : ");
                //    BasicConsole.WriteLine((uint)theHeader.Flags);
                //    BasicConsole.Write(" - Offset : ");
                //    BasicConsole.WriteLine(theHeader.SectionFileOffset);
                //    BasicConsole.Write(" - Size : ");
                //    BasicConsole.WriteLine(theHeader.SectionSize);
                //    BasicConsole.Write(" - Load address : ");
                //    BasicConsole.WriteLine((uint)theHeader.LoadAddress);

                //    if (theSection is ELFSymbolTableSection)
                //    {
                //        #region ELFSymbolTableSection

                //        BasicConsole.WriteLine(" - Symbol table :");

                //        ELFSymbolTableSection theSymTable = (ELFSymbolTableSection)theSection;
                //        ELFStringTableSection theStringTable = (ELFStringTableSection)(Sections[theSymTable.StringsSectionIndex]);
                //        for (uint j = 0; j < theSymTable.Symbols.Count; j++)
                //        {
                //            ELFSymbolTableSection.Symbol theSym = theSymTable[j];

                //            BasicConsole.Write("     - Symbol : ");
                //            BasicConsole.WriteLine(theStringTable[theSym.NameIdx]);
                //            BasicConsole.Write("         - Type : ");
                //            BasicConsole.WriteLine((uint)theSym.Type);
                //            BasicConsole.Write("         - Binding : ");
                //            BasicConsole.WriteLine((uint)theSym.Binding);
                //            BasicConsole.Write("         - Section index : ");
                //            BasicConsole.WriteLine(theSym.SectionIndex);
                //            BasicConsole.Write("         - Value : ");
                //            BasicConsole.WriteLine((uint)theSym.Value);
                //            BasicConsole.Write("         - Size : ");
                //            BasicConsole.WriteLine(theSym.Size);
                //        }

                //        #endregion
                //    }
                //    else if (theSection is ELFRelocationAddendTableSection)
                //    {
                //        #region ELFRelocationAddendTableSection

                //        ELFRelocationAddendTableSection theRelASection = (ELFRelocationAddendTableSection)theSection;

                //        BasicConsole.WriteLine(" - Relocation (with addends) table :");
                //        BasicConsole.Write("     - Symbol table index : ");
                //        BasicConsole.WriteLine(theRelASection.SymbolTableSectionIndex);
                //        BasicConsole.Write("     - Section to relocate index : ");
                //        BasicConsole.WriteLine(theRelASection.SectionToRelocateIndex);

                //        for (uint j = 0; j < theRelASection.Relocations.Count; j++)
                //        {
                //            ELFRelocationAddendTableSection.RelocationAddend theRel = theRelASection[j];

                //            BasicConsole.WriteLine("     - Relocation : ");
                //            BasicConsole.Write("         - Type : ");
                //            BasicConsole.WriteLine((uint)theRel.Type);
                //            BasicConsole.Write("         - Symbol : ");
                //            BasicConsole.WriteLine(theRel.Symbol);
                //            BasicConsole.Write("         - Offset : ");
                //            BasicConsole.WriteLine((uint)theRel.Offset);
                //            BasicConsole.Write("         - Addend : ");
                //            BasicConsole.WriteLine(theRel.Addend);
                //        }

                //        #endregion
                //    }
                //    else if (theSection is ELFRelocationTableSection)
                //    {
                //        #region ELFRelocationTableSection

                //        ELFRelocationTableSection theRelSection = (ELFRelocationTableSection)theSection;

                //        BasicConsole.WriteLine(" - Relocation table :");
                //        BasicConsole.Write("     - Symbol table index : ");
                //        BasicConsole.WriteLine(theRelSection.SymbolTableSectionIndex);
                //        BasicConsole.Write("     - Section to relocate index : ");
                //        BasicConsole.WriteLine(theRelSection.SectionToRelocateIndex);

                //        for (uint j = 0; j < theRelSection.Relocations.Count; j++)
                //        {
                //            ELFRelocationTableSection.Relocation theRel = theRelSection[j];

                //            BasicConsole.WriteLine("     - Relocation : ");
                //            BasicConsole.Write("         - Type : ");
                //            BasicConsole.WriteLine((uint)theRel.Type);
                //            BasicConsole.Write("         - Symbol : ");
                //            BasicConsole.WriteLine(theRel.Symbol);
                //            BasicConsole.Write("         - Offset : ");
                //            BasicConsole.WriteLine((uint)theRel.Offset);
                //        }

                //        #endregion
                //    }
                //    if (theSection is ELFDynamicSection)
                //    {
                //        #region ELFDynamicSection

                //        BasicConsole.WriteLine(" - Dynamics table :");

                //        ELFDynamicSection theDynTable = (ELFDynamicSection)theSection;
                //        ELFDynamicSection.Dynamic StrTabDynamic = theDynTable.StrTabDynamic;
                //        ELFDynamicSection.Dynamic StrTabSizeDynamic = theDynTable.StrTabSizeDynamic;
                //        if (StrTabDynamic == null ||
                //            StrTabSizeDynamic == null)
                //        {
                //            Console.Default.WarningColour();
                //            BasicConsole.WriteLine("WARNING: Dynamic Table's String Table not found!");
                //            Console.Default.DefaultColour();
                //        }
                //        else
                //        {
                //            BasicConsole.Write("     - String table offset : ");
                //            BasicConsole.WriteLine(StrTabDynamic.Val_Ptr);
                //            BasicConsole.Write("     - String table size : ");
                //            BasicConsole.WriteLine(StrTabSizeDynamic.Val_Ptr);

                //            for (uint j = 0; j < theDynTable.Dynamics.Count; j++)
                //            {
                //                ELFDynamicSection.Dynamic theDyn = theDynTable[j];

                //                BasicConsole.WriteLine("     - Dynamic : ");
                //                BasicConsole.Write("         - Tag : ");
                //                BasicConsole.WriteLine((int)theDyn.Tag);
                //                BasicConsole.Write("         - Value or Pointer : ");
                //                BasicConsole.WriteLine(theDyn.Val_Ptr);
                //            }
                //        }

                //        #endregion
                //    }

                //    Hardware.Processes.Thread.Sleep(500);
                //}

                #endregion
            }
            else
            {
                ExceptionMethods.Throw(new FOS_System.Exception("Failed to read sections table data from file!"));
            }
        }