Native C compilation

44 views
Skip to first unread message

Diluka Moratuwage

unread,
Jan 9, 2008, 4:20:58 AM1/9/08
to Android Internals
Hi all,
I read articles posted by motz, but I'm curious if we can build
C applications without calling the linker explicitly. Do you have
any idea regarding the matter. I used the following command to build
a program, but seems it's not working properly.

arm-none-linux-gnueabi-gcc hello.c -v -o hello
-L/home/diluka/projects/softwares/arm-2007q3/arm-none-linux-gnueabi/libc/lib
-Wl,--dynamic-linker /system/bin/linker -Wl,-rpath /system/lib
-Wl,-L/home/diluka/projects/softwares/arm-2007q3/arm-none-linux-gnueabi/libc/lib
-Wl,-lc

I have got the following strace as well.

# ./strace ./hello
execve("./hello", ["./hello"], [/* 9 vars */]) = 0
getpid() = 2855
syscall_983045(0xb0016b48, 0xb0013760, 0x3e4, 0, 0xbead7e58, 0x1, 0,
0xf0005, 0xb0013760, 0, 0, 0xbead7e54, 0, 0xbead7e08, 0xb0000d89,
0xb00016ec, 0x10, 0xb0016b48, 0, 0, 0xc764, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0) = 0
gettid() = 2855
sigaction(SIGILL, {0xb0001469, [], SA_RESTART}, {SIG_DFL}, 0) = 0
sigaction(SIGABRT, {0xb0001469, [], SA_RESTART}, {SIG_DFL}, 0) = 0
sigaction(SIGBUS, {0xb0001469, [], SA_RESTART}, {SIG_DFL}, 0) = 0
sigaction(SIGFPE, {0xb0001469, [], SA_RESTART}, {SIG_DFL}, 0) = 0
sigaction(SIGSEGV, {0xb0001469, [], SA_RESTART}, {SIG_DFL}, 0) = 0
sigaction(SIGSTKFLT, {0xb0001469, [], SA_RESTART}, {SIG_DFL}, 0) = 0
open("libc.so.6", O_RDONLY|O_LARGEFILE) = 3
lseek(3, -8, SEEK_END) = 231908
read(3, "\0\0\340\257PRE ", 8) = 8
mmap2(0xafe00000, 233472, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE,
3, 0) = 0xafe00000
close(3) = 0
mmap2(0xafe39000, 45056, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, 0, 0) = 0xafe39000
mprotect(0xafe00000, 221184, PROT_READ|PROT_EXEC) = 0
fstat64(1, {st_mode=S_IFCHR|0600, st_rdev=makedev(253, 0), ...}) = 0
brk(0) = 0x11000
brk(0x11000) = 0x11000
brk(0x13000) = 0x13000
ioctl(1, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig icanon echo
...}) = 0
write(1, "ERROR: 2855 cannot locate \'__li"..., 50ERROR: 2855 cannot
locate '__libc_start_main'...
) = 50
write(1, "ERROR: failed to link ./hello\n", 30ERROR: failed to link ./hello
) = 30
write(1, "ERROR: CANNOT LINK EXECUTABLE \'."..., 40ERROR: CANNOT LINK
EXECUTABLE './hello'
) = 40
exit_group(-1) = ?
Process 2855 detached

It seems that, when I do not specifically call the linker, dynamic
linking doesn't happen and it can't find
the simbles in the libc.so. Any thoughts ?

Thanks,
Diluka.

motz

unread,
Jan 9, 2008, 7:03:34 PM1/9/08
to Android Internals
Hi, Diluka,

I'm not sure what you want to do, but I have an idea why the error
occurs.

The cause of trouble is that you are about to link your program with
gnu libc
which is supplied by toolchains. The gnu libc exists on your host but
not on the
android. The android libc is not compatible with gnu libc. As I
mention on my blog,
copy the android libc on your host, and link your program with it.

motz

Diluka Moratuwage

unread,
Jan 9, 2008, 9:46:55 PM1/9/08
to android-...@googlegroups.com
Hi Motz,
Thanks for the light. So I need to copy the /system/lib folder (or the
libc.so) from android
into my host and put somewhere like ~/tmp/system... and link my programs
against it. Is that
what you meant ? (just to make it more clear). And I saw some libc
initialization things in your
blog. Do I need to do something other than linking against as above ?
Thank you.

Thanks,
Diluka.

Diluka Moratuwage

unread,
Jan 10, 2008, 12:08:38 AM1/10/08
to android-...@googlegroups.com
Hi Motz,
If I explain the exact problem I have is that I can build and link and
run the
executable using the following command;

