From d0a46c9aa30c13f2fffe3a35ce807894337ff009 Mon Sep 17 00:00:00 2001 From: Cyrille Bagard Date: Sat, 7 Jul 2018 12:52:48 +0200 Subject: Played with the Itanium demangling Python API. --- misc/hole.cpp | 41 ++++++++++++++++++++ python/c++filt.py | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 misc/hole.cpp create mode 100644 python/c++filt.py diff --git a/misc/hole.cpp b/misc/hole.cpp new file mode 100644 index 0000000..1f38d83 --- /dev/null +++ b/misc/hole.cpp @@ -0,0 +1,41 @@ + +#include +#include + +namespace How { + + namespace Deep { + + namespace The { + + template + class Rabbit { + + public: + + template + static t1 HoleIs(std::vector a) + { + return 0; + } + + }; + + } + + } + +} + +int main() +{ + How::Deep::The::Rabbit r; + + typedef int (* func) (double); + + std::vector array(10); + + std::cout << "Hello" << std::endl; + std::cout << r.HoleIs(array) << std::endl; + +} diff --git a/python/c++filt.py b/python/c++filt.py new file mode 100644 index 0000000..db6b47e --- /dev/null +++ b/python/c++filt.py @@ -0,0 +1,114 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- + +import argparse +import sys +import pychrysalide +from pychrysalide.analysis.types import TemplateType +from pychrysalide.mangling import ItaniumDemangler + + + +def display_template_params(root): + """Look for template parameters in a type and its namespace.""" + + if root: + + got = display_template_params(root.namespace) + + if isinstance(root, TemplateType): + + for p in root.params: + print(' -', p, '(', type(p), ')') + + got = True + + else: + + got = False + + return got + + +if __name__ == '__main__': + """Entry point.""" + + parser = argparse.ArgumentParser(description='c++filt - Demangle C++ Itanium symbols.', add_help=False) + + parser.add_argument('-h', '--help', action='store_true', help='Display the command line options.') + + parser.add_argument('symbol', type=str, help='Symbol to be demangled') + + args = parser.parse_args() + + if args.help: + parser.print_help() + sys.exit(1) + + demangler = ItaniumDemangler() + + demangled = demangler.decode_routine(args.symbol) + + if demangled is None: + print('Error') + + else: + + print() + + print(demangled) + print() + + print('Namespace') + print('---------') + + print(' -', demangled.namespace, '(', type(demangled.namespace), ')') + print() + + print('Template parameters') + print('-------------------') + + got = display_template_params(demangled.namespace) + + if demangled.typed_name: + got |= display_template_params(demangled.typed_name) + + if not(got): + print(' - (none)') + + print() + + print('Name') + print('----') + + if demangled.typed_name: + print(' -', demangled.typed_name, '(', type(demangled.typed_name), ')') + + else: + print(' -', demangled.name, '(', type(demangled.name), ')') + + print() + + print('Return') + print('------') + + print(' -', demangled.ret, '(', type(demangled.ret), ')') + print() + + print('Arguments') + print('------') + + args = demangled.args + + if len(args) == 0: + print(' - (none)') + + else: + + count = 0 + + for a in args: + print(' - [%d] -' % count, a.type, '(', type(a.type), ')') + count += 1 + + print() -- cgit v0.11.2-87-g4458