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
}
}