Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__attribute__((section("name"))) usage?

Tags:

c

gcc

linker

I have ran through code that use _ attribute _((section("name")). I understand that for gcc compiler this allows you to tell the linker to put the object created at a specific section "name" (with "name" absolute address declared in a linker file).

What is the point of doing this instead of just using the .data section?

like image 238
tll Avatar asked Aug 21 '13 00:08

tll


People also ask

What does__ attribute__ mean in c?

The __attribute__ directive is used to decorate a code declaration in C, C++ and Objective-C programming languages. This gives the declared code additional attributes that would help the compiler incorporate optimizations or elicit useful warnings to the consumer of that code.

What is a section attribute?

The section attribute specifies that a variable (or function) lives in a particular section.

What is attribute in GCC?

GCC Attributes are used to add various annotations to code outside of the basic source language that assist the compiler. There is also a user attribute that is not interpreted by the compiler but can be used by plugins and static analyses.


1 Answers

There are many possible uses. [Edit to add note: this is just a sample of uses I've seen myself or considered, not a complete list.]

The Linux kernel, for instance, marks some code and data sections as used only during kernel bootstrap. These can be jettisoned after the kernel is running, reclaiming the space for other uses.

You can use this to mark code or data values that need patching on a particular processor variant, e.g., with or without a coprocessor.

You can use it to make things live in "special" address spaces that will be burned to PROM or saved on an EEPROM, rather than in ordinary memory.

You can use it to collect together code or data areas for purposes like initialization and cleanup, as with C++ constructors and destructors that run before the program starts and when it ends, or for using shorter addressing modes (I don't know how much that would apply on ARM as I have not written any ARM code myself).

The actual use depends on the linker script(s).

like image 86
torek Avatar answered Nov 01 '22 08:11

torek