public Elf(Stream stream) : base(stream) { elf_header = new elf_header(); elf_header.m_dwFormat = ReadUInt32(); elf_header.m_arch = ReadByte(); if (elf_header.m_arch == 2)//64 { throw new Exception("ERROR: 64 bit not supported."); } elf_header.m_endian = ReadByte(); elf_header.m_version = ReadByte(); elf_header.m_osabi = ReadByte(); elf_header.m_osabi_ver = ReadByte(); elf_header.e_pad = ReadBytes(7); elf_header.e_type = ReadUInt16(); elf_header.e_machine = ReadUInt16(); elf_header.e_version = ReadUInt32(); elf_header.e_entry = ReadUInt32(); elf_header.e_phoff = ReadUInt32(); elf_header.e_shoff = ReadUInt32(); elf_header.e_flags = ReadUInt32(); elf_header.e_ehsize = ReadUInt16(); elf_header.e_phentsize = ReadUInt16(); elf_header.e_phnum = ReadUInt16(); elf_header.e_shentsize = ReadUInt16(); elf_header.e_shnum = ReadUInt16(); elf_header.e_shtrndx = ReadUInt16(); program_table_element = ReadClassArray <program_header_table>(elf_header.e_phoff, elf_header.e_phnum); }
public ELF(string elf_file) { //Load ELF file error = 0; FileStream file = new FileStream(elf_file, FileMode.Open); elf = new byte[file.Length]; file.Read(elf, 0, (int)file.Length); file.Close(); //Parse ELF //Read Header hdr = new elf_header(); MemoryStream elf_data = new MemoryStream(elf); elf_data.Seek(0, SeekOrigin.Begin); hdr.e_hdr_magic = ReadUInt32(elf_data, 2); hdr.e_hdr_class = elf[4]; hdr.e_hdr_data = elf[5]; hdr.e_hdr_version = elf[6]; if (hdr.e_hdr_magic != 0x7F454C46) { //Not ELF file Console.WriteLine(hdr.e_hdr_magic.ToString("x")); error = 1; return; } if (hdr.e_hdr_class != 1) { //Is not 32-bit ELF error = 2; return; } elf_data.Seek(0x18, SeekOrigin.Begin); hdr.e_entry = ReadUInt32(elf_data, hdr.e_hdr_data); hdr.e_phoff = ReadUInt32(elf_data, hdr.e_hdr_data); hdr.e_shoff = ReadUInt32(elf_data, hdr.e_hdr_data); hdr.e_flags = ReadUInt32(elf_data, hdr.e_hdr_data); hdr.e_ehsize = ReadUInt16(elf_data, hdr.e_hdr_data); hdr.e_phentsize = ReadUInt16(elf_data, hdr.e_hdr_data); hdr.e_phnum = ReadUInt16(elf_data, hdr.e_hdr_data); hdr.e_shentsize = ReadUInt16(elf_data, hdr.e_hdr_data); hdr.e_shnum = ReadUInt16(elf_data, hdr.e_hdr_data); hdr.e_shstrndx = ReadUInt16(elf_data, hdr.e_hdr_data); //Read Section Headers section_hdrs = new List <section_header>(); for (int i = 0; i < hdr.e_shnum; i++) { elf_data.Seek(hdr.e_shoff + hdr.e_shentsize * i, SeekOrigin.Begin); section_header section = new section_header(); section.sh_name_off = ReadUInt32(elf_data, hdr.e_hdr_data); section.sh_type = ReadUInt32(elf_data, hdr.e_hdr_data); section.sh_flags = ReadUInt32(elf_data, hdr.e_hdr_data); section.sh_addr = ReadUInt32(elf_data, hdr.e_hdr_data); section.sh_offset = ReadUInt32(elf_data, hdr.e_hdr_data); section.sh_size = ReadUInt32(elf_data, hdr.e_hdr_data); section.sh_link = ReadUInt32(elf_data, hdr.e_hdr_data); section.sh_info = ReadUInt32(elf_data, hdr.e_hdr_data); section.sh_addralign = ReadUInt32(elf_data, hdr.e_hdr_data); section.sh_entsize = ReadUInt32(elf_data, hdr.e_hdr_data); section_hdrs.Add(section); } //Get Names for (int i = 0; i < section_hdrs.Count; i++) { elf_data.Seek(section_hdrs[hdr.e_shstrndx].sh_offset + section_hdrs[i].sh_name_off, SeekOrigin.Begin); section_hdrs[i].sh_name = ReadString(elf_data); Console.WriteLine(section_hdrs[i].sh_name + " - Offset: 0x" + section_hdrs[i].sh_offset.ToString("x")); if (section_hdrs[i].sh_name == ".debug") { debug_offset = section_hdrs[i].sh_offset; debug_size = section_hdrs[i].sh_size; } } elf_data.Close(); }