Skip to content

Commit

Permalink
Refactor reset_architecture: check for param forced, binary running, …
Browse files Browse the repository at this point in the history
…elf header

Signed-off-by: José Luis Di Biase <[email protected]>
  • Loading branch information
josx committed Sep 12, 2023
1 parent 5927df4 commit 53d0aa5
Showing 1 changed file with 16 additions and 39 deletions.
55 changes: 16 additions & 39 deletions gef.py
Original file line number Diff line number Diff line change
Expand Up @@ -2150,34 +2150,6 @@ def checksec(filename: str) -> Dict[str, bool]:
return Elf(filename).checksec


@lru_cache()
def get_arch() -> str:
"""Return the binary's architecture."""
if is_alive():
arch = gdb.selected_frame().architecture()
return arch.name()

arch_str = gdb.execute("show architecture", to_string=True).strip()
pat = "The target architecture is set automatically (currently "
if arch_str.startswith(pat):
arch_str = arch_str[len(pat):].rstrip(")")
return arch_str

pat = "The target architecture is assumed to be "
if arch_str.startswith(pat):
return arch_str[len(pat):]

pat = "The target architecture is set to "
if arch_str.startswith(pat):
# GDB version >= 10.1
if '"auto"' in arch_str:
return re.findall(r"currently \"(.+)\"", arch_str)[0]
return re.findall(r"\"(.+)\"", arch_str)[0]

# Unknown, we throw an exception to be safe
raise RuntimeError(f"Unknown architecture: {arch_str}")


@deprecated("Use `gef.binary.entry_point` instead")
def get_entry_point() -> Optional[int]:
"""Return the binary entry point."""
Expand Down Expand Up @@ -3679,20 +3651,25 @@ def reset_architecture(arch: Optional[str] = None) -> None:
raise OSError(f"Specified arch {arch.upper()} is not supported")
return

gdb_arch = get_arch()
# check for bin running
if is_alive():
try:
arch = gdb.selected_frame().architecture()
gef.arch = arches[arch.name()]()
except KeyError:
raise OSError(f"Running Binary arch {arch.name().upper()} is not supported")
return

preciser_arch = next((a for a in arches.values() if a.supports_gdb_arch(gdb_arch)), None)
if preciser_arch:
gef.arch = preciser_arch()
# check for elf header architecture
if gef.binary.e_machine:
try:
gef.arch = arches[gef.binary.e_machine]()
except KeyError:
raise OSError(f"CPU type is currently not supported: {gef.binary.e_machine}")
return

# last resort, use the info from elf header to find it from the known architectures
try:
arch_name = gef.binary.e_machine if gef.binary else gdb_arch
gef.arch = arches[arch_name]()
except KeyError:
raise OSError(f"CPU type is currently not supported: {get_arch()}")
return
# Unknown, we throw an exception to be safe
raise RuntimeError(f"Unknown architecture")


@lru_cache()
Expand Down

0 comments on commit 53d0aa5

Please sign in to comment.