"""Find the functions that build the address of a string. usage: python3 -I string_refs.py ... For each string containing a substring, scan .text for lui rt,hi followed within eight instructions by addiu/ori rX,rt,lo that rebuild its address, and report the generated function (sub_XXXXXXXX file) that contains the reference. """ import bisect import os import re import struct import sys def sections(elf): shoff, = struct.unpack_from("> 26 != 0x0F: continue rt, hi = (word >> 16) & 31, word & 0xFFFF for follow in words[index + 1:index + 9]: op, rs, imm = follow >> 26, (follow >> 21) & 31, follow & 0xFFFF if rs != rt or op not in (0x09, 0x0D): continue value = ((hi << 16) + (imm - 0x10000 if op == 0x09 and imm & 0x8000 else imm)) & 0xFFFFFFFF if value in targets: pc = text_addr + index * 4 owner = starts[bisect.bisect_right(starts, pc) - 1] if starts and pc >= starts[0] else 0 print("0x%08X in sub_%08X %r" % (pc, owner, targets[value][:70])) break main()