From a4543a9ffcc52f205a8c2aaa56909acda4e9d0b1 Mon Sep 17 00:00:00 2001 From: Malfurious Date: Wed, 22 Feb 2023 15:01:22 -0500 Subject: symtbl: Rename file to match class name I assume that the preferred style is to leave one major class each to a file. In this case, synchronize the names of the Symtbl class and its containing module. Per PEP8, the module is lowercase, and the class remains Pascal case. If other memory-oriented utilities are introduced in the future, we may wish to move them, as well as Symtbl, back into a subpackage named 'mem'. Signed-off-by: Malfurious Signed-off-by: dusoleil --- sploit/symtbl.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 sploit/symtbl.py (limited to 'sploit/symtbl.py') diff --git a/sploit/symtbl.py b/sploit/symtbl.py new file mode 100644 index 0000000..3a3e697 --- /dev/null +++ b/sploit/symtbl.py @@ -0,0 +1,53 @@ +import types + +class Symtbl: + def __init__(self, *, base=0, **kwargs): + object.__setattr__(self, '_namesp', types.SimpleNamespace(base=base,sym={},sub={})) + for k, v in {**kwargs}.items(): + setattr(self, k, v) + + def __getattr__(self, ident): + self = self._namesp + if ident == 'base': return self.base + off = self.base + self.sym[ident] + if ident in self.sub: return self.sub[ident].map(off) + return off + + def __setattr__(self, ident, value): + if ident in dir(self): raise Exception(f'Symtbl: assignment would shadow non-symbol "{ident}"') + self = self._namesp + if ident == 'base': + self.base = value + else: + if type(value) is tuple: self.sub[ident], off = value + else: off = value + self.sym[ident] = off - self.base + + def map(self, addr, off=0): + self = self._namesp + mm = Symtbl() + mm._namesp.sym, mm._namesp.sub = self.sym, self.sub + mm._namesp.base = addr - off + return mm + + def adjust(self, off): + self = self._namesp + for k, v in self.sym.items(): + self.sym[k] = v + off + + def rebase(self, off): + self.adjust(self.base - off) + + def __str__(_self): + FMT = '\n{:<20} {:<20}' + self = _self._namesp + + s = f'{len(self.sym)} symbols @ {hex(_self.base)}' + s += FMT.format('ADDRESS', 'SYMBOL') + for sym, _ in sorted(self.sym.items(), key=lambda x:x[1]): + addr = getattr(_self, sym) + if type(addr) is Symtbl: + s += FMT.format(hex(addr.base), f'[{sym}]') + else: + s += FMT.format(hex(addr), sym) + return s -- cgit v1.2.3