GNU libc.so如何既是共享对象又是独立可执行文件?

时间:2021-11-10 05:38:43

In Linux, the shared library for the GNU standard C library (libc.so) is not only a shared library, but can also be run as a standalone executable, which prints out version information:

在Linux中,GNU标准C库(libc.so)的共享库不仅是一个共享库,还可以作为独立的可执行文件运行,它可以打印出版本信息:

[me@computer ~]$ /lib/libc.so.6 

GNU C Library stable release version 2.12, by Roland McGrath et al.
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 4.4.7 20120313 (Red Hat 4.4.7-11).
Compiled on a Linux 2.6.32 system on 2015-01-07.
Available extensions:
    The C stubs add-on version 2.1.2.
    crypt add-on version 2.1 by Michael Glad and others
    GNU Libidn by Simon Josefsson
    Native POSIX Threads Library by Ulrich Drepper et al
    BIND-8.2.3-T5B
    RT using linux kernel aio
libc ABIs: UNIQUE IFUNC
For bug reporting instructions, please see:
<http://www.gnu.org/software/libc/bugs.html>.

How did they do this? I tried creating a shared library that also happened to have a main() function, but it segfaulted when I tried to run it.

他们是怎么做到的?我尝试创建一个也碰巧有一个main()函数的共享库,但是当我尝试运行它时它就被分割了。

What I tried:

我尝试了什么:

/* myso.c */
#include <stdlib.h>
#include <stdio.h>

static void foo(void) {
    printf("hello foo\n");
}

int main(int argc, char *argv[]) {
    printf("hello world\n");
    foo();
    return 0;
}

Then:

$ gcc -Wall -fPIC -shared -o myso.so myso.c
$ ./myso.so
Segmentation fault (core dumped)

As a note: I don't really want to do this for any real purpose, I just want to know how the GNU guys (and gals) did it.

作为一个注释:我真的不想为任何真正的目的这样做,我只是想知道GNU人(和gals)是如何做到的。

1 个解决方案

#1


4  

In file Makerules of glibc:

在glibc文件Makerules中:

# Give libc.so an entry point and make it directly runnable itself.
LDFLAGS-c.so += -e __libc_main

And in csu/version.c

在csu / version.c中

extern void __libc_print_version (void);
void
__libc_print_version (void)
{
  __write (STDOUT_FILENO, banner, sizeof banner - 1);
}

This is called by __libc_main().

这由__libc_main()调用。

In file elf/interp.c a program interpreter is added to the shared object.

在文件elf / interp.c中,程序解释器被添加到共享对象中。

This is working for me on 64 bit Linux: gcc -Wall -Wextra -fPIC -shared -o myso.so myso.c

这在64位Linux上对我有用:gcc -Wall -Wextra -fPIC -shared -o myso.so myso.c

//myso.c
#include <unistd.h>

char const __invoke_dynamic_linker__[] __attribute__ ((section (".interp")))
#ifdef __LP64__
  = "/lib64/ld-linux-x86-64.so.2";
#else
  = "/lib/ld-linux.so.2";
#endif

void _start(void)
{
    static char const msg[] = "Hello world!\n";

    write(STDOUT_FILENO, msg, sizeof msg - 1);          
    _exit(0);
}

The __invoke_dynamic_linker__[] line is needed because without it dynamic linking of write and _exit can't be done. The name of this variable is not important. There is an option for ld, -dynamic-linker, which would do the same, but it is ignored with -shared, but putting the line into the source code works.

需要__invoke_dynamic_linker __ []行,因为如果没有它,则无法完成write和_exit的动态链接。此变量的名称并不重要。 ld,-dynamic-linker有一个选项可以做同样的事情,但是-shared会忽略它,但是把这一行放到源代码中就行了。

Here is a variant without dynamic linking:

这是一个没有动态链接的变体:

#include <unistd.h>
#include <sys/syscall.h>

__asm__(
#ifdef __LP64__
    "syscall: mov    %rdi,%rax;"
    "   mov    %rsi,%rdi;"
    "   mov    %rdx,%rsi;"
    "   mov    %rcx,%rdx;"
    "   mov    %r8,%r10;"
    "   mov    %r9,%r8;"
    "   mov    0x8(%rsp),%r9;"
    "   syscall;"
    "   cmp    $0xfffffffffffff001,%rax;"
    "   jae    __syscall_error;"
    "   retq;   "
    "__syscall_error: neg    %rax;"
    "   mov    %eax,%fs:0xffffffffffffffd0;"
    "   or     $0xffffffffffffffff,%rax;"
    "   retq;"   
#else
    "syscall:   push   %ebp;"
    "   push   %edi;"
    "   push   %esi;"
    "   push   %ebx;"
    "   mov    0x2c(%esp),%ebp;"
    "   mov    0x28(%esp),%edi;"
    "   mov    0x24(%esp),%esi;"
    "   mov    0x20(%esp),%edx;"
    "   mov    0x1c(%esp),%ecx;"
    "   mov    0x18(%esp),%ebx;"
    "   mov    0x14(%esp),%eax;"
    "   int    $0x80;"
    "   pop    %ebx;"
    "   pop    %esi;"
    "   pop    %edi;"
    "   pop    %ebp;"
    "   cmp    $0xfffff001,%eax;"
    "   jae    __syscall_error;"
    "   ret;"    
    "__syscall_error:   neg    %eax;"
    "   mov    %gs:0x0,%ecx;"
    "   mov    %eax,-0x18(%ecx);"
    "   mov    $0xffffffff,%eax;"
    "   ret;"    
#endif
);

