demangle module

binaryninja.demangle.CoreDemangler([handle])

binaryninja.demangle.Demangler([handle])

Pluggable name demangling interface.

class CoreDemangler(handle=None)[source]

Bases: Demangler

demangle(arch: Architecture, name: str, view: BinaryView | None = None) Tuple[Type | None, QualifiedName] | None[source]

Demangle a raw name into a Type and QualifiedName.

The result of this function is a (Type, QualifiedName) tuple for the demangled name’s details.

Any unresolved named types referenced by the resulting Type will be created as empty structures or void typedefs in the view, if the result is used on a data structure in the view. Given this, the call to demangle should NOT cause any side-effects creating types in the view trying to resolve this and instead just return a type with unresolved named type references.

The most recently registered demangler that claims a name is a mangled string (returns true from is_mangled_string), and then returns a value from this function will determine the result of a call to demangle_generic. If this call returns None, the next most recently used demangler(s) will be tried instead.

If the mangled name has no type information, but a name is still possible to extract, this function may return a successful (None, <name>) result, which will be accepted.

Parameters:
  • arch (Architecture) – Architecture for context in which the name exists, eg for pointer sizes

  • name (str) – Raw mangled name

  • view (BinaryView | None) – (Optional) BinaryView context in which the name exists, eg for type lookup

Returns:

Tuple of (Type, Name) if successful, None if not. Type may be None if only a demangled name can be recovered from the raw name.

Return type:

Tuple[Type | None, QualifiedName] | None

is_mangled_string(name: str) bool[source]

Determine if a given name is mangled and this demangler can process it

The most recently registered demangler that claims a name is a mangled string (returns true from this function), and then returns a value from demangle will determine the result of a call to demangle_generic. Returning True from this does not require the demangler to succeed the call to demangle, but simply implies that it may succeed.

Parameters:

name (str) – Raw mangled name string

Returns:

True if the demangler thinks it can handle the name

Return type:

bool

class Demangler(handle=None)[source]

Bases: object

Pluggable name demangling interface. See register and demangle for details on the process of this interface.

The list of Demanglers can be queried:

>>> list(Demangler)
[<Demangler: MS>, <Demangler: GNU3>]
demangle(arch: Architecture, name: str, view: BinaryView | None = None) Tuple[Type, QualifiedName] | None[source]

Demangle a raw name into a Type and QualifiedName.

The result of this function is a (Type, QualifiedName) tuple for the demangled name’s details.

Any unresolved named types referenced by the resulting Type will be created as empty structures or void typedefs in the view, if the result is used on a data structure in the view. Given this, the call to demangle should NOT cause any side-effects creating types in the view trying to resolve this and instead just return a type with unresolved named type references.

The most recently registered demangler that claims a name is a mangled string (returns true from is_mangled_string), and then returns a value from this function will determine the result of a call to demangle_generic. If this call returns None, the next most recently used demangler(s) will be tried instead.

If the mangled name has no type information, but a name is still possible to extract, this function may return a successful (None, <name>) result, which will be accepted.

Parameters:
  • arch (Architecture) – Architecture for context in which the name exists, eg for pointer sizes

  • name (str) – Raw mangled name

  • view (BinaryView | None) – (Optional) BinaryView context in which the name exists, eg for type lookup

Returns:

Tuple of (Type, Name) if successful, None if not. Type may be None if only a demangled name can be recovered from the raw name.

Return type:

Tuple[Type, QualifiedName] | None

is_mangled_string(name: str) bool[source]

Determine if a given name is mangled and this demangler can process it

The most recently registered demangler that claims a name is a mangled string (returns true from this function), and then returns a value from demangle will determine the result of a call to demangle_generic. Returning True from this does not require the demangler to succeed the call to demangle, but simply implies that it may succeed.

Parameters:

name (str) – Raw mangled name string

Returns:

True if the demangler thinks it can handle the name

Return type:

bool

classmethod promote(demangler)[source]

Promote a demangler to the highest-priority position.

>>> list(Demangler)
[<Demangler: MS>, <Demangler: GNU3>]
>>> Demangler.promote(list(Demangler)[0])
>>> list(Demangler)
[<Demangler: GNU3>, <Demangler: MS>]
Parameters:

demangler – Demangler to promote

classmethod register()[source]

Register a custom Demangler. Newly registered demanglers will get priority over previously registered demanglers and built-in demanglers.

name = None
demangle_generic(archOrPlatform: Architecture | Platform, mangled_name: str, view: BinaryView | None = None, simplify: bool = False) Tuple[Type | None, List[str]] | None[source]

demangle_generic demangles a mangled symbol name to a Type object.

Parameters:
  • archOrPlatform (Architecture | Platform) – Architecture or Platform for the symbol. Required for pointer/integer sizes and calling conventions.

  • mangled_name (str) – a mangled symbol name

  • view (BinaryView | None) – (optional) view of the binary containing the mangled name

  • simplify (bool) – (optional) Whether to simplify demangled names

  • archOrPlatform

