How execve calls the dynamic linker / loader (ld-linux.so.2)

I used gcc to compile and reference the most basic C program, test.c:

int main() { } 

As expected, the output is a dynamically linked executable file:

 $ file test test: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.26, BuildID[sha1]=0x0f806c099f74132a158d98aebde4639ae0998971, not stripped 

Running strace gives the following result:

 $ strace -f ./test execve("./test", ["./test"], [/* 31 vars */]) = 0 brk(0) = 0x248d000 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory) mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f77eeb27000 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory) open("/etc/ld.so.cache", O_RDONLY) = 3 fstat(3, {st_mode=S_IFREG|0644, st_size=109292, ...}) = 0 mmap(NULL, 109292, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f77eeb0c000 close(3) = 0 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory) open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY) = 3 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\300\357\1\0\0\0\0\0"..., 832) = 832 fstat(3, {st_mode=S_IFREG|0755, st_size=1595408, ...}) = 0 mmap(NULL, 3709016, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f77ee580000 mprotect(0x7f77ee700000, 2097152, PROT_NONE) = 0 mmap(0x7f77ee900000, 20480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x180000) = 0x7f77ee900000 mmap(0x7f77ee905000, 18520, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f77ee905000 close(3) = 0 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f77eeb0b000 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f77eeb0a000 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f77eeb09000 arch_prctl(ARCH_SET_FS, 0x7f77eeb0a700) = 0 mprotect(0x7f77ee900000, 16384, PROT_READ) = 0 mprotect(0x7f77eeb29000, 4096, PROT_READ) = 0 munmap(0x7f77eeb0c000, 109292) = 0 exit_group(-292524312) = ? 

I expected to see "/lib64/ld-linux.so.2" somewhere in the output of strace, since according to the execve manual:

If the executable is a dynamically linked ELF executable, the interpreter named in the PT_INTERP segment is used to load the required shared libraries. This interpreter is usually / lib / ld-linux.so.2 for binaries related to glibc 2

I assume that the linker / loader call (/lib64/ld-linux.so.2) is part of execve. Can someone confirm?

+5
source share
1 answer

I assume that calling linker / loader (/lib64/ld-linux.so.2) is part of execve.

This is somewhat correct.

The kernel first considers the main executable segments and mmap them into a new "process shell".

When it is discovered that the executable file has a PT_INTERP segment, it mmap also executes file segments and transfers control to it.

Thus, after "returning" from execve() to user mode, the interpreter (usually /lib64/ld-linux-x86-64.so.2 on Linux / x86_64) is already displayed and working. Then the task of this interpreter should move to the remaining necessary shared libraries in mmap , initialize them and, finally, transfer control to the main executable file.

If you want more information, run here .

+9
source

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


All Articles