void _start(void)
{
    static char const msg[] = "Hello world!\n";

    syscall(SYS_write, STDOUT_FILENO, msg, sizeof msg - 1);
    syscall(SYS_exit, 0);
}

#1


4  

In file Makerules of glibc:

在glibc文件Makerules中:

# Give libc.so an entry point and make it directly runnable itself.
LDFLAGS-c.so += -e __libc_main

And in csu/version.c

在csu / version.c中

extern void __libc_print_version (void);
void
__libc_print_version (void)
{
  __write (STDOUT_FILENO, banner, sizeof banner - 1);
}

This is called by __libc_main().

这由__libc_main()调用。

In file elf/interp.c a program interpreter is added to the shared object.

在文件elf / interp.c中,程序解释器被添加到共享对象中。

This is working for me on 64 bit Linux: gcc -Wall -Wextra -fPIC -shared -o myso.so myso.c

这在64位Linux上对我有用:gcc -Wall -Wextra -fPIC -shared -o myso.so myso.c

//myso.c
#include <unistd.h>

char const __invoke_dynamic_linker__[] __attribute__ ((section (".interp")))
#ifdef __LP64__
  = "/lib64/ld-linux-x86-64.so.2";
#else
  = "/lib/ld-linux.so.2";
#endif

void _start(void)
{
    static char const msg[] = "Hello world!\n";

    write(STDOUT_FILENO, msg, sizeof msg - 1);          
    _exit(0);
}

The __invoke_dynamic_linker__[] line is needed because without it dynamic linking of write and _exit can't be done. The name of this variable is not important. There is an option for ld, -dynamic-linker, which would do the same, but it is ignored with -shared, but putting the line into the source code works.

需要__invoke_dynamic_linker __ []行,因为如果没有它,则无法完成write和_exit的动态链接。此变量的名称并不重要。 ld,-dynamic-linker有一个选项可以做同样的事情,但是-shared会忽略它,但是把这一行放到源代码中就行了。

Here is a variant without dynamic linking:

这是一个没有动态链接的变体:

#include <unistd.h>
#include <sys/syscall.h>

__asm__(
#ifdef __LP64__
    "syscall: mov    %rdi,%rax;"
    "   mov    %rsi,%rdi;"
    "   mov    %rdx,%rsi;"
    "   mov    %rcx,%rdx;"
    "   mov    %r8,%r10;"
    "   mov    %r9,%r8;"
    "   mov    0x8(%rsp),%r9;"
    "   syscall;"
    "   cmp    $0xfffffffffffff001,%rax;"
    "   jae    __syscall_error;"
    "   retq;   "
    "__syscall_error: neg    %rax;"
    "   mov    %eax,%fs:0xffffffffffffffd0;"
    "   or     $0xffffffffffffffff,%rax;"
    "   retq;"   
#else
    "syscall:   push   %ebp;"
    "   push   %edi;"
    "   push   %esi;"
    "   push   %ebx;"
    "   mov    0x2c(%esp),%ebp;"
    "   mov    0x28(%esp),%edi;"
    "   mov    0x24(%esp),%esi;"
    "   mov    0x20(%esp),%edx;"
    "   mov    0x1c(%esp),%ecx;"
    "   mov    0x18(%esp),%ebx;"
    "   mov    0x14(%esp),%eax;"
    "   int    $0x80;"
    "   pop    %ebx;"
    "   pop    %esi;"
    "   pop    %edi;"
    "   pop    %ebp;"
    "   cmp    $0xfffff001,%eax;"
    "   jae    __syscall_error;"
    "   ret;"    
    "__syscall_error:   neg    %eax;"
    "   mov    %gs:0x0,%ecx;"
    "   mov    %eax,-0x18(%ecx);"
    "   mov    $0xffffffff,%eax;"
    "   ret;"    
#endif
);

void _start(void)
{
    static char const msg[] = "Hello world!\n";

    syscall(SYS_write, STDOUT_FILENO, msg, sizeof msg - 1);
    syscall(SYS_exit, 0);
}