Returns:

returns tuple of (Optional[Type], demangled_name) or None on error

Return type:

Tuple

Example:
>>> demangle_generic(Architecture["x86_64"], "?testf@Foobar@@SA?AW4foo@1@W421@@Z")
(<type: public: static enum Foobar::foo __cdecl (enum Foobar::foo)>, ['Foobar', 'testf'])
>>> demangle_generic(Architecture["x86_64"], "__ZN20ArmCallingConvention27GetIntegerArgumentRegistersEv")
(<type: immutable:FunctionTypeClass 'int64_t()'>, ['ArmCallingConvention', 'GetIntegerArgumentRegisters'])
>>>
demangle_gnu3(arch, mangled_name: str, options: bool | BinaryView | None = None)[source]

demangle_gnu3 demangles a mangled name to a Type object.

Parameters:
  • arch (Architecture) – Architecture for the symbol. Required for pointer and integer sizes.

  • mangled_name (str) – a mangled GNU3 name

  • options (Optional[Union[bool, BinaryView]]) – (optional) Whether to simplify demangled names : None falls back to user settings, a BinaryView uses that BinaryView’s settings, or a boolean to set it directly

Returns:

returns tuple of (Type, demangled_name) or (None, mangled_name) on error

Return type:

Tuple[Optional[Type], Union[str, List[str]]]

demangle_llvm(mangled_name: str, options: bool | BinaryView | None = None) List[str] | None[source]

demangle_llvm demangles a mangled name using the LLVM demangler.

Parameters:
  • mangled_name (str) – a mangled (msvc/itanium/rust/dlang) name

  • options (Optional[Union[bool, BinaryView]]) – (optional) Whether to simplify demangled names : None falls back to user settings, a BinaryView uses that BinaryView’s settings, or a boolean to set it directly

Returns:

returns demangled name or None on error

Return type:

Optional[List[str]]

Example:
>>> demangle_llvm("?testf@Foobar@@SA?AW4foo@1@W421@@Z")
['public: static enum Foobar::foo __cdecl Foobar::testf(enum Foobar::foo)']
>>>
demangle_ms(archOrPlatform: Architecture | Platform, mangled_name: str, options: bool | BinaryView | None = False)[source]

demangle_ms demangles a mangled Microsoft Visual Studio C++ name to a Type object.

Parameters:
  • archOrPlatform (Architecture | Platform) – Architecture or Platform for the symbol. Required for pointer/integer sizes and calling conventions.

  • mangled_name (str) – a mangled Microsoft Visual Studio C++ name

  • options (Optional[Union[bool, BinaryView]]) – (optional) Whether to simplify demangled names : None falls back to user settings, a BinaryView uses that BinaryView’s settings, or a boolean to set it directly

  • archOrPlatform

Returns:

returns tuple of (Type, demangled_name) or (None, mangled_name) on error

Return type:

Tuple[Optional[Type], Union[str, List[str]]]

Example:
>>> demangle_ms(Platform["x86_64"], "?testf@Foobar@@SA?AW4foo@1@W421@@Z")
(<type: public: static enum Foobar::foo __cdecl (enum Foobar::foo)>, ['Foobar', 'testf'])
>>>
get_qualified_name(names: Iterable[str])[source]

get_qualified_name gets a qualified name for the provided name list.

Parameters:

names (list(str)) – name list to qualify

Returns:

a qualified name

Return type:

str

Example:
>>> type, name = demangle_ms(Architecture["x86_64"], "?testf@Foobar@@SA?AW4foo@1@W421@@Z")
>>> get_qualified_name(name)
'Foobar::testf'
>>>
simplify_name_to_qualified_name(input_name: str | QualifiedName, simplify: bool = True)[source]

simplify_name_to_qualified_name simplifies a templated C++ name with default arguments and returns a qualified name. This can also tokenize a string to a qualified name with/without simplifying it

Parameters:
  • input_name (Union[str, QualifiedName]) – String or qualified name to be simplified

  • simplify (bool) – (optional) Whether to simplify input string (no effect if given a qualified name; will always simplify)

Returns:

simplified name (or one-element array containing the input if simplifier fails/cannot simplify)

Return type:

QualifiedName

Example:
>>> demangle.simplify_name_to_qualified_name(QualifiedName(["std", "__cxx11", "basic_string<wchar, std::char_traits<wchar>, std::allocator<wchar> >"]), True)
'std::wstring'
>>>
simplify_name_to_string(input_name: str | QualifiedName)[source]

simplify_name_to_string simplifies a templated C++ name with default arguments and returns a string

Parameters:

input_name (Union[str, QualifiedName]) – String or qualified name to be simplified

Returns:

simplified name (or original name if simplifier fails/cannot simplify)

Return type:

str

Example:
>>> demangle.simplify_name_to_string("std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >")
'std::string'
>>>