# stolen from the contrib folder in https://github.com/blackle/LZMA-Vizualizer # (i.e. I'm stealing it from myself) # custom elf parser because a standard one wouldn't be trustable because the # ELFs we're parsing will be a bit wonky anyway from struct import unpack from typing import * ELFCLASS32 = 1 ELFCLASS64 = 2 EM_386 = 3 EM_X86_64 = 62 PT_NULL = 0 PT_LOAD = 1 PT_DYNAMIC = 2 PT_INTERP = 3 DT_NULL = 0 DT_NEEDED = 1 DT_STRTAB = 5 DT_SYMTAB = 6 class Phdr(NamedTuple): ptype: int off : int vaddr: int paddr: int filesz: int memsz: int flags: int align: int class Dyn(NamedTuple): tag: int val: int class ELF(NamedTuple): data : bytes ident : bytes eclass: int mach : int entry : int phdrs : Sequence[Phdr] dyn : Sequence[Dyn] is32bit: bool # yeah, there's some code duplication here # idgaf def parse_phdr32(data: bytes, phoff:int, phentsz:int, phnum:int) -> Sequence[Phdr]: ps = [] for off in range(phoff, phoff+phentsz*phnum, phentsz): ptype, off, vaddr, paddr, filesz, memsz, flags, align = \ unpack(' Dyn: ds = [] off = dynp.off while True: tag, val = unpack(' ELF: ident = data[:16] eclass = data[4] mach = unpack(' Sequence[Phdr]: ps = [] for off in range(phoff, phoff+phentsz*phnum, phentsz): # TODO ptype, flags, off, vaddr, paddr, filesz, memsz, align = \ unpack(' Dyn: ds = [] off = dynp.off while True: tag, val = unpack(' ELF: ident = data[:16] eclass = data[4] mach = unpack(' ELF: assert data[:4] == b'\x7FELF', "Not a valid ELF file" # good enough ecls = data[4] if ecls == ELFCLASS32: return parse_32(data) elif ecls == ELFCLASS64: return parse_64(data) else: emch = unpack('