Why can't I directly run the shared library on Linux?

$ chmod +x libsomelibrary.so
$ ./libsomelibrary.so
Segmentation fault

$ gcc -O2 http://vi-server.org/vi/bin/rundll.c -ldl -o rundll
$ ./rundll ./libsomelibrary.so main
(application starts normally)

Why can't I start libsomelibrary.so if it has a convenient entry point?

rundll.c trivially:

void* d = dlopen(argv[1], RTLD_LAZY);
void* m = dlsym(d, argv[2]);
return ((int(*)(int,char**,char**))m)(argc-2, argv+2, envp);

Why is it not used internally when trying to download a binary file?

+3
source share
3 answers

main It is not an entry point recognized by the kernel or the dynamic linker - it is called by the startup code associated with your executable file when it is compiled (such a startup code is not connected to shared libraries by default).

The ELF header contains the start address.

+4
source

. . , . rundll. , , , rundll . , , , rundll.

+3

Linux.

, /lib/libc.so.6, :

$ /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.5.0 20100520 (prerelease).
Compiled on a Linux 2.6.34 system on 2010-05-29.
Available extensions:
        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
libc ABIs: UNIQUE IFUNC
For bug reporting instructions, please see:
<http://www.gnu.org/software/libc/bugs.html>.

- .

+3
source

Source: https://habr.com/ru/post/1767985/


All Articles