I have created a minimal example of my problem. In the c file I
initialize an array with 16 elements. Then I call a function with the
array pointer and its size as parameter. The function itself works, I've
verified that with the disassembly file. Also the stackpointer
initialisation works fine. The problem is that the function parameter of
the array pointer is wrong. I checked it and the origin of failure is at
the very beginning of the main section. Here the pointer gets stored on
the stack.
What I don't understand: The array values are stored in the .rodata
section. This section begins at 0x1000. So the pointer to the array
should also be 0x1000. In the disassembly 0x1000 gets loaded into a5
(the right value). But then it loads the value of address a5 into a4. So
0x1000 represents a pointer to a pointer to the array, which makes no
sense imho. Has anyone a idea what I'm doing wrong?
Here is all the information needed:
c program:
1 | void test(uint8_t *array, int size){
|
2 | for(int i; i<size; ++i){
|
3 | LED_ADDR = array[i];
|
4 | }
|
5 | }
|
6 | int main(){
|
7 | uint8_t buf[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
|
8 | test(buf, 16);
|
9 | }
|
linker script:
1 | OUTPUT_ARCH( "riscv" )
|
2 | ROMSIZE = 0x1000;
|
3 | ROM_OFFSET = 0x0000;
|
4 | RAMSIZE = 0x1000;
|
5 | RAM_OFFSET = 0x1000;
|
6 | /* provide variables for startup code (stack init) */
|
7 | STACK_TOP = ( RAMSIZE + RAM_OFFSET - 4 );
|
8 | ENTRY (start)
|
9 | MEMORY {
|
10 | rom (rx) : ORIGIN = ROM_OFFSET, LENGTH = ROMSIZE
|
11 | ram (!rx) : ORIGIN = RAM_OFFSET, LENGTH = RAMSIZE
|
12 | }
|
13 | SECTIONS {
|
14 | .reset : { <PROJECTPATH>/obj/startup.o } > rom /* startup code */
|
15 | .text : { *(.text) } > rom /* executable data */
|
16 | .rodata : { *(.rodata) } > ram /* read only data */
|
17 | .sdata : { *(.sdata) } > ram /* init vars */
|
18 | .bss (NOLOAD) : { *(.bss) FILL(0x0f); } > ram /* uninit data */
|
19 | }
|
disassembly file important parts:
-in .text, beginning of main(): Pointer of array should be stored on
stack I assume:
1 | 80: 000017b7 lui a5,0x1 # a5 = first ram addr: begin of .rodata
|
2 | 84: 0007a703 lw a4,0(a5) # a4 = content of this addr?!?!?!
|
3 | 88: fee42023 sw a4,-32(s0) # a4 gets stored on stack
|
-.rodata, contains values of array:
1 | Disassembly of section .rodata:
|
2 | 00001000 <.rodata>:
|
3 | 1000: 0100
|
4 | 1002: 0302
|
5 | 1004: 0504
|
6 | ...
|
7 | 100e: 0f0e
|