gcc

Creating Named sections per variable?


suppose I have variables:

int global a = 1;

int banana b = 2;

int mango c = 3;

I want GCC to generate them such that:

.global
a .long 1
.banana 
b .long 2
.mango
c .long 3

What's the easiest way to do that?

Updates:

Got:

 __attribute__ ((section ("mmm"))) int a = 432;`

along with

target_asm_named_section()` 

to generate:


    .global
    a: 
       .long 1

Which is great but there are two problems.

One is that unless the lists are ordered for different sections, you'll get repeat sections.

so


     __attribute__ ((section ("mmm"))) int a = 432;
     __attribute__ ((section ("mmm"))) int b = 432;
     __attribute__ ((section ("global"))) int c = 432;

is good, but


     __attribute__ ((section ("mmm"))) int a = 432;
     __attribute__ ((section ("global"))) int c = 432;
     __attribute__ ((section ("mmm"))) int b = 432;

is bad, because .mmm will appear twice.

The second problem is that I'm already using attributes to do

`__attribute__((global))`

which, to the best of my attempts cannot be combined with the previous attribute.

Is there any easy way to resolve either of those two issues?


Solution

  • Solution: First use:

    #define MMR __attribute__((section ("section mmr")))

    Then, inside the function defining

    #define TARGET_ENCODE_SECTION_INFO
    navigate to the string via:

        #define ATTRIBUTES(decl) \
          (TYPE_P (decl)) ? TYPE_ATTRIBUTES (decl) \
                        : DECL_ATTRIBUTES (decl) \
                          ? (DECL_ATTRIBUTES (decl)) \
                  : TYPE_ATTRIBUTES (TREE_TYPE (decl))
      tree attr = ATTRIBUTES(decl);
      char* section_name = TREE_STRING_POINTER( TREE_VALUE( TREE_VALUE(attr)));
    

    And viola, the section_name is the phrase you created inside section(""). Then match it to what you want it to do.

    I use flags for example:

      if(strcmp(section_name, "apple") == 0)
      {
         flags |= SYMBOL_FLAG_APPLE;
      }
    

    The flag being set was the goal of the original _attribute_, and now that it can be done using the section attribute, both goals are achieved with the use of one attribute