Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does __section( ) mean in linux kernel source

I see the following code in some OS kernel. But I don't understand the way __section is used, and don't know what does this code mean.

#define KEEP_PAGER(sym) \
    extern const unsigned long ____keep_pager_##sym; \
    const unsigned long ____keep_pager_##sym  \
        __section("__keep_meta_vars_pager") = (unsigned long)&sym
like image 738
shijun zhao Avatar asked Sep 05 '26 09:09

shijun zhao


1 Answers

It's specific linux kernel C macro definition wrapped around a GCC extension, specifying an atttribute to use for an object. It's a shorter way of writing the section attribute definition

Historically the linux kernel has been written specifically for building with the GCC compiler, and makes extensive use of low level extensions to do specific hardware operations and optimisations.

The section attribute specifically is used to determine the storage location of the object tagged with it. ELF binary format arranges the object file into named sections, and using the attribute like this allows the programmer to more precisely specify where the information for the tagged object will be placed in the target object

Over the years, there's been work put in to increasing the compatibility of these compiler extensions between different compilers, as well as making linux compilable with alternative compilers (if you look at the linux header file where the macro is defined you'll see that it is full of conditional directives for various compiler features). Macros like this can be a useful way to have a portable internal API for low level features across different compiler implementations.

Kernel and kernel driver C code is atypically concerned with direct specifics of physical hardware implementation, and needs to be explicit about the compiler binary output in a way that application level C code rarely will.

One example of why the linux kernel uses named sections is in the init handling - functions and data that are only used during bootup are grouped into one section of memory that can be easily released once startup is complete - you may be familiar with the boot message along the lines of 'freeing unused kernel memory:...' towards the end of the linux boot sequence

like image 76
cms Avatar answered Sep 07 '26 02:09

cms



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!