Need little help for JNI

141 views
Skip to first unread message

Diluka Moratuwage

unread,
Feb 19, 2008, 2:08:53 AM2/19/08
to Android Internals
Hi all,
I have a new problem in Android, hope some one can give some light
on this; in one of my applications I use JNI. My story is follows; I
have two libraries called libloader.so and libutil.so. In my java
application I do a System.loadLibrary("loader"). And this works fine and
I can call a native method in loader library successfully. Then I need
to call a function in the util library from loader library. But when I
do that, it gives a unsatisfied linker exception.

Then I tried to load the util library manually to test whether java
can find it, by calling System.loadLibrary("util"). But this fails
giving an unsatisfied linker error. I have used the symbolic links and
so on accurately. I'm 100% sure that java can find the library. (I use
the same way as I do to the loader library here)

What I have a suspect on the headers of the libraries. I used the
readelf -l command and what I found was as follows;
**When I did it on loader library the header information was;

root@diluka-laptop:/home/diluka/TestPrograms/JNI-svc-client/Android/svc_client/src/com/android/wso2/loader-testing/deploy/lib#
readelf -l libloader.so.0.0.0

Elf file type is DYN (Shared object file)
Entry point 0x23c
There are 3 program headers, starting at offset 52

Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
LOAD 0x000000 0x00000000 0x00000000 0x002f0 0x002f0 RWE 0x8000
DYNAMIC 0x000284 0x00000284 0x00000284 0x00060 0x00060 RW 0x4
GNU_STACK 0x000000 0x00000000 0x00000000 0x00000 0x00000 RW 0x4

Section to Segment mapping:
Segment Sections...
00 .hash .dynsym .dynstr .text .rodata .dynamic .got
01 .dynamic
02

** But when I did the same thing for the util library I got the following;

root@diluka-laptop:/home/diluka/projects/EmbeddedAxis2c/c/deploy/lib#
readelf -l libutil.so.0.0.0

Elf file type is DYN (Shared object file)
Entry point 0x6cb4
There are 3 program headers, starting at offset 52

Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
LOAD 0x000000 0x00000000 0x00000000 0x16f84 0x19b18 RWE 0x8000
DYNAMIC 0x0169fc 0x000169fc 0x000169fc 0x00098 0x00098 RW 0x4
GNU_STACK 0x000000 0x00000000 0x00000000 0x00000 0x00000 RW 0x4

Section to Segment mapping:
Segment Sections...
00 .hash .dynsym .dynstr .rel.dyn .rel.plt .plt .text .rodata
.data.rel.ro .dynamic .got .data .bss
01 .dynamic
02

I think any library with simple header info like above get loaded but
the libraries like util didn't get loaded for me. Anybody have some idea
on this ? Please put me a small info on this if you know something about
this.

Thanks,
Diluka.


Diluka Moratuwage

unread,
Feb 20, 2008, 11:52:53 PM2/20/08
to android-...@googlegroups.com
Does anybody have some idea of this problem ? My problem is when JNI
loads one library and it uses another one, the second library which is
used internally by the first library is not loaded and linked properly.

Thanks,
Diluka.

hackbod

unread,
Feb 21, 2008, 1:21:30 AM2/21/08
to Android Internals
I don't know if this is the cause of your problem, but the android
system already has a library called libutil.

Diluka Moratuwage

unread,
Feb 21, 2008, 2:57:19 AM2/21/08
to android-...@googlegroups.com
Hi Hackbod,
Thanks for the answer. When I do load only one library, when I use
logcat, I get the following output. Do you see something suspicious in
there if you are familiar with that. This is a simple native function,
which only return a string. It doesn't load another library. I'm
suspicious about JNI_OnLoad not found thing. Do you have some idea about
that.

Thanks,
Diluka.


I/ActivityManager( 467): Starting activity: Intent {
action=android.intent.action.MAIN launchFlags=4
comp={com.android.wso2/com.android.wso2.svc_client} }
I/ActivityManager( 467): Starting app process com.android.wso2 to host
activity {com.android.wso2/com.android.wso2.svc_client}: pid=798
D/jdwp ( 798): JDWP listening on port 8004
I/ActivityThread( 798): Loading code package com.android.wso2 (in
com.android.wso2)
D/ActivityThread( 798): Performing launch of ActivityRecord{400cc9a0
{com.android.wso2/com.android.wso2.svc_client}}
I/ActivityManager( 467): Displayed activity
{com.android.wso2/com.android.wso2.svc_client}: 429 ms
I/ActivityManager( 467): Done launching
{com.android.wso2/com.android.wso2.svc_client}: 432 ms
D/ActivityManager( 467): Stopping: HistoryRecord{40174cb0
{com.google.android.home/com.google.android.home.AllApps}}
D/dalvikvm( 798): LOADING path /system/lib/libloader.so 0x4005c9e8
I/dalvikvm( 798): Added shared lib /system/lib/libloader.so 0x4005c9e8
I/dalvikvm( 798): No JNI_OnLoad found in /system/lib/libloader.so
0x4005c9e8
D/dalvikvm( 798): +++ not scanning '/system/lib/libwebcore.so' for
'invoke' (wrong CL)
D/dalvikvm( 798): +++ not scanning '/system/lib/libmedia_jni.so' for
'invoke' (wrong CL)

