Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of pragma code section and data section?

What exactly will happen to the data segment and text segment if I use the below two lines in my c source code file?

#pragma CODE_SECTION(func1, "Sec1")
#pragma DATA_SECTION(globalvar1, "Sec2")
like image 449
rashok Avatar asked Mar 15 '12 09:03

rashok


People also ask

What is pragma section?

#pragma section defines a section class, and optionally, one or two sections in the class. A section class controls the addressing and accessibility of variables and code placed in an instance of the class.

What is pragma used for?

A pragma is a compiler directive that allows you to provide additional information to the compiler. This information can change compilation details that are not otherwise under your control. For example, the pack pragma affects the layout of data within a structure. Compiler pragmas are also called directives.

Is #pragma a macro?

The #pragma directive isn't usable in a macro definition, because the compiler interprets the number sign character ('#') in the directive as the stringizing operator (#).


2 Answers

Source (contains examples): https://web.archive.org/web/20080803190119/http://hi.baidu.com/jevidyang/blog/item/6d4dc436d87e3a300b55a918.html

Note: #pragma is compiler specific, so syntax may vary for your compiler.

The DATA_SECTION pragma allocates space for the symbol in a section called section name. The syntax for the pragma in C could be:

#pragma DATA_SECTION (symbol, "section name");

The syntax for the pragma in C++ could be:

#pragma DATA_SECTION ("section name");

The DATA_SECTION pragma is useful if you have data objects that you want to link into an area separate from the .bss section.


The CODE_SECTION pragma allocates space for the func in a section named section name. The CODE_SECTION pragma is useful if you have code objects that you want to link into an area separate from the .text section. The syntax of the pragma in C could be:

#pragma CODE_SECTION (func, "section name")

The syntax of the pragma in C++ could be:

#pragma CODE_SECTION ("section name")
like image 90
user247702 Avatar answered Oct 29 '22 22:10

user247702


#pragma means "here follows something implementation-defined unique to this compiler". So what will happen depends on the compiler you are using. If the compiler doesn't support this specific pragma, the whole thing will be ignored.


In this case it is fairly obvious, however.

#pragma CODE_SECTION(func1, "Sec1") means: "func1 should be in program memory, in the memory area called Sec1". Sec1 will be a read-only memory location where the actual code of func1 will be allocated.

#pragma DATA_SECTION(globalvar1, "Sec2") means: "globalvar1 should be in data memory, in the memory area called Sec2". Sec2 will be a read/write location where the variable globalvar1 will be allocated.

like image 21
Lundin Avatar answered Oct 29 '22 21:10

Lundin