AVGTechnologies / cppmangle

A library for demangling and mangling Visual Studio C++ names.
Other
21 stars 8 forks source link

RuntimeError: unknown obj when using mangler #4

Open liorsegev opened 5 years ago

liorsegev commented 5 years ago

Hey,

After installation i tested the demangler and like the example in the readme file it works good.

i tried to take the demangled string and and use the mangle but i get an exception.

can you please write an example i might doing something wrong.

Thanks

twostars commented 2 years ago

This is an old issue but I was also a little confused on this. Since it doesn't appear to be documented (and I don't have a ton of experience with python), I went through it a bit to try and understand how it worked.

So: as it says in the readme (but I also seemed to miss), mangle in fact takes in an AST object (specifically an instance of the Function class).

It's currently defined in ast.py as such:

class Function(object):
    def __init__(self, qname, type, kind, access_spec, addr_space):
        self.qname = qname
        self.type = type
        self.kind = kind
        self.access_spec = access_spec
        self.addr_space = addr_space

Since demangle returns this, I played with this a bit to check each argument (and their arguments), etc, to finally come up with a simple example of a constructor created without demangle:

import cppmangle

cv_none = 0
cuitest_t = cppmangle.ClassType(
    cv_none,                    # cv (cv_const, cv_volatile or 0 for none)
    cppmangle.k_class,          # kind (k_union, k_struct, k_class, k_enum)
    ("CUITest",))               # qname (tuple of qualified name components)

cuitest_ptr_t = cppmangle.PtrType(
    cv_none,                    # cv (cv_const, cv_volatile or 0 for none)
    cuitest_t,                  # target (Type the pointer points to)
    False,                      # ref (boolean - "is it a reference?")
    cppmangle.as_default)       # addr_space (AddressSpace: as_default, as_msvc_x64_absolute)

cuitest_ctor_t = cppmangle.FunctionType(
    cppmangle.cconv_thiscall,   # cconv (calling convention: cconv_cdecl, cconv_stdcall, cconv_thiscall, cconv_fastcall)
    cuitest_ptr_t,              # ret_type (Type)
    [cuitest_ptr_t],            # params (list of Types)
    cv_none)                    # this_cv (cv_const, cv_volatile or 0 for none)

cuitest_ctor = cppmangle.Function(
    ("CUITest", cppmangle.n_constructor), # qname (tuple of qualified name components)
    cuitest_ctor_t,             # type (Type)
    cppmangle.fn_instance,      # kind (FunctionKind: fn_free, fn_instance, fn_virtual, fn_class_static)
    cppmangle.access_public,    # access_spec (AccessSpecifier: access_public, access_protected, access_private)
    cppmangle.as_default)       # addr_space (AddressSpace: as_default, as_msvc_x64_absolute)

# produces: ??0CUITest@@QAEPAV0@PAV0@@Z
print(cppmangle.mangle(cuitest_ctor))

Really, as the readme says, everything you need here is in ast.py - it just took a little bit to go through. There's a lot of presets for special names (n_*) and basic types (t_*) in there that can be used as well.

This probably isn't that helpful, since like it says, it is all in ast.py, but hopefully this helps save someone else some time.