My question could looks weird, indeed, here's the context:
I am currently facing a strange issue, while switching - on the project i'm working on - the core from pulpino to CV32 (some other changes have happend too, for instance about the crt0, like some dataram reset).
Here's a (real) example of what's happening with quite a simple main (i cannot give the startup/crt0 files edit: I partially gave it later in the post).
#include <string.h>
#include <inttypes.h>
#include <stdio.h>
typedef struct
{
uintptr_t addr;
uint32_t foo;
} some_struct_t;
static uint32_t text_in_data[8] = {0x11111111, 0x22222222, 0x33333333, 0x44444444, 0x55555555, 0x66666666, 0x77777777, 0x88888888};
uint32_t text_in_data2[8] = {0x11111111, 0x22222222, 0x33333333, 0x44444444, 0x55555555, 0x66666666, 0x77777777, 0x88888888};
some_struct_t text_in = {(uintptr_t)text_in_data, 8};
static some_struct_t text_in2 = {(uintptr_t)text_in_data, 8};
int main(void)
{
some_struct_t text_in3 = {(uintptr_t)text_in_data, 8};
static some_struct_t text_in4 = {(uintptr_t)text_in_data, 8};
static some_struct_t text_in5 = {(uintptr_t)text_in_data2, 8};
printf("text_in_data[]: ");
for (uint8_t i=0;i<8;i++)
{
printf("0x%08x, ",(unsigned int)text_in_data[i]);
}
printf("\n");
printf("text_in_data2[]: ");
for (uint8_t i=0;i<8;i++)
{
printf("0x%08x, ",(unsigned int)text_in_data2[i]);
}
printf("\n");
printf("text_in1.addr 0x%08x -- @text_in_data 0x%08x\n",(unsigned int)text_in.addr,(unsigned int)text_in_data);
printf("text_in2.addr 0x%08x -- @text_in_data 0x%08x\n",(unsigned int)text_in2.addr,(unsigned int)text_in_data);
printf("text_in3.addr 0x%08x -- @text_in_data 0x%08x\n",(unsigned int)text_in3.addr,(unsigned int)text_in_data);
printf("text_in4.addr 0x%08x -- @text_in_data 0x%08x\n",(unsigned int)text_in4.addr,(unsigned int)text_in_data);
printf("text_in_data2.addr 0x%08x -- @text_in_data2 0x%08x\n",(unsigned int)text_in5.addr,(unsigned int)text_in_data2);
text_in = (some_struct_t){(uintptr_t)text_in_data, 8};
text_in2 = (some_struct_t){(uintptr_t)text_in_data, 8};
text_in3 = (some_struct_t){(uintptr_t)text_in_data, 8};
text_in4 = (some_struct_t){(uintptr_t)text_in_data, 8};
text_in5 = (some_struct_t){(uintptr_t)text_in_data2, 8};
printf("text_in1.addr 0x%08x -- @text_in_data 0x%08x\n",(unsigned int)text_in.addr,(unsigned int)text_in_data);
printf("text_in2.addr 0x%08x -- @text_in_data 0x%08x\n",(unsigned int)text_in2.addr,(unsigned int)text_in_data);
printf("text_in3.addr 0x%08x -- @text_in_data 0x%08x\n",(unsigned int)text_in3.addr,(unsigned int)text_in_data);
printf("text_in4.addr 0x%08x -- @text_in_data 0x%08x\n",(unsigned int)text_in4.addr,(unsigned int)text_in_data);
printf("text_in_data2.addr 0x%08x -- @text_in_data2 0x%08x\n",(unsigned int)text_in5.addr,(unsigned int)text_in_data2);
return 0;
}
gives
text_in_data[]: 0x11111111, 0x22222222, 0x33333333, 0x44444444, 0x55555555, 0x66666666, 0x77777777, 0x88888888,
text_in_data2[]: 0x11111111, 0x22222222, 0x33333333, 0x44444444, 0x55555555, 0x66666666, 0x77777777, 0x88888888,
text_in1.addr 0x00000000 -- @text_in_data 0x00140478
text_in2.addr 0x00000000 -- @text_in_data 0x00140478
text_in3.addr 0x00140478 -- @text_in_data 0x00140478
text_in4.addr 0x00000000 -- @text_in_data 0x00140478
text_in_data2.addr 0x00000000 -- @text_in_data2 0x00140498
text_in1.addr 0x00140478 -- @text_in_data 0x00140478
text_in2.addr 0x00140478 -- @text_in_data 0x00140478
text_in3.addr 0x00140478 -- @text_in_data 0x00140478
text_in4.addr 0x00140478 -- @text_in_data 0x00140478
text_in_data2.addr 0x00140498 -- @text_in_data2 0x00140498
A first issue is that text_in<x>.addr
when defined with a static storage, are not initialized, but local defined are.
Quite a few things could explain this behavior, like for instance the fact that we are resetting the dataram in the crt0.
edit: the reset was not due to dataram reset which is not occuring for the test firmware code, it was corrected with the fix provided later in the post
What I can't understand though, is that text_in_data<x>
is well initialized, but text_in<x>
is not.
They should share the same section (=be stored in the same place, the same way), shouldn't they?
The only difference between those is that some are arrays and some are structures...
I looked at the differences between the original pulpino crt0.riscv.S and the one we were using.
there was no differences that could explain such behavior, but I found this patch.
Indeed, the latest crt0.S contains a few more corrections
But by JUST replacing the la a0, _edata
by la a0, _bss_start
, that made the code snippet work.
Cannot really explain why such behavior though. Because of using edata
in the bss reset loop, I think I understand that the last edata
unaligned word was resetted as well
(. = ALIGN(4);
between _edata
and _bss_start
)
BUT this does not explain why such different behavior before the fix between struct and array...
here's part of the linker script
.data : {
. = ALIGN(4);
sdata = .;
_sdata = .;
*(.data);
*(.data.*)
edata = .;
_edata = .;
} > dataram
.bss :
{
. = ALIGN(4);
_bss_start = .;
*(.bss)
*(.bss.*)
*(.sbss)
*(.sbss.*)
*(COMMON)
_bss_end = .;
} > dataram
/* ensure there is enough room for stack */
.stack (NOLOAD): {
. = ALIGN(4);
. = . + _min_stack ;
. = ALIGN(4);
stack = . ;
_stack = . ;
} > stack
.stab 0 (NOLOAD) :
{
[ .stab ]
}
.stabstr 0 (NOLOAD) :
{
[ .stabstr ]
}
.bss :
{
. = ALIGN(4);
_end = .;
} > dataram
after doing a riscv32-corev-elf-objdump -d *.elf
, the only difference between the two reports is the address of the beginning of the bss reset loop.
But the address changes to 0x1405bc
(for the working), and 0x14059c
(for the not working).
0x14059c
is the address of text_in
for those binaries.
Do you have any explaination on why such differences between array and struct and why did this patch correct the issue?