Executable file formats of Linux kernel Research Series

Source: Internet
Author: User
The executable file format of the Linux kernel Research Series-general Linux technology-Linux programming and kernel information. The following is a detailed description. We know that not all binary files in linux have the same format. linux uses the binary file processing program to process different binary files separately. The binary processing program identifies a file by embedding a "feature sequence" (a special byte sequence) at the beginning of the file, and sometimes uses some features of the file name, for example, the ELF file starts with 'e' l' F' and the java file starts with 0xcafebabe.
Linux uses sys_execve to mount executable binary files.
The current linux version (2.2) provides the following binary file handlers:
L a. out: it is mainly used for compatibility with the past, because a. out is difficult to implement dynamic links to be obtained by ELF.
For more information, see
L ELF: Mainstream linux binary files. However, she needs to be the same as other formats.
Use a binary processing program. The related content is in
L EM86: runs Intel linux binary files on the Alpha host, as if they are Alpha local binary files. Related content
L Java: the handler returns the interpreted program that executes bytecode by taking the file name of the. class file as a parameter. For more information, see
L Misc: the most sensible binary processing method. She uses embedded feature numbers or filename suffixes to identify binary formats. In addition, she can configure it at runtime instead of during compilation. In this way, you can add support for new binary format files without re-compiling the kernel. Some say it will replace Java and EM86 binary processors with her. For more information, see
L Script: mainly supports shell scripts and Perl scripts. To put it bluntly, all the first two characters are #! The executable files are all processed by her. For more information, see
Before proceeding, we must first understand a Data Structure linux_binfmt;

Struct linux_binfmt {
Struct linux_binfmt * next;
Long * use_count;
Int (* load_binary) (struct linux_binprm *, struct pt_regs * regs );
Int (* load_shlib) (int fd );
Int (* core_dump) (long signr, struct pt_regs * regs );
};
Linux_binfmt contains two important pointer to the function. load_binary loads executable code and load_shlib loads the shared library. Core_dump is a pointer to the dump function.
Apparently, a chain table is composed of next, and the header is directed by formats, for example:
Static struct linux_binfmt * formats = (struct linux_binfmt *) NULL;
The system defines an object for each different file format:
Static struct linux_binfmt elf_format;
Static struct linux_binfmt java_format;
Static struct linux_binfmt em86_format;
......
The linked list of linux_binfmt (directed by formats) is in different file formats.
A linked list composed of linux_binfmt. The handler for binary files of different formats is implemented by registering the function in the corresponding linux_binfmt. In reality, the object-oriented idea has been used here, this situation is often seen in the linux kernel source code.
For example, for the ELF File Format:
When the "pointer to the loaded function" is used, the Pointer Points to the loaded function as follows:
Load_elf_binary (struct linux_binprm * bprm, struct pt_regs * regs );
Static int load_elf_library (int fd );
The above functions are defined and implemented in.
The corresponding elf_format is defined:
Static struct linux_bmfmt elf_format = {
# Ifndef MODULE
NULL, NULL, load_elf_binary, load_elf_library, elf_core_dump
# Else
NULL, & mod_use_count, load_elf_binary, load_elf_library,
Elf_core_dump ()
# Endif
};

For reference, we can naturally think of other file format definitions.
"NOTE" the load function of Java is load_java, the load function of em86 is load_em86, and the load function of script is load_script. They do not provide the load function of library.

Let's take a look at the main operations on these data structures:
L register_binfmt: Mainly used to add a binary file to the formats linked list.
Extern int register_binfmt (struct linux_binfmt *);
L unregister_binfmt: executes the inverse operation with register_binfmt.
Extern int unregister_binfmt (struct linux_binfmt *);
L init: provides a set of init operations for each binary file format system, as follows:
Extern int init_elf_binfmt (void );
Extern int init_aout_binfmt (void );
Extern int init_script_binfmt (void );
Extern int init_java_binfmt (void );
Extern int init_em86_binfmt (void );
Extern int init_misc_binfmt (void );
The above functions are declared in;
The init function is very simple. It only calls the register_binfmt function to add the file format to the formats linked list.
L load function: execute binary file calling. The actual work is generally completed by a corresponding do_load function, such as do_load_elf_binary, do_load_elf_library, do_load_java ........
L extern int prepare_binprm (struct linux_binprm *);
Extern int search_binary_handler (struct linux_binprm *, struct pt_regs *);
These two functions mainly involve another data structure, linux_binprm. Then, let's talk about it next time. Here we should have a general impression on several binary file handlers in linux, go to the source code for details.
Okay, this time it's time to take a rest.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.