Diluka Moratuwage

unread,
Feb 21, 2008, 12:52:12 PM2/21/08
to android-...@googlegroups.com
When I try to load a C library (for example libcrypto.so), using a java
program in Android, it can't load that function. For ex: I called
System.loadLibrary("crypto"); and it fails giving a "can't find library"
error message. When I look at the logcat I get the log as follows;

D/dalvikvm( 781): LOADING path /system/lib/libloader.so 0x400cffd8
I/dalvikvm( 781): Added shared lib /system/lib/libloader.so 0x400cffd8
I/dalvikvm( 781): No JNI_OnLoad found in /system/lib/libloader.so
0x400cffd8
D/dalvikvm( 781): LOADING path /system/lib/libcrypto.so 0x400cffd8
I/dalvikvm( 781): Unable to dlopen(/system/lib/libcrypto.so): Cannot
find library

But the library is in the place /system/lib/libcrypto.so. But for some
reason it can't be loaded using JNI. Anybody has some idea on this ?
Anybody has some idea please ?

Diluka Moratuwage

unread,
Feb 22, 2008, 4:34:59 AM2/22/08
to android-...@googlegroups.com
Anybody has some idea of the linker that is used by JNI to load native
libraries in Android ?

Diluka Moratuwage

unread,
Feb 22, 2008, 6:18:17 AM2/22/08
to android-...@googlegroups.com
Yep, I do, that is default linker. Dalvik is using that. So I have run
into a nice problem now. I need to use a custom linker for my java
application that use native libraries. But the virtual machine that is
inside Android (Dalvikvm) uses the default linker (/system/bin/linker)
in order to load the native libraries that are needed by my application.
Anybody has some idea, that my application (java) needs to use a
seperate linker instead of system linker. But seems the Dalvikvm
prevents me of using that custom linker inside android. Any ideas of
using a custom linker to run applications that load native libraries ?

Susmith M R

unread,
Feb 24, 2008, 2:00:28 PM2/24/08
to Android Internals
Hai Diluka,
I don't have enough knowledge in JNI and java coding. But i have
tried creating c++ application with the support of custom linker. I
have
successfully did that.
How r u creating libloader and libutils libraries?
If u r creating it manually then do not use -Wl,-dynamic-linker=/
system/lib/linker. Use -Wl,-dynamic-linker=/system/lib/ld-linux.so.3
and push this library to android /system/lib directory
I doubt about what u said about loading shared library of JNI.
If u want to know how to create a shared library without the android
linker
follow the link http://groups.google.com/group/android-internals/browse_thread/thread/7c728a36474fbc05


Diluka Moratuwage

unread,
Feb 24, 2008, 9:50:07 PM2/24/08
to android-...@googlegroups.com
Thanks Susmith, but the problem is my front end is java. So, it the
Dalvikvm is the one who is responsible for calling the dynamic linker
(it calls /system/bin/linker), so it can't load any library other than
the one in the Android i.e. Can't load GLIBC libraries. I also am using
your tactic. Now going to try some other way (compiling the toolchain to
suite for Android.) Then those GLIBC libraries will be loaded by the
default linker. Any thoughts ?

Thanks,
Diluka.

Susmith M R

unread,
Feb 28, 2008, 1:23:22 PM2/28/08
to Android Internals
Hai Diluka,
I have one idea. I don't know whether it will work or not. I haven't
tried it yet. You have to create a very simple shared library(which is
created by /system/bin/linker) with Benno's approach. Call your
orginal library ( which is created with linker /system/lib/ld-linux.so.
3) from the above created simple library through dl_open functions.
Then i think it will use the ld-linux.so.3 linker to load the
applcation. This is only an idea. The feasibility of this technique is
not identified. If u can do it pls inform me also.
Best of luck,
Susmith

Diluka Moratuwage

unread,
Feb 28, 2008, 1:50:17 PM2/28/08
to android-...@googlegroups.com
HI Susmith,
I'm happy that at least one person replied to me :), thanks Susmith.
Well I tried that technique it doesn't work. What I figured out is that,
since the Dalvik's linker is /system/bin/linker we have to depend upon
that. What I mean is that, we need to come up with libraries that can be
loaded by the default android linker. But the real problem is that,
since our libraries internally depend on toolchain's libc libraries, the
default linker (system/bin/linker) tries to load them and fails (even
though we call dl_open by that simple library the linker is the default
one for the process - vm as I figured out). So it's not really working.
I'm trying to build a toolchain which is compatible with Android. Then
all libraries and binaries in the toolchain, will be able to be put into
android. I think this will work. Any ideas on this ?

Thanks,
Diluka.

Susmith M R

unread,
Mar 2, 2008, 2:25:51 PM3/2/08
to Android Internals
Hai Diluka,
Because of this jni difficulty and some other problem, i am turning
my approach to develop a virtual machine or converting my machine code
to bytecode, evenif the resulting code is not perfectly suited for
embedded system'. I don't know why this android people did like this.
There is already a strong bootstrap loader is available for them in
linux, they changed it.
if u have interest.. u compare the binaries of /system/bin/linker and
ld-linux.so.3 and it may help in developing ur toolchain. Because they
have removed this ld-linux.so.3 from the shared libraries inside the
android and that's why these libraries were pre-linked.

Diluka Moratuwage

unread,
Mar 2, 2008, 11:17:00 PM3/2/08
to android-...@googlegroups.com
Hi Susmith,
Yes you are true. I made a huge effort to get this thing done. But
still seems I have more work :(. I managed to build the glibc libraries
with the help of modified linker script the found in motz blog. So the
glibc libraries have the format which can be loaded by the Android
linker. But the problem is that, glibc library (libc-2.5), has an entry
that requests the linker ld-linux.so.3. So if I managed to remove that
dependency I think I will be a step forward to a conclusion (whether
it's possible to do it or not). At the moment, I'm trying to build the
glibc, so that, it avoids the hard dependency to /lib/ld-linux.so.3. Do
you have some idea on this ? Or some resource regarding this ?

Thanks,
Diluka.

Diluka Moratuwage

unread,
Mar 2, 2008, 11:23:01 PM3/2/08
to android-...@googlegroups.com
By the way, does anybody think that, even though glibc libraries were
build to suit for Android, will the Android linker be able to load and
link them with a C application ? Anybody has some idea on this ?

Thanks,
Diluka.

jitendra

unread,
Mar 18, 2008, 5:23:57 AM3/18/08
to Android Internals
Hi,

I have created a libhello.so with one native unction to print a hello
world message. I am loading this library from my java code and i see
following logs in my logcat window:

INFO/dalvikvm(934): Added shared lib /system/lib/libhello.so
0x400692f0
DEBUG/dalvikvm(934): +++ not scanning '/system/lib/libwebcore.so' for
'hello' (wrong CL)
DEBUG/dalvikvm(934): +++ not scanning '/system/lib/libmedia_jni.so'
for 'hello' (wrong CL)

Does anybody why exactly this error is coming ?

Thanks,
Jitendra
> >>> on this; in one of my applications I useJNI. My story is follows; I

sbVB

unread,
Mar 19, 2008, 12:10:29 PM3/19/08
to Android Internals
Dear Diluka,

I also want VERY MUCH to use JNI. Just like you. Java app that calls
compiled from C++.

I've been following the comments in this thread of yours, while trying
to make something work. So far, I couldn't make a decent "hello JNI"
to work.

But I have an idea I'd like to tell you.
I'm really confident that some recipe for that must exist.
The Android framework has these tiers:
1) linux kernel (compiled C code)
2) C++ tier (includes OpenGL)
3) Java tier
4) Java application tier (where most people produce their
applications).

One can use OpenGL from the Java app tier (4) using classes in the
Java tier (3), that calls OpenGL from the C++ tier (2) using JNI.

Since that's possible, and everything is open source anyway, we should
be able to replicate the same procedure and reach a recipe to develop
a custom library in the C++ tier (2), with some adequate JNI in tier
(3) so that the application tier (4) use it.

I'm working on this idea. I will let the community know of any
progress I reach, however partial.

Digit

unread,
Mar 19, 2008, 12:15:50 PM3/19/08
to android-...@googlegroups.com
jus a clarification: everything is not open-source *yet*.

sbVB

unread,
Mar 20, 2008, 8:08:08 PM3/20/08
to Android Internals
The Android code is available at link below

http://code.google.com/p/android/downloads/list
Reply all
Reply to author
Forward
0 new messages