1

I was cross compiling for android using linaro and codesourcery toolchains i found even after providing -static here problem seems to come from glibc dynamically link libnss_* libraries.

Here is my code

#include <sys/types.h>
#include <pwd.h>

int main(){
   struct passwd *pw = getpwnam("root");
   return 0;
}

run following command

arm-linux-gnueabihf-gcc -static  pwnam_test.c -lc -o pwtest

after stracing it a got following output

11455 uname(0xf6ffeb70) = 0 11455 brk(NULL) = 0x0006d000 11455 brk(0x0006dd00) = 0x0006dd00 11455 brk(0x0008ed00) = 0x0008ed00 11455 brk(0x0008f000) = 0x0008f000 11455 socket(1,526337,0,0,445504,319244) = 3 11455 connect(3,0xf6ffea30,110) = -1 errno=2 (No such file or directory) 11455 close(3) = 0 11455 socket(1,526337,0,1,445504,0) = 3 11455 connect(3,0xf6ffeb50,110) = -1 errno=2 (No such file or directory) 11455 close(3) = 0 11455 open("/etc/nsswitch.conf",O_RDONLY|O_CLOEXEC) = 3 11455 fcntl64(3,F_GETFD) = 1 11455 fstat64(3,0xf6ffeb78) = 0 11455 mmap2(NULL,4096,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS,-1,0) = 0xf67fe000 11455 read(3,0xf67fe000,4096) = 513 11455 read(3,0xf67fe000,4096) = 0 11455 close(3) = 0 11455 munmap(0xf67fe000,4096) = 0 11455 open("/etc/ld.so.cache",O_RDONLY|O_CLOEXEC) = 3 11455 fstat64(3,0xf6ffe450) = 0 11455 mmap2(NULL,88624,PROT_READ,MAP_PRIVATE,3,0) = 0xf67e9000 11455 close(3) = 0 11455 access("/etc/ld.so.nohwcap",F_OK) = -1 errno=2 (No such file or directory) 11455 open("/lib/arm-linux-gnueabihf/libnss_compat.so.2",O_RDONLY|O_CLOEXEC) = -1 errno=2 (No such file or directory) 11455 stat64("/lib/arm-linux-gnueabihf",0xf6ffe488) = -1 errno=2 (No such file or directory) 11455 open("/usr/lib/arm-linux-gnueabihf/libnss_compat.so.2",O_RDONLY|O_CLOEXEC) = -1 errno=2 (No such file or directory) 11455 stat64("/usr/lib/arm-linux-gnueabihf",0xf6ffe488) = -1 errno=2 (No such file or directory) 11455 open("/lib/libnss_compat.so.2",O_RDONLY|O_CLOEXEC) = -1 errno=2 (No such file or directory) 11455 stat64("/lib",0xf6ffe488) = 0 11455 open("/usr/lib/libnss_compat.so.2",O_RDONLY|O_CLOEXEC) = -1 errno=2 (No such file or directory) 11455 stat64("/usr/lib",0xf6ffe488) = 0 11455 munmap(0xf67e9000,88624) = 0 11455 exit_group(0) how can i able to statically link all dynamic required library or do i need to cross compile glibc ?

Well, i am not in the favor of using NDK because i am trying to cross compile nginx somehow it execute but on accessing localhost:8080 nginx doesn't responds

Shushant
  • 1,625
  • 1
  • 13
  • 23

2 Answers2

3

Even when you use -static glibc will still use dlopen to use the local libraries for things like DNS.

I'm afraid you can't stop it doing this; it's just the way it is. Trying to use a glibc-based Linux toolchain for Android is probably the wrong thing to do (although you can certainly install glibc into Android if you choose -- into a chroot, say, or with alternative -Wl,-rpath and -Wl,--dynamic-linker settings).

Note that passing -lc is usually redundant (although I'm surprised you didn't have to pass -ldl to make the link work).

I'd suggest you get a real Android toolchain, configured to work with the Bionic C library, and use that. The Google NDK will work, and Linaro do one too (they do both Android and Linux so make sure you get the right one). All the toolchains use GCC so you should have no problems figuring out how to use it.

ams
  • 24,923
  • 4
  • 54
  • 75
  • i have NDK installed in my system but due to limited support of Bionic i am restricted to use glibc well linaro toolchain doesnt have support for armv4 or armv5te cpu even after passing `march=armv5te` it simply ignores – Shushant Mar 19 '14 at 10:20
  • ya, i need to pass `-ldl` when i try to cross compile php – Shushant Mar 19 '14 at 10:22
  • 1
    In that case, read up on `-rpath`, `--dynamic-linker`, and experiment with installing glibc into your device. Then you won't need `-static` in the first place. It usually sufficient to copy the `libc` directory from the CodeSourcery toolchain onto your device (you won't need all the subdirectories). Beware that those are GPL libraries, so if you distribute them then you also need to have the source handy if anybody asks for it. – ams Mar 21 '14 at 15:36
0

Android is almost a different OS using Linux kernel, so you can't really use other toolchains rather than NDK to build native binaries for Android.

For example from your strace, Android doesn't put libraries under lib or /usr/lib but /system/lib.

About dynamic linking, you probably shouldn't pass -lc but pass static libraries when invoking compiler.

auselen
  • 27,577
  • 7
  • 73
  • 114