From 8300860bdc3af55dcc6dee56c4963bb5e8f274ec Mon Sep 17 00:00:00 2001 From: Andrew Guschin Date: Mon, 19 Feb 2024 20:44:55 +0400 Subject: initial commit --- elf-layout.py | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 elf-layout.py (limited to 'elf-layout.py') diff --git a/elf-layout.py b/elf-layout.py new file mode 100644 index 0000000..ee8530b --- /dev/null +++ b/elf-layout.py @@ -0,0 +1,96 @@ +import sys + +if len(sys.argv) < 2: + print("First arg should be name of the binary") + exit() + +with open(sys.argv[1], "rb") as f: + data = f.read() + +def toi(val): + res = 0 + shift = 0 + for i in val: + res += i << shift + shift += 8 + return res + + +def field(offset, size): + return data[offset:offset + size] + +magic = field(0x00, 4) +plat_bit = field(0x04, 1) +endian = field(0x05, 1) +ver = field(0x06, 1) +abi = field(0x07, 1) +abi_ver = field(0x08, 1) +padding = field(0x09, 7) +obj_t = field(0x10, 2) +isa = field(0x12, 2) +orig_ver = field(0x14, 4) + +bit32 = toi(plat_bit) == 1 +bit64 = toi(plat_bit) == 2 + +# expect 64-bit +entry = field(0x18, 8) +phoff = field(0x20, 8) +shoff = field(0x28, 8) +flags = field(0x30, 4) +ehsize = field(0x34, 2) + +phentsize = field(0x36, 2) +phnum = field(0x38, 2) + +shentsize = field(0x3a, 2) +shnum = field(0x3c, 2) +shstrndx = field(0x3e, 2) + +print(data[0x3e]) +print(list(shstrndx)) +shstrndx = toi(shstrndx) +shnum = toi(shnum) +shoff = toi(shoff) + +strshtab_off = 0 + +for i in range(shnum): + sh_name = toi(field(shoff + 0x40 * i + 0x00, 4)) + sh_type = toi(field(shoff + 0x40 * i + 0x04, 4)) + sh_flags = toi(field(shoff + 0x40 * i + 0x08, 8)) + sh_addr = toi(field(shoff + 0x40 * i + 0x10, 8)) + sh_offset = toi(field(shoff + 0x40 * i + 0x18, 8)) + sh_size = toi(field(shoff + 0x40 * i + 0x20, 8)) + sh_link = toi(field(shoff + 0x40 * i + 0x28, 4)) + sh_info = toi(field(shoff + 0x40 * i + 0x2c, 4)) + sh_addralign = toi(field(shoff + 0x40 * i + 0x30, 8)) + sh_entsize = toi(field(shoff + 0x40 * i + 0x38, 8)) + + if i == shstrndx: + strshtab_off = sh_offset + break + +for i in range(shnum): + sh_name = toi(field(shoff + 0x40 * i + 0x00, 4)) + sh_type = toi(field(shoff + 0x40 * i + 0x04, 4)) + sh_flags = toi(field(shoff + 0x40 * i + 0x08, 8)) + sh_addr = toi(field(shoff + 0x40 * i + 0x10, 8)) + sh_offset = toi(field(shoff + 0x40 * i + 0x18, 8)) + sh_size = toi(field(shoff + 0x40 * i + 0x20, 8)) + sh_link = toi(field(shoff + 0x40 * i + 0x28, 4)) + sh_info = toi(field(shoff + 0x40 * i + 0x2c, 4)) + sh_addralign = toi(field(shoff + 0x40 * i + 0x30, 8)) + sh_entsize = toi(field(shoff + 0x40 * i + 0x38, 8)) + + if sh_type == 0: + continue + # print(sh_name) + name = [] + name_idx = strshtab_off + sh_name + while data[name_idx] != 0: + name.append(data[name_idx]) + name_idx += 1 + name = bytes(name).decode() + # print(name) + -- cgit v1.2.3