Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile a static binary which code there a function gethostbyname

How to resolve compile a static binary which code include a function gethostbyname and if compiled without warning like this:

warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking

I compile on ubuntu 12.04 with command:

$ gcc -static lookup.c -o lookup 

This is code for lookup.c:

  /* lookup.c */    #include <stdio.h>   #include <unistd.h>   #include <stdlib.h>   #include <string.h>   #include <errno.h>   #include <sys/socket.h>   #include <netinet/in.h>   #include <arpa/inet.h>   #include <netdb.h>    extern int h_errno;    int main(int argc,char **argv) {      int x, x2;      struct hostent *hp;       for ( x=1; x<argc; ++x ) {         hp = gethostbyname(argv[x]);         if ( !hp ) {            fprintf(stderr,                    "%s: host '%s'\n",                    hstrerror(h_errno),                    argv[x]);            continue;         }          printf("Host %s : \n" ,argv[x]);         printf(" Officially:\t%s\n", hp->h_name);         fputs(" Aliases:\t",stdout);         for ( x2=0; hp->h_aliases[x2]; ++x2 ) {            if ( x2 ) {               fputs(", ",stdout);              }         fputs(hp->h_aliases[x2],stdout);         }              fputc('\n',stdout);         printf(" Type:\t\t%s\n",                hp->h_addrtype == AF_INET                ? "AF_INET" : "AF_INET6");         if ( hp->h_addrtype == AF_INET ) {            for ( x2=0; hp->h_addr_list[x2]; ++x2 ) {               printf(" Address:\t%s\n",                      inet_ntoa( *(struct in_addr *)                       hp->h_addr_list[x2]));            }         }      putchar('\n');      }      return 0;   } 

I want to if I check via $ file lookup will get output like this:

lookup: ELF 32-bit LSB executable, Intel 80386, version 1 (GNU/Linux), statically linked, for GNU/Linux 2.6.24, BuildID[sha1]=0x6fcb2684ad8e5e842036936abb50911cdde47c73, not stripped

Not like this:

lookup: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0xf9f18671751927bea80de676d207664abfdcf5dc, not stripped

If you commented with suggested I must use without static because different libc every linux I knew it, I hope you do not need to comment. Why do I persist in for static? Because there I need to do to mandatory use static, binary files must be static and not dynamic.

I have more than 2 weeks looking for this but so far have not succeeded.

Thanks for help me to resolve my heavy problem.

like image 604
Loren Ramly Avatar asked Mar 01 '13 19:03

Loren Ramly


1 Answers

What you are asking for is going to be very difficult.

See this StackOverflow question about getaddrinfo. Basically, underneath getaddrinfo/gethostbyname is glibc's NSS layer. This allows a sysadmin to say "use DNS for resolving hostnames to IP addresses", or "use LDAP", or "don't use anything other than /etc/hosts". This control is at runtime; the sysadmin can at any point change the way hostnames are resolved to IPs.

Because of this flexibility, all of the name-resolution calls in glibc use helper libraries (plugins, basically) to do the grunt work of resolution. There's one shared library for LDAP addressing, one for files, one for DNS, one for YP, and so on and so on.

If you want your program to be 100% statically linked, you're going to have to go elsewhere (NOT gethostbyname) to convert a hostname to an IP address. You could do this with a resolver library like uDNS (not this exact one - there are similar tools available), but you should keep in mind that your binary is not going to do the right thing on systems which are configured not to use DNS!

Instead, I would recommend just leaving the program (technically) dynamically linked. If you really want to make sure it will run on any platform, you could even ship glibc with the binary - although doing this would require LGPL conformance. Leaving this one dynamic link in place will only mean you won't work on systems with the wrong glibc version - not a huge compatibility issue.

Speaking of license compliance, it's worth noting that if you statically link glibc, you most likely have to ship the source code for your entire application to comply with glibc's LGPL license. I am not a lawyer, and this is not qualified legal advice, but reading the LGPL makes it very clear that applications statically linking glibc must be open-source. See this StackOverflow question on the topic.

like image 184
Borealid Avatar answered Sep 23 '22 00:09

Borealid