Skip to content

Instantly share code, notes, and snippets.

@ocean1
Last active September 13, 2018 17:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ocean1/1dc9fb4303cc28f72147961a2971f40d to your computer and use it in GitHub Desktop.
Save ocean1/1dc9fb4303cc28f72147961a2971f40d to your computer and use it in GitHub Desktop.
rename symbols in IDA using nm
# rename functions loading addresses using nm
import idaapi
import idc
from subprocess import Popen, PIPE
def make_func(addr):
idc.MakeCode(addr)
idc.MakeFunction(addr)
if __name__ == "__main__":
executable = idaapi.get_input_file_path()
proc = Popen(
"nm {}".format(executable),
shell=True,
stdout=PIPE,
stderr=PIPE)
out, err = proc.communicate()
errcode = proc.returncode
if errcode != 0:
raise Exception("cannot get symbols!")
proc = Popen(
"nm -C {}".format(executable),
shell=True,
stdout=PIPE,
stderr=PIPE)
out_demangled, err = proc.communicate()
errcode = proc.returncode
if errcode != 0:
raise Exception("cannot get demangled symbols!")
symbols = {}
for nlist, dlist in zip(out.splitlines(), out_demangled.splitlines()):
a, t, name = nlist.split(" ")
ad, td, named = dlist.split(" ", 2)
if a != ad:
raise Exception("error processing %s/%s, %s != %s".format(
name, named, a, ad))
addr = int(a, 16)
if t in ["t", "T"]:
make_func(addr)
if name.lstrip("_") != named.lstrip("_"):
idc.SetFunctionCmt(addr, named, 0)
idc.MakeNameEx(addr, name, idc.SN_NOWARN)
if name.lstrip("_") != named.lstrip("_"):
idc.MakeComm(addr, named)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment