From 32f8851e3bad5bfc08a3f0aac5cc28bdb3ca6611 Mon Sep 17 00:00:00 2001 From: r12f Date: Wed, 20 Dec 2023 17:59:10 +0000 Subject: [PATCH 1/4] add c++ symbol support for xinfo. --- gef.py | 6 +++--- tests/binaries/Makefile | 6 ++++++ tests/binaries/class.cpp | 29 +++++++++++++++++++++++++++++ tests/commands/xinfo.py | 9 ++++++++- 4 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 tests/binaries/class.cpp diff --git a/gef.py b/gef.py index 8e9e4d2f3..f4f140cec 100644 --- a/gef.py +++ b/gef.py @@ -2011,10 +2011,10 @@ def gdb_get_location_from_symbol(address: int) -> Optional[Tuple[str, int]]: return None i = sym.find(" in section ") - sym = sym[:i].split() + sym = sym[:i].split('+') name, offset = sym[0], 0 - if len(sym) == 3 and sym[2].isdigit(): - offset = int(sym[2]) + if len(sym) == 2 and sym[1].isdigit(): + offset = int(sym[1]) return name, offset diff --git a/tests/binaries/Makefile b/tests/binaries/Makefile index d0fd53ac2..0a8acc58f 100644 --- a/tests/binaries/Makefile +++ b/tests/binaries/Makefile @@ -2,7 +2,9 @@ CC = gcc DEBUG = 1 CFLAGS += -Wall SOURCES = $(wildcard *.c) +SOURCES += $(wildcard *.cpp) LINKED = $(SOURCES:.c=.out) +LINKED := $(LINKED:.cpp=.out) LDFLAGS = EXTRA_FLAGS = TMPDIR ?= /tmp @@ -27,6 +29,10 @@ all: $(LINKED) @echo "[+] Building '$(TMPDIR)/$@'" @$(CC) $(CFLAGS) $(EXTRA_FLAGS) -o $(TMPDIR)/$@ $? $(LDFLAGS) +%.out : %.cpp + @echo "[+] Building '$(TMPDIR)/$@'" + @$(CC) $(CFLAGS) $(EXTRA_FLAGS) -o $(TMPDIR)/$@ $? $(LDFLAGS) -lstdc++ + clean : @echo "[+] Cleaning stuff" @cd $(TMPDIR) && rm -f $(LINKED) diff --git a/tests/binaries/class.cpp b/tests/binaries/class.cpp new file mode 100644 index 000000000..acc427c48 --- /dev/null +++ b/tests/binaries/class.cpp @@ -0,0 +1,29 @@ +#include + +class TraitA {}; +class TraitB {}; + +class A { +private: + int _a; + +public: + virtual ~A() {} + virtual void Run() { printf("I am A\n"); } +}; + +template +class B : public A { +private: + int _b; + +public: + virtual void Run() { printf("I am B\n"); } +}; + +int main() { + A* a = new B(); + a->Run(); + delete a; + return 0; +} diff --git a/tests/commands/xinfo.py b/tests/commands/xinfo.py index 662e2c38f..22cd52a35 100644 --- a/tests/commands/xinfo.py +++ b/tests/commands/xinfo.py @@ -3,7 +3,7 @@ """ -from tests.utils import GefUnitTestGeneric, gdb_run_cmd, gdb_start_silent_cmd +from tests.utils import GefUnitTestGeneric, gdb_run_cmd, gdb_start_silent_cmd, gdb_run_silent_cmd, _target class XinfoCommand(GefUnitTestGeneric): @@ -18,3 +18,10 @@ def test_cmd_xinfo(self): res = gdb_start_silent_cmd("xinfo $sp") self.assertNoException(res) self.assertTrue(len(res.splitlines()) >= 7) + + def test_cmd_xinfo_on_class(self): + cmd = "xinfo $pc" + target = _target("class") + res = gdb_run_silent_cmd(cmd, target=target, before=["b B::Run()"]) + self.assertNoException(res) + self.assertIn("Symbol: B::Run", res) \ No newline at end of file From 97940e754dbbc8bdb808c17346c16ca7db673fd4 Mon Sep 17 00:00:00 2001 From: r12f Date: Wed, 20 Dec 2023 18:08:18 +0000 Subject: [PATCH 2/4] pre-commit fix. --- tests/commands/xinfo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/commands/xinfo.py b/tests/commands/xinfo.py index 22cd52a35..7c5b299b6 100644 --- a/tests/commands/xinfo.py +++ b/tests/commands/xinfo.py @@ -24,4 +24,4 @@ def test_cmd_xinfo_on_class(self): target = _target("class") res = gdb_run_silent_cmd(cmd, target=target, before=["b B::Run()"]) self.assertNoException(res) - self.assertIn("Symbol: B::Run", res) \ No newline at end of file + self.assertIn("Symbol: B::Run", res) From 94fd47dd7ef3fafe94c765f0def1669f4be9d4e6 Mon Sep 17 00:00:00 2001 From: r12f Date: Thu, 21 Dec 2023 02:14:49 +0000 Subject: [PATCH 3/4] address comments. --- gef.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/gef.py b/gef.py index f4f140cec..039d51023 100644 --- a/gef.py +++ b/gef.py @@ -2010,9 +2010,11 @@ def gdb_get_location_from_symbol(address: int) -> Optional[Tuple[str, int]]: if sym.startswith("No symbol matches"): return None + # gdb outputs symbols with format: " + in section of ", + # here, we are only interested in symbol name and offset. i = sym.find(" in section ") - sym = sym[:i].split('+') - name, offset = sym[0], 0 + sym = sym[:i].split("+") + name, offset = sym[0].strip(), 0 if len(sym) == 2 and sym[1].isdigit(): offset = int(sym[1]) return name, offset From 42d5c91580adb324dbf8ab7a61aeb0a428280d9a Mon Sep 17 00:00:00 2001 From: r12f Date: Thu, 21 Dec 2023 02:17:19 +0000 Subject: [PATCH 4/4] precommit fix. --- gef.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gef.py b/gef.py index 039d51023..e8fef99b0 100644 --- a/gef.py +++ b/gef.py @@ -2011,7 +2011,7 @@ def gdb_get_location_from_symbol(address: int) -> Optional[Tuple[str, int]]: return None # gdb outputs symbols with format: " + in section of ", - # here, we are only interested in symbol name and offset. + # here, we are only interested in symbol name and offset. i = sym.find(" in section ") sym = sym[:i].split("+") name, offset = sym[0].strip(), 0