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!")); } }
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; }
public void Load(bool UserMode) { bool OK = true; try { bool DynamicLinkingRequired = false; ThreadStartMethod mainMethod = (ThreadStartMethod)Utilities.ObjectUtilities.GetObject(theFile.Header.EntryPoint); theProcess = ProcessManager.CreateProcess( mainMethod, theFile.TheFile.Name, UserMode); uint threadStackVirtAddr = (uint)((Thread)theProcess.Threads[0]).State->ThreadStackTop - 4092; uint threadStackPhysAddr = (uint)Hardware.VirtMemManager.GetPhysicalAddress(threadStackVirtAddr); ProcessManager.CurrentProcess.TheMemoryLayout.AddDataPage(threadStackPhysAddr, threadStackVirtAddr); // Load the ELF segments (i.e. the program code and data) BaseAddress = theFile.BaseAddress; LoadSegments(theFile, ref OK, ref DynamicLinkingRequired, BaseAddress); //BasicConsole.WriteLine(); #region Relocations // Useful articles / specifications on Relocations: // - Useful / practical explanation of various relocation types: http://eli.thegreenplace.net/2011/08/25/load-time-relocation-of-shared-libraries/#id20 // - Orcale : ELF Specification copy: http://docs.oracle.com/cd/E23824_01/html/819-0690/chapter6-54839.html if (DynamicLinkingRequired) { Console.Default.WriteLine("Dynamic Linking"); BasicConsole.WriteLine("Dynamic Linking"); ELFDynamicSection dynamicSection = theFile.DynamicSection; ELFDynamicSymbolTableSection dynamicSymbolsSection = theFile.DynamicSymbolsSection; ELFStringTable DynamicsStringTable = new ELFStringTable( dynamicSection.StrTabDynamic.Val_Ptr, dynamicSection.StrTabSizeDynamic.Val_Ptr); for (uint i = 0; i < dynamicSection.Dynamics.Count; i++) { ELFDynamicSection.Dynamic theDyn = dynamicSection[i]; //BasicConsole.WriteLine(" - Dynamic : "); //BasicConsole.Write(" - Tag : "); //BasicConsole.WriteLine((int)theDyn.Tag); //BasicConsole.Write(" - Value or Pointer : "); //BasicConsole.WriteLine(theDyn.Val_Ptr); if (theDyn.Tag == ELFDynamicSection.DynamicTag.Needed) { BasicConsole.Write(" - Needed library name : "); FOS_System.String libFullPath = DynamicsStringTable[theDyn.Val_Ptr]; Console.Default.WriteLine(libFullPath); BasicConsole.WriteLine(libFullPath); FOS_System.String libFileName = (FOS_System.String)libFullPath.Split('\\').Last(); libFileName = (FOS_System.String)libFileName.Split('/').Last(); FOS_System.String libTestPath = theFile.TheFile.Parent.GetFullPath() + libFileName; File sharedObjectFile = File.Open(libTestPath); if (sharedObjectFile == null) { Console.Default.WarningColour(); Console.Default.WriteLine("Failed to find needed library file!"); BasicConsole.WriteLine("Failed to find needed library file!"); Console.Default.DefaultColour(); OK = false; } else { Console.Default.WriteLine("Found library file. Loading library..."); BasicConsole.WriteLine("Found library file. Loading library..."); ELFSharedObject sharedObject = DynamicLinkerLoader.LoadLibrary_FromELFSO(sharedObjectFile, this); SharedObjectDependencies.Add(sharedObject); Console.Default.WriteLine("Library loaded."); BasicConsole.WriteLine("Library loaded."); } } } Console.Default.WriteLine("Library Relocations"); BasicConsole.WriteLine("Library Relocations"); // Perform relocation / dynamic linking of all libraries for (int i = 0; i < SharedObjectDependencies.Count; i++) { ELFSharedObject SO = (ELFSharedObject)SharedObjectDependencies[i]; //BasicConsole.WriteLine("Shared Object base address : " + (FOS_System.String)SO.BaseAddress); //BasicConsole.WriteLine("Shared Object file base address : " + (FOS_System.String)SO.TheFile.BaseAddress); List SOSections = SO.TheFile.Sections; for (int j = 0; j < SOSections.Count; j++) { ELFSection SOSection = (ELFSection)SOSections[j]; if (SOSection is ELFRelocationTableSection) { //BasicConsole.WriteLine(" - Normal Relocation"); ELFRelocationTableSection relocTableSection = (ELFRelocationTableSection)SOSection; ELFSymbolTableSection symbolTable = (ELFSymbolTableSection)SO.TheFile.Sections[relocTableSection.SymbolTableSectionIndex]; ELFStringTableSection symbolNamesTable = (ELFStringTableSection)SO.TheFile.Sections[symbolTable.StringsSectionIndex]; List Relocations = relocTableSection.Relocations; for (int k = 0; k < Relocations.Count; k++) { // Reference: http://docs.oracle.com/cd/E19683-01/817-3677/chapter6-26/index.html ELFRelocationTableSection.Relocation relocation = (ELFRelocationTableSection.Relocation)Relocations[k]; if (relocation.Type == ELFRelocationTableSection.RelocationType.R_386_NONE) { continue; } uint *resolvedRelLocation = (uint *)(SO.BaseAddress + (relocation.Offset - SO.TheFile.BaseAddress)); ELFSymbolTableSection.Symbol symbol = (ELFSymbolTableSection.Symbol)symbolTable[relocation.Symbol]; FOS_System.String symbolName = symbolNamesTable[symbol.NameIdx]; //BasicConsole.WriteLine("Relocation:"); ////BasicConsole.WriteLine(" > Symbol index : " + (FOS_System.String)relocation.Symbol); //BasicConsole.WriteLine(" > Type : " + (FOS_System.String)(uint)relocation.Type); //BasicConsole.WriteLine(" > Offset : " + (FOS_System.String)(uint)relocation.Offset); //BasicConsole.WriteLine(((FOS_System.String)" > Resolved location address: ") + (uint)resolvedRelLocation); ////BasicConsole.WriteLine(((FOS_System.String)" > Resolved location start value: ") + *resolvedRelLocation); //BasicConsole.Write(" > Symbol name : "); //BasicConsole.WriteLine(symbolName); uint newValue = 0; switch (relocation.Type) { case ELFRelocationTableSection.RelocationType.R_386_32: newValue = GetSymbolAddress(symbol, symbolName) + *resolvedRelLocation; break; case ELFRelocationTableSection.RelocationType.R_386_PC32: newValue = GetSymbolAddress(symbol, symbolName) + *resolvedRelLocation - (uint)resolvedRelLocation; break; case ELFRelocationTableSection.RelocationType.R_386_RELATIVE: newValue = SO.BaseAddress + *resolvedRelLocation; break; //TODO: Support more relocation types default: Console.Default.WarningColour(); Console.Default.Write("WARNING: Unrecognised relocation type! ("); Console.Default.Write_AsDecimal((uint)relocation.Type); Console.Default.WriteLine(")"); Console.Default.DefaultColour(); BasicConsole.Write("WARNING: Unrecognised relocation type! ("); BasicConsole.Write((uint)relocation.Type); BasicConsole.WriteLine(")"); break; } *resolvedRelLocation = newValue; //BasicConsole.WriteLine(" > New value: " + (FOS_System.String)(newValue)); //BasicConsole.WriteLine(" > Resolved location end value: " + (FOS_System.String)(*resolvedRelLocation)); } } else if (SOSection is ELFRelocationAddendTableSection) { //BasicConsole.WriteLine(" - Addend Relocation"); ELFRelocationAddendTableSection relocTableSection = (ELFRelocationAddendTableSection)SOSection; ELFSymbolTableSection symbolTable = (ELFSymbolTableSection)SO.TheFile.Sections[relocTableSection.SymbolTableSectionIndex]; ELFStringTableSection symbolNamesTable = (ELFStringTableSection)SO.TheFile.Sections[symbolTable.StringsSectionIndex]; List Relocations = relocTableSection.Relocations; for (int k = 0; k < Relocations.Count; k++) { ELFRelocationAddendTableSection.RelocationAddend relocation = (ELFRelocationAddendTableSection.RelocationAddend)Relocations[k]; if (relocation.Type == ELFRelocationTableSection.RelocationType.R_386_NONE) { continue; } ELFSymbolTableSection.Symbol symbol = (ELFSymbolTableSection.Symbol)symbolTable[relocation.Symbol]; FOS_System.String symbolName = symbolNamesTable[symbol.NameIdx]; uint *resolvedRelLocation = (uint *)(SO.BaseAddress + (relocation.Offset - SO.TheFile.BaseAddress)); //BasicConsole.WriteLine("Relocation:"); ////BasicConsole.WriteLine(" > Symbol index : " + (FOS_System.String)relocation.Symbol); //BasicConsole.WriteLine(" > Type : " + (FOS_System.String)(uint)relocation.Type); //BasicConsole.WriteLine(" > Offset : " + (FOS_System.String)(uint)relocation.Offset); //BasicConsole.WriteLine(((FOS_System.String)" > Resolved location address: ") + (uint)resolvedRelLocation); ////BasicConsole.WriteLine(((FOS_System.String)" > Resolved location start value: ") + *resolvedRelLocation); //BasicConsole.Write(" > Symbol name : "); //BasicConsole.WriteLine(symbolName); uint newValue = 0; switch (relocation.Type) { //TODO: Support more relocation types default: Console.Default.WarningColour(); Console.Default.Write("WARNING: Unrecognised relocation type! ("); Console.Default.Write_AsDecimal((uint)relocation.Type); Console.Default.WriteLine(")"); Console.Default.DefaultColour(); BasicConsole.Write("WARNING: Unrecognised relocation type! ("); BasicConsole.Write((uint)relocation.Type); BasicConsole.WriteLine(")"); break; } *resolvedRelLocation = newValue; //BasicConsole.WriteLine(" > New value: " + (FOS_System.String)(newValue)); //BasicConsole.WriteLine(" > Resolved location end value: " + (FOS_System.String)(*resolvedRelLocation)); } } } } Console.Default.WriteLine("Executable Relocations"); BasicConsole.WriteLine("Executable Relocations"); //BasicConsole.WriteLine("Executable base address : " + (FOS_System.String)BaseAddress); //BasicConsole.WriteLine("Executable file base address : " + (FOS_System.String)theFile.BaseAddress); // Perform dynamic linking of executable List ExeSections = theFile.Sections; for (int j = 0; j < ExeSections.Count; j++) { ELFSection ExeSection = (ELFSection)ExeSections[j]; if (ExeSection is ELFRelocationTableSection) { //BasicConsole.WriteLine(" - Normal Relocations"); ELFRelocationTableSection relocTableSection = (ELFRelocationTableSection)ExeSection; ELFSymbolTableSection symbolTable = (ELFSymbolTableSection)theFile.Sections[relocTableSection.SymbolTableSectionIndex]; ELFStringTableSection symbolNamesTable = (ELFStringTableSection)theFile.Sections[symbolTable.StringsSectionIndex]; List Relocations = relocTableSection.Relocations; for (int k = 0; k < Relocations.Count; k++) { ELFRelocationTableSection.Relocation relocation = (ELFRelocationTableSection.Relocation)Relocations[k]; if (relocation.Type == ELFRelocationTableSection.RelocationType.R_386_NONE) { continue; } uint *resolvedRelLocation = (uint *)(BaseAddress + (relocation.Offset - theFile.BaseAddress)); ELFSymbolTableSection.Symbol symbol = (ELFSymbolTableSection.Symbol)symbolTable[relocation.Symbol]; FOS_System.String symbolName = symbolNamesTable[symbol.NameIdx]; //BasicConsole.WriteLine("Relocation:"); ////BasicConsole.WriteLine(" > Symbol index : " + (FOS_System.String)relocation.Symbol); //BasicConsole.WriteLine(" > Type : " + (FOS_System.String)(uint)relocation.Type); //BasicConsole.WriteLine(" > Offset : " + (FOS_System.String)(uint)relocation.Offset); //BasicConsole.WriteLine(((FOS_System.String)" > Resolved location address: ") + (uint)resolvedRelLocation); ////BasicConsole.WriteLine(((FOS_System.String)" > Resolved location start value: ") + *resolvedRelLocation); //BasicConsole.Write(" > Symbol name : "); //BasicConsole.WriteLine(symbolName); bool setFromNewValue = true; uint newValue = 0; switch (relocation.Type) { //TODO: Support more relocation types case ELFRelocationTableSection.RelocationType.R_386_JMP_SLOT: newValue = GetSymbolAddress(symbol, symbolName); break; case ELFRelocationTableSection.RelocationType.R_386_COPY: // Created by the link-editor for dynamic executables to preserve a read-only text segment. // Its offset member refers to a location in a writable segment. The symbol table index // specifies a symbol that should exist both in the current object file and in a shared object. // During execution, the runtime linker copies data associated with the shared object's symbol // to the location specified by the offset. // See Copy Relocations: // http://docs.oracle.com/cd/E19683-01/817-3677/6mj8mbtbs/index.html#chapter4-84604 setFromNewValue = false; uint symbolAddress = 0; uint symbolSize = 0; if (GetSymbolAddressAndSize(symbol, symbolName, ref symbolAddress, ref symbolSize)) { byte *symbolValuePtr = (byte *)symbolAddress; //BasicConsole.Write(" > Symbol size : "); //BasicConsole.WriteLine(symbolSize); for (int i = 0; i < symbolSize; i++) { resolvedRelLocation[i] = symbolValuePtr[i]; } } else { BasicConsole.WriteLine("Failed to get symbol address and size for R_386_COPY relocation!"); } break; default: Console.Default.WarningColour(); Console.Default.Write("WARNING: Unrecognised relocation type! ("); Console.Default.Write_AsDecimal((uint)relocation.Type); Console.Default.WriteLine(")"); Console.Default.DefaultColour(); BasicConsole.Write("WARNING: Unrecognised relocation type! ("); BasicConsole.Write((uint)relocation.Type); BasicConsole.WriteLine(")"); break; } if (setFromNewValue) { *resolvedRelLocation = newValue; //BasicConsole.WriteLine(" > New value: " + (FOS_System.String)(newValue)); //BasicConsole.WriteLine(" > Resolved location end value: " + (FOS_System.String)(*resolvedRelLocation)); } } } else if (ExeSection is ELFRelocationAddendTableSection) { //BasicConsole.WriteLine(" - Addend Relocations"); ELFRelocationAddendTableSection relocTableSection = (ELFRelocationAddendTableSection)ExeSection; ELFSymbolTableSection symbolTable = (ELFSymbolTableSection)theFile.Sections[relocTableSection.SymbolTableSectionIndex]; ELFStringTableSection symbolNamesTable = (ELFStringTableSection)theFile.Sections[symbolTable.StringsSectionIndex]; List Relocations = relocTableSection.Relocations; for (int k = 0; k < Relocations.Count; k++) { ELFRelocationAddendTableSection.RelocationAddend relocation = (ELFRelocationAddendTableSection.RelocationAddend)Relocations[k]; if (relocation.Type == ELFRelocationTableSection.RelocationType.R_386_NONE) { continue; } uint *resolvedRelLocation = (uint *)(BaseAddress + (relocation.Offset - theFile.BaseAddress)); ELFSymbolTableSection.Symbol symbol = (ELFSymbolTableSection.Symbol)symbolTable[relocation.Symbol]; FOS_System.String symbolName = symbolNamesTable[symbol.NameIdx]; //BasicConsole.WriteLine("Relocation:"); ////BasicConsole.WriteLine(" > Symbol index : " + (FOS_System.String)relocation.Symbol); //BasicConsole.WriteLine(" > Type : " + (FOS_System.String)(uint)relocation.Type); //BasicConsole.WriteLine(" > Offset : " + (FOS_System.String)(uint)relocation.Offset); //BasicConsole.WriteLine(((FOS_System.String)" > Resolved location address: ") + (uint)resolvedRelLocation); ////BasicConsole.WriteLine(((FOS_System.String)" > Resolved location start value: ") + *resolvedRelLocation); //BasicConsole.Write(" > Symbol name : "); //BasicConsole.WriteLine(symbolName); uint newValue = 0; switch (relocation.Type) { //TODO: Support more relocation types default: Console.Default.WarningColour(); Console.Default.Write("WARNING: Unrecognised relocation type! ("); Console.Default.Write_AsDecimal((uint)relocation.Type); Console.Default.WriteLine(")"); Console.Default.DefaultColour(); BasicConsole.Write("WARNING: Unrecognised relocation type! ("); BasicConsole.Write((uint)relocation.Type); BasicConsole.WriteLine(")"); break; } *resolvedRelLocation = newValue; //BasicConsole.WriteLine(" > New value: " + (FOS_System.String)(newValue)); //BasicConsole.WriteLine(" > Resolved location end value: " + (FOS_System.String)(*resolvedRelLocation)); } } } // TODO: Call Init functions of libraries } // Unmap processes' memory from current processes' memory for (int i = 0; i < SharedObjectDependencies.Count; i++) { ELFSharedObject SO = (ELFSharedObject)SharedObjectDependencies[i]; uint FileBaseAddress = SO.TheFile.BaseAddress; uint MemBaseAddress = SO.BaseAddress; List SOSegments = SO.TheFile.Segments; for (int j = 0; j < SOSegments.Count; j++) { ELFSegment SOSegment = (ELFSegment)SOSegments[j]; ProcessManager.CurrentProcess.TheMemoryLayout.RemovePage( (MemBaseAddress + ((uint)SOSegment.Header.VAddr - FileBaseAddress)) & 0xFFFFF000); } } { uint FileBaseAddress = theFile.BaseAddress; uint MemBaseAddress = BaseAddress; List ExeSegments = theFile.Segments; for (int j = 0; j < ExeSegments.Count; j++) { ELFSegment ExeSegment = (ELFSegment)ExeSegments[j]; ProcessManager.CurrentProcess.TheMemoryLayout.RemovePage( (MemBaseAddress + ((uint)ExeSegment.Header.VAddr - FileBaseAddress)) & 0xFFFFF000); } } #endregion ProcessManager.CurrentProcess.TheMemoryLayout.RemovePage(threadStackVirtAddr); } finally { if (!OK) { theProcess = null; } } }
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; }
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!")); } }