Is it possible to remove symbols from a shared library built with Android NDK?
Asked Answered
P

1

7

I'm building a shared library for use in a Java app using the Android NDK. Using readelf to inspect the lib/armeabi-v7a/libXXXlib.so file generated by a release build, it appears to contain all the symbols (function, variable names) of my native C/C++ code.

Indeed, the shared object file appears to be identical for the debug and release builds. (The only difference in the output in the libs folder being whether or not the gsb.setup and gdbserver files are created.) I'm overriding the optimisation set by the NDK with an APP_CFLAGS += -O3 in my Application.mk, but I wouldn't expect the release build to generate debug symbols by including the -g flag, which it does.

I found a comment on SO stating 'In both cases, debug and release, it leaves the -g flag in because, to quote the comment, "we generate symbol versions of the binaries that are later stripped when they are copied to the final project's libs/ directory"', and another stating: 'JNI cannot find the functions for java if they don't have names.'

ndk-build is certainly stripping something from the obj/armeabi-v7a/libXXXlib.so file, as that is much larger than the one eventually created in libs, but it still seems to be leaving all my functions' and variables' names intact in the file.

Are all my functions' names required by JNI (rather than just the entry-points into the native code)?

If not, how can I remove the symbols which are not required?

Passing answered 10/6, 2013 at 11:36 Comment(0)
O
9

Feel free to set default visibility=hidden, and set attribute visibility=default for the few functions you need to export (similar to DLLEXPORT) in Windows

Ozuna answered 10/6, 2013 at 20:25 Comment(4)
Also, if you use explicit registration of JNI methods, you don't even need those to be visible. See developer.android.com/training/articles/… .Collect
Thanks @alex-cohn, @fadden. I added -fvisibility=hidden to my LOCAL_CFLAGS in my Android.mk and it removed all the symbols apart from the JNI entry-points I'm exporting. (So I haven't set up explicit registration of JNI methods in JNI_OnLoad.) Do you know what's setting the visibility on those functions?Passing
Also, do you know why this isn't set by default for release builds by ndk-build?Passing
@Collect I did. Removing that removed them from the symbols. Thanks!Passing

© 2022 - 2024 — McMap. All rights reserved.