c - Gather variables from multiple files into a single contiguous block of memory at compile time -


i'd define (and initialize) number of instances of struct across number of *.c files, want them gather @ compile time single contiguous array. i've been looking using custom section , using section's start , end address start , end of array of structs, haven't quite figured out details yet, , i'd rather not write custom linker script if can away it. here's summary of first hack 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;     ( ; p < &__mysection_end ; p++) {         // stuff using p->a , p->b     } } 

in order use custom section, must define start address in custom linker script. copy device's linker script , add new section sections block:

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

to make objects go section, use 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, boundaries of custom section, can use .startof.(section_name) , .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;     ( ; (char *)p < mysection_end ; p++)     {         // stuff using p->a , p->b     } } 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -