public static ELFSection GetSection(ELFSectionHeader header) { if (header.SectionType == ElfSectionTypes.StrTab) { return(new ELFStringTableSection(header)); } else if (header.SectionType == ElfSectionTypes.SymTab) { return(new ELFSymbolTableSection(header)); } else if (header.SectionType == ElfSectionTypes.DynSym) { return(new ELFDynamicSymbolTableSection(header)); } else if (header.SectionType == ElfSectionTypes.Rel) { return(new ELFRelocationTableSection(header)); } else if (header.SectionType == ElfSectionTypes.RelA) { return(new ELFRelocationAddendTableSection(header)); } else if (header.SectionType == ElfSectionTypes.Dynamic) { return(new ELFDynamicSection(header)); } return(new ELFSection(header)); }
public ELFDynamicSection(ELFSectionHeader header) : base(header) { }
public ELFRelocationAddendTableSection(ELFSectionHeader header) : base(header) { }
public ELFDynamicSymbolTableSection(ELFSectionHeader header) : base(header) { }
public ELFStringTableSection(ELFSectionHeader header) : base(header) { }
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); // TODO: Use system call not direct call //} #endregion } else { ExceptionMethods.Throw(new FOS_System.Exception("Failed to read sections table data from file!")); } }
protected ELFSection(ELFSectionHeader aHeader) { header = aHeader; }
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!")); } }
public static ELFSection GetSection(ELFSectionHeader header) { if (header.SectionType == ElfSectionTypes.StrTab) { return new ELFStringTableSection(header); } else if (header.SectionType == ElfSectionTypes.SymTab) { return new ELFSymbolTableSection(header); } else if (header.SectionType == ElfSectionTypes.DynSym) { return new ELFDynamicSymbolTableSection(header); } else if (header.SectionType == ElfSectionTypes.Rel) { return new ELFRelocationTableSection(header); } else if (header.SectionType == ElfSectionTypes.RelA) { return new ELFRelocationAddendTableSection(header); } else if (header.SectionType == ElfSectionTypes.Dynamic) { return new ELFDynamicSection(header); } return new ELFSection(header); }