Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the loading mechanism of Linux application?

2025-03-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "what is the loading mechanism of Linux application". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "what is the loading mechanism of Linux application" can help you solve the problem.

1. Behavior of the parent process: copy, wait

There are many ways to execute an application, and it is common to execute from shell. Interactive shell is a process (all processes are obtained by the init process fork with pid number 1, and we'll talk about this topic in an article about Linux startup and initialization, as well as idle processes, etc.). When users type in the. / test execution program in shell, fork () first fork a child process (this is also the child shell in many articles), and the child process wait () ends, so when the test execution ends Back to shell waiting for user input (if you are creating a so-called background process, shell will not wait for the child process to finish, but will continue to execute). So the main job of the shell process is to copy a new process and wait for it to finish.

two。 Behavior of child processes: "execute" the application

2.1 execve ()

On the other hand, execve () is called in the child process to load the test and start execution. This is the key to the execution of test, so let's analyze it in detail. What is execve ()?

Execve () is a very important system call provided by the operating system, which is called exec () system call in many articles (note that it is different from the shell internal exec command). In fact, there is no system call exec () in Linux. Exec is just used to describe a set of functions, which all start with exec. They are:

# include int execl (const char * path, const char * arg,...); int execlp (const char * file, const char * arg,...); int execle (const char * path, const char * arg,..., char * const envp []); int execv (const char * path, char * const argv []); int execvp (const char * file, char * const argv []); int execve (const char * path, char * const argv [], char * const envp [])

These are all packaged library functions in libc, which are implemented through the system call execve () (# define _ _ NR_evecve 11, system call number 11).

The role of the exec function is to execute the executable file in the current process, that is, to find the executable file according to the specified file name and use it to replace the contents of the current process, and this replacement is irreversible, that is, the replaced content is no longer saved, when the executable file ends, the whole process is frozen. Because the code snippets, data segments and stacks of the current process have been replaced by new content, the functions of the exec function family will not return after successful execution, but will return-1 if they fail. The executable file can be either a binary file or an executable script file, which is slightly different when loading. Here we mainly analyze the operation of the binary file.

2.2 do_execve ()

When execve () is called in user mode, after causing a system interruption, the corresponding function executed in kernel state is do_sys_execve (), while do_sys_execve () calls the do_execve () function. Do_execve () first reads the executable and reports an error if the executable does not exist. Then check the permissions of the executable file.

If the file is not executable by the current user, execve () returns-1, reporting an error for permission denied. Otherwise, continue to read in the information needed to run the executable (see struct linux_binprm).

Execve ()-> do_sys_execve ()-> do_execve () (check if file exist and if can be runed by current user)

2.3 search_binary_handler ()

Then the system calls search_binary_handler (), according to the type of executable file (such as shell,a.out,ELF, etc.), find the corresponding processing function (the system creates a struct linux_binfmt for each file type, and strings it on a linked list, and traverses the linked list during execution to find the corresponding type of structure. If you want to define a kind of

To execute the file format, you also need to implement such a handler). Then execute the corresponding load_binary () function to start loading the executable.

2.4 load_elf_binary ()

The handler that loads the elf type file is load_elf_binary (), which first reads into the header of the ELF file and reads in all kinds of data (header information) according to the header information of the ELF file. Scan the program segment description table again, find the segment of type PT_LOAD, and map it (elf_map ()) to the fixed address of memory. If there is no description segment of the dynamic linker, set the returned entry address to the application entry. This is done by start_thread (), which does not start a thread, but is used to modify the value of registers such as PC saved in pt_regs to point to the entry of the loaded application. So when the kernel operation ends and returns to the user state, the next thing to execute is the application.

2.5 load_elf_interp ()

If the dynamic link library is used in the application, it is not so simple. In addition to loading the specified executable file, the kernel also gives control to the dynamic connector (program interpreter,ld.so in linux) to handle the dynamically linked program. The kernel searches the segment table, finds the name of the dynamic connector corresponding to the segment marked PT_INTERP, loads its image using load_elf_interp (), and sets the returned entry address to the return value of load_elf_interp (), the dynamic linker entry. The dynamic linker continues to run when execve exits. Dynamic connectors check the application's dependence on shared connection libraries, load them when needed, and relocate external references to the program. The dynamic connector then gives control to the application and executes from the program entry point defined in the header of the ELF file (a file format that we'll explain separately). (for example, the function foo () in userlib.so is used in test.c. When compiling, this information is put into the ELF file test, and the corresponding statement becomes call fakefoo (). When loading test, knowing that foo () is an external call, turn to the dynamic linker, load userlib.so, parse the address of the foo () function, and then redirect fakefoo () to foo (), so call foo () succeeds.)

That's all for "what is the loading mechanism of Linux applications?" Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report