#!/usr/bin/env python3 import os.path, struct, sys import argparse, glob, shutil, subprocess import hackyelf, linkmap from smolshared import * from smolparse import * def readbyte(blob, off): return struct.unpack('= x.vaddr and addr < x.vaddr + x.memsz: aoff = addr - x.vaddr assert aoff < x.filesz, ".bss address!" return aoff + x.off assert False, "Address %08x not in the static address range!" % addr def get_needed_libs(elf, blob): assert elf.dyn is not None, "No DYNAMIC table present in the ELF file!" strtabs = [x.val for x in elf.dyn if x.tag == hackyelf.DT_STRTAB] assert len(strtabs) == 1, "Only one DT_STRTAB may be present in an ELF file." strtab = strtabs[0] return [readstr(blob,addr2off(elf, strtab+x.val))[0] for x in elf.dyn if x.tag == hackyelf.DT_NEEDED] def get_hashtbl(elf, blob, args): htaddr = None if args.map is not None: lmap = linkmap.parse(args.map.read()) tabs = [x for x in lmap.mmap if x.sym == '_symbols'] assert len(tabs) == 1, "One '_symbols' symbol must be present." htaddr = tabs[0].org elif elf.is32bit: txtoff = addr2off(elf, elf.entry) # scan for 'push IMM32' while blob[txtoff] != 0x68: txtoff = txtoff + 1 assert txtoff < len(blob), "wtf??? (can't find a push IMM32 instruction which pushes the hashtable address)" txtoff = txtoff + 1 htaddr = struct.unpack(' %s" % (x, v[x])) if len(noresolves) > 0: print("UNRESOLVED:") for x in noresolves: print("\t%08x" % x) return 0 if __name__ == '__main__': rv = main() if rv is None: pass else: try: sys.exit(int(rv)) except: sys.exit(1)