diff options
author | Malfurious <m@lfurio.us> | 2023-02-22 15:01:22 -0500 |
---|---|---|
committer | dusoleil <howcansocksbereal@gmail.com> | 2023-02-24 03:39:04 -0500 |
commit | a4543a9ffcc52f205a8c2aaa56909acda4e9d0b1 (patch) | |
tree | edf8490858215dc6ef9d0375ac8b4ab783924ec5 /sploit/symtbl.py | |
parent | f8cabdb49cbbc993790efd0d9844cde3d4617347 (diff) | |
download | nsploit-a4543a9ffcc52f205a8c2aaa56909acda4e9d0b1.tar.gz nsploit-a4543a9ffcc52f205a8c2aaa56909acda4e9d0b1.zip |
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 <m@lfurio.us>
Signed-off-by: dusoleil <howcansocksbereal@gmail.com>
Diffstat (limited to 'sploit/symtbl.py')
-rw-r--r-- | sploit/symtbl.py | 53 |
1 files changed, 53 insertions, 0 deletions
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 |