Skip to content

Commit

Permalink
added a class decorator to register architectures
Browse files Browse the repository at this point in the history
  • Loading branch information
hugsy committed Jan 8, 2022
1 parent bca69da commit 32941ad
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions gef.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def update_gef(argv):
gef = None
__registered_commands__ = []
__registered_functions__ = []
__registered_architectures__ = {}


def reset_all_caches():
Expand Down Expand Up @@ -2049,6 +2050,13 @@ def get_zone_base_address(name):
#
# Architecture classes
#
def register_architecture(cls):
"""Class decorator for declaring an architecture to GEF."""
global __registered_architectures__
for key in cls.aliases:
__registered_architectures__[key] = cls
return cls


class Architecture(metaclass=abc.ABCMeta):
"""Generic metaclass for the architecture supported by GEF."""
Expand Down Expand Up @@ -2080,6 +2088,7 @@ def is_branch_taken(self, insn): pass
@abc.abstractmethod
def get_ra(self, insn, frame): pass

aliases = []
special_registers = []

def __get_register(self, regname):
Expand Down Expand Up @@ -2176,9 +2185,11 @@ def is_branch_taken(self, insn): return False
def get_ra(self, insn, frame): return 0


@register_architecture
class RISCV(Architecture):
arch = "RISCV"
mode = "RISCV"
aliases = ("RISCV")

all_registers = ["$zero", "$ra", "$sp", "$gp", "$tp", "$t0", "$t1",
"$t2", "$fp", "$s1", "$a0", "$a1", "$a2", "$a3",
Expand Down Expand Up @@ -2282,9 +2293,10 @@ def get_ra(self, insn, frame):
return ra


@register_architecture
class ARM(Architecture):
aliases = ("ARM", Elf.ARM)
arch = "ARM"

all_registers = ["$r0", "$r1", "$r2", "$r3", "$r4", "$r5", "$r6",
"$r7", "$r8", "$r9", "$r10", "$r11", "$r12", "$sp",
"$lr", "$pc", "$cpsr",]
Expand Down Expand Up @@ -2427,7 +2439,9 @@ def mprotect_asm(cls, addr, size, perm):
return "; ".join(insns)


@register_architecture
class AARCH64(ARM):
aliases = ("ARM64", "AARCH64", Elf.AARCH64)
arch = "ARM64"
mode = ""

Expand Down Expand Up @@ -2526,7 +2540,9 @@ def is_branch_taken(self, insn):
return taken, reason


@register_architecture
class X86(Architecture):
aliases = ("X86", Elf.X86_32)
arch = "X86"
mode = "32"

Expand Down Expand Up @@ -2660,7 +2676,9 @@ def get_ith_parameter(self, i, in_func=True):
return key, val


@register_architecture
class X86_64(X86):
aliases = ("X86_64", Elf.X86_64, "i386:x86-64")
arch = "X86"
mode = "64"

Expand Down Expand Up @@ -2701,7 +2719,9 @@ def mprotect_asm(cls, addr, size, perm):
return "; ".join(insns)


@register_architecture
class PowerPC(Architecture):
aliases = ("PowerPC", Elf.POWERPC, "PPC")
arch = "PPC"
mode = "PPC32"

Expand Down Expand Up @@ -2795,15 +2815,19 @@ def mprotect_asm(cls, addr, size, perm):
return ";".join(insns)


@register_architecture
class PowerPC64(PowerPC):
aliases = ("PowerPC64", Elf.POWERPC64, "PPC64")
arch = "PPC"
mode = "PPC64"


@register_architecture
class SPARC(Architecture):
""" Refs:
- http://www.cse.scu.edu/~atkinson/teaching/sp05/259/sparc.pdf
"""
aliases = ("SPARC", Elf.SPARC)
arch = "SPARC"
mode = ""

Expand Down Expand Up @@ -2903,12 +2927,13 @@ def mprotect_asm(cls, addr, size, perm):
return "; ".join(insns)


@register_architecture
class SPARC64(SPARC):
"""Refs:
- http://math-atlas.sourceforge.net/devel/assembly/abi_sysV_sparc.pdf
- https://cr.yp.to/2005-590/sparcv9.pdf
"""

aliases = ("SPARC64", Elf.SPARC64)
arch = "SPARC"
mode = "V9"

Expand Down Expand Up @@ -2949,7 +2974,9 @@ def mprotect_asm(cls, addr, size, perm):
return "; ".join(insns)


@register_architecture
class MIPS(Architecture):
aliases = ("MIPS", Elf.MIPS)
arch = "MIPS"
mode = "MIPS32"

Expand Down Expand Up @@ -3031,7 +3058,9 @@ def mprotect_asm(cls, addr, size, perm):
return "; ".join(insns)


@register_architecture
class MIPS64(MIPS):
aliases = ("MIPS64")
arch = "MIPS"
mode = "MIPS64"
ptrsize = 8
Expand Down Expand Up @@ -3625,19 +3654,7 @@ def set_arch(arch=None, default=None):
Return the selected arch, or raise an OSError.
"""
global gef
arches = {
"ARM": ARM, Elf.ARM: ARM,
"AARCH64": AARCH64, "ARM64": AARCH64, Elf.AARCH64: AARCH64,
"X86": X86, Elf.X86_32: X86,
"X86_64": X86_64, Elf.X86_64: X86_64, "i386:x86-64": X86_64,
"PowerPC": PowerPC, "PPC": PowerPC, Elf.POWERPC: PowerPC,
"PowerPC64": PowerPC64, "PPC64": PowerPC64, Elf.POWERPC64: PowerPC64,
"RISCV": RISCV, Elf.RISCV: RISCV,
"SPARC": SPARC, Elf.SPARC: SPARC,
"SPARC64": SPARC64, Elf.SPARC64: SPARC64,
"MIPS": MIPS, Elf.MIPS: MIPS,
"MIPS64": MIPS64,
}
arches = __registered_architectures__

if arch:
try:
Expand Down

0 comments on commit 32941ad

Please sign in to comment.