Usage of _ init ,__ exit and _ initdata in Linux kernel and driver development

Source: Internet
Author: User

To understand the segment information of Linux kernel code, you need to understand the encoding attribute or defined function or number of GCC's _ attribute, __attribute _ is mainly used to change the declared data features. It has many subitems used to change the characteristics of the target object. For example, for a function, noline will prohibit inline extension, noreturn indicates that no return value is returned, and pure indicates that the function will not affect the function outside of the return value (such as global variables and pointers. But here we are more interested in the function of the subitem section for the code segment.

The format of section subitem of _ attribute _ is as follows:

_ Attribute _ (Section ("section_name ")))

The function data is put into the input segment named "section_name.

Note the following two concepts: Input and Output segments.

The input and output segments are relative to the link process when the final Elf or binary file is to be generated. Most of the inputs in the Link process are the target files generated by source code encoding. o, then these. the segment contained in the o file is the input segment relative to the link process, while the link output is generally the executable file Elf or library, and these output files also contain segments, the segments in these output files are called output segments. The input and output segments are independent of each other. In the link process, the link program will follow certain rules (these rules actually come from the link script ), re-combine different input segments into different output segments. Even if the segment name is used, the input and output segments can be completely different.

The usage example is as follows:

Int VaR _ attribute _ (Section (". xdata") = 0;

The variable VAR defined in this way will be put in the name. xdata input segment (Note: _ attribute _ In this usage, the brackets seem to be very strict, and the parentheses here seem to be less than one .)

Static int _ attribute _ (Section (". xinit") functiona (void)

{

.....
}

In this example, the function functiona is placed in the input segment named. xinit.

Note that section attribute of __attribute _ only specifies the input segment of the object, which does not affect the segment of the object to be placed in the executable file.

2. Important macro definitions related to segments in Linux kernel source code

A. About _ init, _ initdata, _ exit, _ exitdata, and similar macros

Open the File Include/init. h In the Linux kernel source code tree. You can see the following macro discussion:

# DEFINE _ init _ attribute _ (_ Section _ (". init. Text") _ cold

# DEFINE _ initdata _ attribute _ (_ Section _ (". init. Data ")))

# DEFINE _ exitdata _ attribute _ (_ Section _ (". Exit. Data ")))

# DEFINE _ exit_call _ attribute_used _ attribute _ (_ Section _ (". exitcall. Exit ")))

# DEFINE _ init_refok oninline _ attribute _ (_ Section _ (". Text. init. refok ")))

# DEFINE _ initdata_refok _ attribute _ (_ Section _ (". Data. init. refok ")))

# DEFINE _ exit_refok noinline _ attribute _ (_ Section _ (". Exit. Text. refok ")))

.........

# Ifdef Module

# DEFINE _ exit _ attribute _ (_ Section _ (". Exit. Text") _ cold

# Else

# DEFINE _ exit _ attribute_used _ attribute _ (_ Section _ (". Exit. Text") _ cold

# Endif

For those who often write the driver module or read the kernel source code, see the familiar macros :__ init, _ initdata, _ exit, _ exitdata.

_ Init macro is the definition of the initialization function of the driver module. The purpose is to put the initialization function of the driver module into the input segment named. init. Text. After the kernel is started, the memory in this segment will be released for other use.

_ Initdata macro is used for data definition to put data into the input segment named. init. Data. The other macros are similar.

Note that _ Section _ is used to replace section in the above description. There are other similar macro definitions, which are not listed here. Their functions are similar.

 

Link: http://www.cnblogs.com/hanyan225/archive/2010/12/30/1922232.html

 

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.