Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
13 years ago

Set function section

how do i put a function or a code file in a specific memory section?

other than .text...

tnx...

3 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Add (IIRC) __attribute__((section("section_name"))) to the definition,

    either at the end of a declaration, or (I think) before the function name itself.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    The attribute directive must be added after the function declaration.

    For example you declare:

    int myFunction (void) __attribute__ ((section (".onchip_mem_code")));

    Then the function definition without any attribute:

    int myFunction (void)

    {

    // function body

    }

    Similarly for data, except here you must specify attribute when you define the variable, not when you declare it:

    int myVariable __attribute__ ((section (".onchip_mem_data")));

    For external referencing the variable, don't use any attribute:

    extern int myVariable;
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    You can also put the section attribute before the function name, eg:

    static __attribute__((section(".text.foo"))) int
    foo(void)
    {
        /* function body */
    }

    (It can go either side of the return type, not sure which is preferred.)

    For non-static functions this rather depends on whether you want the section info in the .c file or the .h one.

    For data, the section in an 'extern' is used to determine whether to use 'small data' (%gp relative) addressing (.sdata or .sbss). The location of the data itself depends on the actual definition.