dlopen suceeds, dlsym fails.

897 views
Skip to first unread message

Luiz Valdetaro

unread,
Sep 3, 2011, 4:47:53 PM9/3/11
to andro...@googlegroups.com
Hi, I have a shared library that is loaded from Java. That library works fine. However, I'm attempting to dynamically load another library from this library, it suceeds at finding the library, but not the function I'm trying to execute. This is the code snippet that faiils:

void *lhandle,*fhandle;
     lhandle = dlopen ("/data/data/com.mytestapp/files/libtws.so", RTLD_NOW);
     if (lhandle == NULL)
        jni_printf ("Failed to load libtws");
     else
        jni_printf ("libtws loaded fine");

     fhandle = dlsym (lhandle,"test_main");
     if (fhandle == NULL)
        jni_printf ("Failed to find function");
     else
        jni_printf ("Found function fine");

(jni_printf is a wrapper that goes to the java program, which in turn prints the message).

It always suceeds at the dlopen, but I get the error "Failed to find function" every time. My second library is very complex, so, I decided to create a simple one:

test_main (argc,argv)
int argc;
char *argv[];
{
// do something simple here
}

real simple. I can do the dlopen, but cannot find the symbol; When I do everything from the command line (no Java), the library is found fine. Any ideas?

Luiz

mic _

unread,
Sep 4, 2011, 2:50:42 AM9/4/11
to andro...@googlegroups.com
Have you tried dumping the symbol table of the second library with objdump?

/Michael

> --
> You received this message because you are subscribed to the Google Groups
> "android-ndk" group.
> To post to this group, send email to andro...@googlegroups.com.
> To unsubscribe from this group, send email to
> android-ndk...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/android-ndk?hl=en.
>
>

Luiz Valdetaro

unread,
Sep 4, 2011, 11:09:00 AM9/4/11
to andro...@googlegroups.com

Actually I found why.... I was copying the library from the assets folder. I have several libraries there, and copyed the wrong one. I guess one should never overlook the obvious....

On Sep 4, 2011 1:50 AM, "mic _" <mico...@gmail.com> wrote:

rod.d...@gmail.com

unread,
Sep 5, 2011, 12:01:00 AM9/5/11
to andro...@googlegroups.com
How are you doing the copy? Are you determining the destination path
programmatic-ally, or is it hard-coded?

Thanks,
Rodney

Luiz Valdetaro

unread,
Sep 5, 2011, 8:25:46 AM9/5/11
to andro...@googlegroups.com

Hardcoded, into the /data/data/mypackage/files folder;

rod.d...@gmail.com

unread,
Sep 5, 2011, 4:13:50 PM9/5/11
to andro...@googlegroups.com
Thanks Luiz. I can't hardcode the path. I was hoping you had done it
programmatic-ally.

Rodney

Luiz Valdetaro

unread,
Sep 5, 2011, 5:48:28 PM9/5/11
to andro...@googlegroups.com

Rodney, If you find a way, pls let me know....

mic _

unread,
Sep 6, 2011, 1:01:17 AM9/6/11
to andro...@googlegroups.com
Why not use getFilesDir in Java and send the path down to one of your
native functions?

/Michael

rod.d...@gmail.com

unread,
Sep 6, 2011, 8:27:10 AM9/6/11
to andro...@googlegroups.com

I would, but currently I have a completely native app that doesn't have any Java in it.  I think I will try grabbing access to that method from the native side.

Rodney

VladG2

unread,
Sep 6, 2011, 4:45:07 PM9/6/11
to android-ndk
I think your lib depends on some other libs which are not linked. You
can build bionic/linker/Android.mk LINKER_DEBUG 0 to 1 and rebuild
linker. Then use "logcat linker".

Regards,
Vlad

rod.d...@gmail.com

unread,
Sep 19, 2011, 12:08:39 AM9/19/11
to andro...@googlegroups.com
Hello Luiz

I am doing the following to grab access to my internal and external
storage areas:

status = vm->GetEnv((void **) &jni_env, JNI_VERSION_1_4);
if(status < 0) {
LOGE("callback_handler: failed to get JNI environment, ""assuming
native thread");
status = vm->AttachCurrentThread(&jni_env, NULL);
if(status < 0) {
LOGE("callback_handler: failed to attach ""current thread");
return;
}
}

cls_Env = jni_env->FindClass("android/app/NativeActivity");
mid_getExtStorage = jni_env->GetMethodID(cls_Env, "getFilesDir",
"()Ljava/io/File;");
obj_File = jni_env->CallObjectMethod( state->activity->clazz,
mid_getExtStorage);
cls_File = jni_env->FindClass("java/io/File");
mid_getPath = jni_env->GetMethodID(cls_File, "getPath",
"()Ljava/lang/String;");
obj_Path = (jstring) jni_env->CallObjectMethod(obj_File, mid_getPath);
path = jni_env->GetStringUTFChars(obj_Path, &stringgot);

LOGI("INTERNAL PATH = %s\n", path);
internal_path = strdup(path);
copy_libs(state->activity->assetManager, internal_path);

jni_env->ReleaseStringUTFChars(obj_Path, path);

mid_getExtStorage = jni_env->GetMethodID( cls_Env,
"getExternalFilesDir", "(Ljava/lang/String;)Ljava/io/File;");
obj_File = jni_env->CallObjectMethod(state->activity->clazz,
mid_getExtStorage, NULL);
cls_File = jni_env->FindClass("java/io/File");
mid_getPath = jni_env->GetMethodID(cls_File, "getPath",
"()Ljava/lang/String;");
obj_Path = (jstring) jni_env->CallObjectMethod(obj_File, mid_getPath);
path = jni_env->GetStringUTFChars(obj_Path, &stringgot);

LOGI("EXTERNAL PATH = %s\n", path);
external_path = strdup(path);

Now I can store my libs in my assets dir and move them from there.
The only problem I have now is trying to break things up in to
subdirectories. The native AAssetDir can read filenames, but not
directories.

Thanks,
Rodney

Reply all
Reply to author
Forward
0 new messages