Five EmbedDev logo Five EmbedDev

An Embedded RISC-V Blog

There were a few bugs in the code examples that have been fixed.

The loop in startup.c function _startup() that calling each entry from __init_array_start to __init_array_end was incorrect in Startup Code in C and Startup Code in C++. It has been fixed and tested.

To test it the main.c routine was updated to demonstrate the operation of the contructors called in _startup() before main() is called.

The corrected C code is below:

extern function_t __init_array_start;
extern function_t __init_array_end;

...

void _start(void) {

   ...

    for (const function_t* entry=&__init_array_start; 
         entry < &__init_array_end;
         ++entry) {
        (*entry)();
    }

Some test code was added to main.c.

// Machine mode interrupt service routine
static void setup_global2(void) __attribute__ ((constructor(102)));
static void setup_global1(void) __attribute__ ((constructor(101)));

static unsigned int global_value1_with_constructor = 1;
static unsigned int global_value2_with_constructor = 2;

void setup_global2(void) {
    global_value1_with_constructor |= 0x200;
    global_value2_with_constructor |= 0x200;
}

void setup_global1(void) {
    global_value1_with_constructor |= 0x100000;
    global_value2_with_constructor |= 0x100000;
}

A trace shows both variables written before main is reached.

Startup Trace