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

28,982

Solution 1

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")

Solution 2

#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.

Share:
28,982
rashok
Author by

rashok

A Passionate Developer

Updated on February 06, 2021

Comments

  • rashok
    rashok over 3 years

    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")
    
  • Lundin
    Lundin about 12 years
    There is no such thing as "the syntax for this in C/C++ is..."! DATA_SECTION and CODE_SECTION are completely compiler-specific and not supported by any standard at all. What will happen is up to the compiler. I know several compilers that support CODE_SECTION and DATA_SECTION but with a completely different syntax.
  • user247702
    user247702 about 12 years
    If I change is to could be, will you remove the downvote then? Of course #pragma is compiler specific.
  • Lundin
    Lundin about 12 years
    Yes, I suppose that is better. The important thing is not to trick anyone into thinking that this is well-defined by some standard. Though of course the original question is lacking, as there was no specific compiler mentioned.
  • rashok
    rashok about 12 years
    Whether #pragama CODE_SECTION is same as #pragma code_seg which is supported in vc. msdn.microsoft.com/en-us/library/s20kdbse(v=vs.71).aspx
  • Lundin
    Lundin about 12 years
    @rajaashok Was that a question or a statement? Any actual reason for downvote, or did you just feel like it?