madhavi chavan
madhavi chavan

Reputation: 11

pragma directives for gcc

I have a code compiled with ghs compiler in which the sections have been defined in c code as

#pragma ghs section data = ".shareddata"

// some c code

#pragma ghs section data = default 

how do we define pragmas for sections using gcc for the above thing

Upvotes: 1

Views: 10842

Answers (1)

Andrew Kephart
Andrew Kephart

Reputation: 31

In general, gcc discourages the use of pragmas, instead suggesting that you use attributes for both functions and variables.

From the GCC manual ("Declaring Attibutes of Functions"):

Normally, the compiler places the code it generates in the text section. Sometimes, however, you need additional sections, or you need certain particular functions to appear in special sections. The section attribute specifies that a function lives in a particular section. For example, the declaration:

extern void foobar (void) __attribute__ ((section ("bar")));

puts the function foobar in the bar section.

From "Specifying Attributes of Variables"

Normally, the compiler places the objects it generates in sections like data and bss. Sometimes, however, you need additional sections, or you need certain particular variables to appear in special sections, for example to map to special hardware. The section attribute specifies that a variable (or function) lives in a particular section. For example, this small program uses several specific section names:

    struct duart a __attribute__ ((section ("DUART_A"))) = { 0 };
    struct duart b __attribute__ ((section ("DUART_B"))) = { 0 };
    char stack[10000] __attribute__ ((section ("STACK"))) = { 0 };
    int init_data __attribute__ ((section ("INITDATA")));          
    main()
    {
        /* Initialize stack pointer */
        init_sp (stack + sizeof (stack));          
        /* Initialize initialized data */
        memcpy (&init_data, &data, &edata - &data);
        /*  Turn on the serial ports */
        init_duart (&a);
        init_duart (&b);
    }

Use the section attribute with global variables and not local variables, as shown in the example.

You may use the section attribute with initialized or uninitialized global variables but the linker requires each object be defined once, with the exception that uninitialized variables tentatively go in the common (or bss) section and can be multiply “defined”. Using the section attribute changes what section the variable goes into and may cause the linker to issue an error if an uninitialized variable has multiple definitions. You can force a variable to be initialized with the -fno-common flag or the nocommon attribute. Some file formats do not support arbitrary sections so the section attribute is not available on all platforms. If you need to map the entire contents of a module to a particular section, consider using the facilities of the linker instead.

Upvotes: 3

Related Questions