Flamefire
Flamefire

Reputation: 5807

Change the exported function name of a compiled shared library

Long story short: I have a compiled shared library where the source code is not available and it has the name of an important function (C++) mangled.

Is it possible to patch this binary somehow such that the exported name is not mangled? I know patchelf can modify the dependencies, but I don't know how to modify the symbols.

Longer: I have a libEGL.so from a phone which has e.g. _Z14eglCreateImagePvS_jS_PKi instead of eglCreateImage which causes the EGL-Loader to not find it (via dlsym) because it (of course) searches only for the non-mangled names.

Strangely all other APIs (except for 6) are exported without mangling, so I expect this to be a bug on their side.

Patching the loader to consider mangled names is possible but also a lot of work, so I'd rather change or add the correct names to the binary.

Upvotes: 1

Views: 828

Answers (2)

The ability to do this was added in Patchelf: https://github.com/NixOS/patchelf/commit/da035d6acee1e5a608aafe5f6572a67609b0198a

It should be available in the next release (after 0.17.2). Meanwhile you can build the tool following the instructions in https://github.com/NixOS/patchelf#compiling-and-testing

To use it, create a map file with two names (old and new) per line, and invoke Patchelf with:

$ patchelf --output libPatched.so --rename-dynamic-symbols map_file libOriginal.so

Please provide feedback if you find issues. Thanks!

Upvotes: 0

datenwolf
datenwolf

Reputation: 162234

If the loader uses dlsym with RTLD_DEFAULT you could introduce a weak alias introduce into your main program. In case there's the proper function available, that weak alias will be ignored. Compile with a C compiler (not C++):

// egl_mangle_wrapper.c
EGLImage eglwrap_CreateImage(
    EGLDisplay dpy,
    EGLContext ctx,
    EGLenum tgt,
    EGLClientBuffer buf,
    const EGLAttrib *atrl )
{
    return _Z14eglCreateImagePvS_jS_PKi(dpy, ctx, tgt, buf, atrl);
}

EGLImage eglCreateImage(
    EGLDisplay dpy,
    EGLContext ctx,
    EGLenum tgt,
    EGLClientBuffer buf,
    const EGLAttrib *atrl )
        __attribute__ ((weak, alias ("eglwrap_CreateImage")));

Upvotes: 2

Related Questions