arm-none-linux-gnueabi-gcc -c hello.c -v
arm-none-linux-gnueabi-ld hello.o -lc
-L/home/projects/softwares/arm-linux/arm-2007q3-none-linux-gnueabi-i686-51/arm-none-linux-gnueabi/libc/lib
--dynamic-linker /system/bin/linker -o hello -v

But what I want is, instead of explicitly calling the linker, I need to
get it done,
using the compiler; What I mean is something like this;
arm-none-linux-gnueabi-gcc hello.c -o hello {all the arguments}
I tried this in the usual way, but seems the compiler calls it's linker
and it links to a different libc.so than what is in android.

Thanks,
Diluka.

motz

unread,
Jan 10, 2008, 12:26:01 AM1/10/08
to Android Internals
> Thanks for the light. So I need to copy the /system/lib folder (or the
> libc.so) from android
> into my host and put somewhere like ~/tmp/system... and link my programs
> against it. Is that
> what you meant ? (just to make it more clear).

Yes.

> And I saw some libc
> initialization things in your
> blog. Do I need to do something other than linking against as above ?

It depends on whether your program needs the initialization of libc or
not. Generally the libc should be initialized at the time the program
starts. Please read my blog below how to initialize it. The "Hello
world" program is so simple that I know it does not need the
initialization of libc.

Initialize libc for Android: http://honeypod.blogspot.com/2007/12/initialize-libc-for-android.html

motz

Diluka Moratuwage

unread,
Jan 10, 2008, 1:01:11 AM1/10/08
to android-...@googlegroups.com
HI Motz,
Thanks for your reply. I did copy, and trying to build using that, so I
now get the following error messages; I guess I need to do the
initialization isn't it.

diluka@diluka-laptop:~/projects/BuildSystem/temp$ sh b.sh
/home/diluka/projects/softwares/arm-2007q3/bin/../arm-none-linux-gnueabi/libc/usr/lib/crt1.o:
In function `_start':
init.c:(.text+0x24): undefined reference to `__libc_start_main'
init.c:(.text+0x2c): undefined reference to `__libc_csu_fini'
init.c:(.text+0x34): undefined reference to `__libc_csu_init'

Thanks,
Diluka.

Diluka Moratuwage

unread,
Jan 10, 2008, 1:32:29 AM1/10/08
to android-...@googlegroups.com
Hi motz,
I got it working. I can issue only -gcc and build the project now.
Thanks a lot for pointing me to the correct way.

Thanks,
Diluka.

motz

unread,
Jan 10, 2008, 1:48:48 AM1/10/08
to Android Internals
Hi Diluka,

You still use crt of gnu libc. You need to use the crt of android's
libc. Try the crt0.s on my blog.

Notice that the crt of android's libc is not supplied officially. The
crt0.s on my blog is gotten by hack.

My crt0.s needs to be generalized for the practical use. I hope
someone do the job.

motz

motz

unread,
Jan 10, 2008, 1:52:17 AM1/10/08
to Android Internals
Hi Diluka,

My previous post had a time lag, so ignore it.

Anyway, Congratulations!!

motz

Diluka Moratuwage

unread,
Jan 10, 2008, 1:59:40 AM1/10/08
to android-...@googlegroups.com
Hi Motz,
No, I used that crt0.s file you had posted, and I wish to contribute for
generalization.
Thank you for posting these important blogs.

Thanks,
Diluka.

Diluka Moratuwage

unread,
Jan 16, 2008, 4:29:42 AM1/16/08
to android-...@googlegroups.com
Hi all,
Now I got a little weired problem when I build my project. It seems
that, there is some incompatibility between the libc.so that
comes with the cross compiler and the libc.so of android. I have copied
the libc.so library (and others) from android into my host
and link against them. But still the compiler use it's own libc.so for
some tasks as I can observe.

/home/diluka/projects/softwares/arm-2007q3/bin/../lib/gcc/arm-none-linux-gnueabi/4.2.1/../../../../arm-none-linux-gnueabi/bin/ld:
h_errno: TLS definition in
/home/diluka/projects/softwares/arm-2007q3/bin/../arm-none-linux-gnueabi/libc/lib/libc.so.6
section .tbss mismatches non-TLS definition in /system/lib/libc.so
section .bss
/home/diluka/projects/softwares/arm-2007q3/bin/../arm-none-linux-gnueabi/libc/lib/libc.so.6:
could not read symbols: Bad value
collect2: ld returned 1 exit status

Anybody has some idea regarding the matter ? Any ideas, highly appreciated.

Thanks,
Diluka.

Diluka Moratuwage

unread,
Jan 16, 2008, 4:48:23 AM1/16/08
to android-...@googlegroups.com
Please ignore this mail. I'm very sorry. It was my bad. It's working now.

Thanks

Reply all
Reply to author
Forward
0 new messages