Last active
November 17, 2020 05:41
-
-
Save 8051Enthusiast/a7457c53ee1005fc41c2c5ba413d0b2d to your computer and use it in GitHub Desktop.
finds general pointer references in ghidra 8051 code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
state = getState() | |
currentProgram = state.getCurrentProgram() | |
def to_positive(b): | |
if b >= 0: | |
return b | |
return b + 256 | |
def maybe_get_gptr_ref(b): | |
if len(b) < 6: | |
return None | |
if not b[0] == 0x7b or not b[2] == 0x7a or not b[4] == 0x79: | |
return None | |
if b[1] not in [0, 1, 0xff]: | |
return None | |
return (b[1], b[3] << 8 | b[5]) | |
def addref(from_addr, space, address): | |
addr_space = None | |
if space == 0: | |
addr_space = currentProgram.getAddressFactory().getAddressSpace("INTMEM") | |
elif space == 0xff: | |
addr_space = from_addr.getAddressSpace() | |
elif space == 0x01: | |
addr_space = currentProgram.getAddressFactory().getAddressSpace("EXTMEM") | |
else: | |
raise NotImplementedError | |
target_addr = addr_space.getAddress(address) | |
disassemble(from_addr) | |
instr = getInstructionAt(from_addr) | |
createMemoryReference(instr, 1, target_addr, ghidra.program.model.symbol.RefType.DATA) | |
def find_everything(from_space): | |
everything = None | |
low_addr = from_space.getMinAddress().getOffset() | |
max_addr = from_space.getMaxAddress().getOffset() | |
length = max_addr - low_addr + 1 | |
everything = list(map(to_positive, getBytes(from_space.getAddress(low_addr), length))) | |
for i in range(length): | |
maybe_ref = maybe_get_gptr_ref(everything[i:]) | |
from_addr = from_space.getAddress(low_addr + i) | |
if maybe_ref != None: | |
(space, addr) = maybe_ref | |
addref(from_addr, space, addr) | |
pages = currentProgram.getAddressFactory().getAddressSpaces() | |
for page in pages: | |
try: | |
find_everything(page) | |
except ghidra.program.model.mem.MemoryAccessException: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment