Skip to content

Instantly share code, notes, and snippets.

@fasterthanlime
Created February 26, 2020 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fasterthanlime/394722850ebb73683760f5a718c4af8d to your computer and use it in GitHub Desktop.
Save fasterthanlime/394722850ebb73683760f5a718c4af8d to your computer and use it in GitHub Desktop.
GDB script: load symbols from all executable objects mapped in memory, automatically.
import re
import subprocess
pid = gdb.selected_inferior().pid
maps_path = "/proc/%d/maps" % pid
maps_lines = open(maps_path).read().split("\n")
for line in maps_lines:
memrange, perms, offset, devnum, size, path = (None, None, None, None, None, None)
try:
memrange, perms, offset, devnum, size, path = re.split("\s+", line)
except:
continue
# exclude [vdso], [vsyscall], etc.
if re.match("^\[.*\]$", path):
continue
# exclude non-executable segments
if perms[2] != "x":
continue
if path == "":
continue
lines = subprocess.check_output(["readelf", "--sections", path]).decode("utf-8").split("\n")
addr = None
for line in lines:
if re.match(".*\[[0-9 ]+\].*\.text", line):
tokens = re.split("\s+", line.strip())
addr = int(tokens[len(tokens)-2], 16)
break
if addr != None:
memstart = int(memrange.split("-")[0], 16)
offset = int(offset, 16)
textaddress = memstart - offset + addr
cmd = "add-symbol-file %s 0x%x" % (path, textaddress)
gdb.execute(cmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment