Native C program "hello world" with shared library----''WARNING: `libmyhello.so` is not a prelinked library and execute error"

83 views
Skip to first unread message

jingshu...@mailhz.techfaith.cn

unread,
Dec 4, 2007, 9:40:33 PM12/4/07
to Android Developers
I want build a native C program "hello world" with shared library as
the below step:

1, build a shared library "libmyhello.so"
/*
* file: hello.h
*/
#ifndef _HELLO_H
#define _HELLO_H

void hello(const char* name);

#endif//_HELLO_H


/*
* file: hello.c
*/
#include <stdio.h>

void hello(const char* name)
{
printf("Hello %s!\n", name);
}

$ arm-none-linux-gnueabi-gcc -c hello.c// generating hello.o
$ arm-none-linux-gnueabi-ld -rpath . hello.o ../arm-2007q3/arm-none-
linux-gnueabi/libc/lib/libc.so -I /system/bin/linker -shared -fpic -o
libmyhello.so


2, build main to call shared library "libmyhello.so"
/*
* file: main.c
*/
#include "hello.h"

int main(void)
{
hello("everyone");
return 0;
}

$ arm-none-linux-gnueabi-gcc -c main.c// generating main.o
$ arm-none-linux-gnueabi-ld -rpath . main.o -L. libmyhello.so ../
arm-2007q3/arm-none-linux-gnueabi/libc/lib/libc.so -I /system/bin/
linker -o hello

3, adb push libmyhello.so /system/lib/
4, adb push hello /data/

5, adb shell /data/hello
WARNING: `libmyhello.so` is not a prelinked library
[1] Segmentation fault /data/hello

Pls help?

Davanum Srinivas

unread,
Dec 4, 2007, 9:54:00 PM12/4/07
to android-d...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I had the same issue (see android-internals). I still have no clue how to overcome this issue.

- -- dims

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (Cygwin)

iD8DBQFHVhLIgNg6eWEDv1kRAuMbAKCTBHoI7fmVE6gCnUpsBuF2ZLW6lACfbM5W
/3PnHJ5wZ4urAQdaU1tkGhU=
=dnQE
-----END PGP SIGNATURE-----

hdante

unread,
Dec 5, 2007, 9:15:43 AM12/5/07
to Android Developers


On Dec 5, 12:40 am, "jingshuang.z...@mailhz.techfaith.cn"
<jingshuang.z...@mailhz.techfaith.cn> wrote:
>
> 3, adb push libmyhello.so /system/lib/
> 4, adb push hello /data/
>
> 5, adb shell /data/hello
> WARNING: `libmyhello.so` is not a prelinked library
> [1] Segmentation fault /data/hello

The example suggests that using a non-prelinked library results in a
segmentation fault (which may or may not be true). This could be true
if, for example, the loader is so simple that it doesn't support
dynamic linking. Try prelinking the library with android system
libraries, then prelinking hello with libmyhello. You may need to
compile a fully standalone "prelink" to run it inside android.

http://en.wikipedia.org/wiki/Prelinking



>
> Pls help?

Digit

unread,
Dec 5, 2007, 10:44:46 AM12/5/07
to android-d...@googlegroups.com
be aware that Android's prelinking and dynamic linkers are non standard, which means that running the GNU "prelink" utility on your shared libraries is likely to not work at all.
the WARNING is simply that, a warning, and should not prevent the dynamic linking to occur, unless there is something weird.
(this is normal because all the native binaries we have in the system are pre-linked for instant loading and reduced footprint).

try compiling your program with -static to see if this solves your problem.

dasGrunge

unread,
Dec 5, 2007, 11:33:45 AM12/5/07
to Android Developers
Ah, that's probably it. What it also could mean is that you've linked
against the wrong loader. The loader is specific to the libc that you
use. You are probably linking against glibc or newlib. My
understanding from another thread recently is that Android uses a
different libc that is BSD licensed. I'm not sure which one or if it's
homegrown, but I think we really need to wait until the full stack is
opened before we can build native apps.

BTW, the workaround is to link with your libraries statically so that
it doesn't try to load any shared objects. But then you get big
executables.

zjshb

unread,
Dec 5, 2007, 9:36:59 PM12/5/07
to Android Developers
I found that the lastest 4 bytes of .so of android all are "PRE ",
so I force change the 4 byte to "PRE ", as expected ,
loaded successfully.

but be sure don't to know how to prelink my so?

Davanum Srinivas

unread,
Dec 6, 2007, 12:05:08 AM12/6/07
to android-d...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

zjshb,

i could not understand. Can you please explain how you got it working? did you use a hex editor to change the bytes in
the .so? which ones? i can't find PRE when i look at say libwebcore.so

thanks,
dims

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (Cygwin)

iD8DBQFHV4MEgNg6eWEDv1kRAk56AJ4sZM369kjY9UpIiDrk2tM18Hz/UACgmGPC
TtTXjfsl3PLISLhSbv66H8Q=
=bQKD
-----END PGP SIGNATURE-----

zhaojingshuang

unread,
Dec 6, 2007, 1:40:57 AM12/6/07
to android-d...@googlegroups.com

> uld not understand. Can you please explain how you got it working? did
> you use a hex editor to change the bytes in
> the .so? which ones? i can't find PRE when i look at say libwebcore.so

I used ultraeidt in windows to change the last 4 byte into lib*.so, but
only loaded successfully, when call function into lib*.so, no success.

motz

unread,
Dec 8, 2007, 3:27:40 PM12/8/07
to Android Developers
Reply all
Reply to author
Forward
0 new messages