Question

I'd like to define (and initialize) a number of instances of a struct across a number of *.c files, but I want them to gather at compile time into a single contiguous array. I've been looking into using a custom section and using the section's start and end address as the start and end of the array of structs, but I haven't quite figured out the details yet, and I'd rather not write a custom linker script if I can get away with it. Here's a summary of my first hack which didn't quite work:

// mystruct.h:
typedef struct { int a; int b; } mystruct;

// mycode1.c:
#include "mystruct.h"
mystruct instance1 = { 1, 2 } __attribute__((section(".mysection")));

// mycode2.c:
#include "mystruct.h"
mystruct instance2 = { 3, 4 } __attribute__((section(".mysection")));

// mystruct.c:
extern char __mysection_start;
extern char __mysection_end;
void myfunc(void) {
    mystruct * p = &__mysection_start;
    for ( ; p < &__mysection_end ; p++) {
        // do stuff using p->a and p->b
    }
}
Was it helpful?

Solution

In order to use a custom section, you must define its start address in a custom linker script. Copy your device's linker script and add the new section into its SECTIONS block:

/* in custom.gld */
mysection 0x2000 :
{
    *(mysection);
} >data

To make your objects go into this section, use the section attribute:

/* mycode1.c: */
#include "mystruct.h"
mystruct __attribute__((section("mysection"))) instance1 = { 1, 2 };

/* mycode2.c: */
#include "mystruct.h"
mystruct __attribute__((section("mysection"))) instance2 = { 3, 4 };

Now, to get the boundaries of your custom section, you can use the .startof.(section_name) and .sizeof.(section_name) assembler operators:

#include "mystruct.h"

char *mysection_start;
char *mysection_end;
size_t mysection_size;

int main(void)
{
    asm("mov #.startof.(mysection), W12");
    asm("mov #.sizeof.(mysection), W13");
    asm("mov W12, _mysection_start");
    asm("mov W13, _mysection_size");

    mysection_end = mysection_start + mysection_size;

    mystruct *p = (mystruct *)mysection_start;
    for ( ; (char *)p < mysection_end ; p++)
    {
        // do stuff using p->a and p->b
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top