public void Save(String filename) { // initialize stream FileStream stream = File.Create(filename); // Initialize Elf endianness ElfEndian endian = new ElfEndian(elf.Header.Encoding); // Save Elf header ElfHDR hdr = elf.Header; Byte[] hdrData = new Byte[hdr.DataSize]; elf.Header.Save(endian, ref hdrData); stream.Write(hdrData, 0, hdrData.Length); // Save programs Byte[] phdrData = new Byte[hdr.ProgramHeaderSize]; stream.Seek((Int64)hdr.ProgramHeaderOffset, SeekOrigin.Begin); for (int i = 0; i < hdr.ProgramHeaderCount; i++) { // Save program headers elf.Programs[i].Header.Save(endian, hdr.Class, ref phdrData); stream.Write(phdrData, 0, phdrData.Length); } // Save sections Byte[] shdrData = new Byte[hdr.SectionHeaderSize]; stream.Seek((Int64)hdr.SectionHeaderOffset, SeekOrigin.Begin); for (int i = 0; i < hdr.SectionHeaderCount; i++) { // Save section header ElfSHDR shdr = elf.Sections[i].Header; ElfSectionType type = shdr.Type; shdr.Save(endian, hdr.Class, ref shdrData); stream.Write(shdrData, 0, shdrData.Length); if (type != ElfSectionType.SHT_NULL && type != ElfSectionType.SHT_NOBITS) { Byte[] section = elf.Sections[i].Section; //stream.Write(elf.Sections[i].Section, 0, ; } } for (int i = 0; i < hdr.SectionHeaderCount; i++) { // Save section data ElfSection section = elf.Sections[i]; ElfSectionType type = section.Header.Type; if (type != ElfSectionType.SHT_NULL && type != ElfSectionType.SHT_NOBITS) { stream.Seek((Int64)section.Header.FileOffset, SeekOrigin.Begin); stream.Write(section.Section, 0, (int)section.Header.Size); } } // Close stream stream.Close(); }
private ElfSection CreateElfSection(int sectionIndex, ElfSectionType sectionType, bool isNullSection) { ElfSection section = null; switch (sectionType) { case ElfSectionType.Null: if (isNullSection) { section = new ElfNullSection(); } break; case ElfSectionType.DynamicLinkerSymbolTable: case ElfSectionType.SymbolTable: section = new ElfSymbolTable(); break; case ElfSectionType.StringTable: if (sectionIndex == _sectionStringTableIndex) { _hasValidSectionStringTable = true; section = new ElfSectionHeaderStringTable(); } else { section = new ElfStringTable(); } break; case ElfSectionType.Relocation: case ElfSectionType.RelocationAddends: section = new ElfRelocationTable(); break; case ElfSectionType.Note: section = new ElfNoteTable(); break; } // If the section is not a builtin section, try to offload to a delegate // or use the default ElfCustomSection. if (section == null) { if (Options.TryCreateSection != null) { section = Options.TryCreateSection(sectionType, Diagnostics); } if (section == null) { section = new ElfBinarySection(); } } return(section); }
private void Write(Byte[] buffer, UInt32 address, Int32 count, Int32 offset) { if (count > 0) { for (int i = 0; i < elf.Header.SectionHeaderCount; i++) { ElfSection section = elf.Sections[i]; ElfSHDR shdr = section.Header; ElfSectionType type = shdr.Type; if (type != ElfSectionType.SHT_NULL && type != ElfSectionType.SHT_NOBITS) { if (address >= shdr.Address && address < (shdr.Address + shdr.Size)) { int soff = (int)(address - shdr.Address); int num = Math.Min(count, (int)shdr.Size - soff); Array.Copy(buffer, offset, section.Section, soff, num); Write(buffer, address + (uint)num, count - num, offset + num); return; } } } } }
public static bool Exist(this ElfSectionHeaderTable table, ElfSectionType type) => table.Any(x => x.Type == type);
private static string GetDefaultName(ElfSectionType type) { return(type == ElfSectionType.Relocation? DefaultName : DefaultNameWithAddends); }
protected ElfSection(ElfSectionType sectionType) { _type = sectionType; }