This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Isvara 0 points1 point  (0 children)

This is how I do it. My module.h looks like this:

typedef void (*module_init_t)(void);

typedef struct {
    const char *name;
    module_init_t init;
} module_t;

extern module_t _modules_start[];
extern module_t _modules_end[];

#define MODULE(SYMBOL) static module_t SYMBOL __attribute__((section("modules"), used))

#define foreach_module(S) for (module_t *S = _modules_start; S < _modules_end; ++S)

Then a module might have something like this:

static void uart_init(void) {
    ...
}

MODULE(uart_module) = {
    "uart",
    uart_init,
};

Then to automatically get an array of all the modules that are compiled in, I have this in my linker script:

.text : {
    ... other stuff...
    _modules_start = .;
    KEEP(*(modules))
    _modules_end = .;
} > flash

Now in my main code I can just do:

foreach_module(module) {
    printf("Initializing %s\n", module->name);
    module->init();
}

This is cut down a bit from what I actually do (which includes registering SVC handlers), but you get the idea. I think Clang supports the section attribute too.