0

I was going through .lst while while trying to run gcc compiled code on my custom rv32I machine. I cant find initial vallues of sp


Disassembly of section .text:

00010074 <register_fini>:
register_fini():
   10074:   00000793            li  a5,0
   10078:   00078863            beqz    a5,10088 <register_fini+0x14>
   1007c:   00010537            lui a0,0x10
   10080:   51050513            addi    a0,a0,1296 # 10510 <__libc_fini_array>
   10084:   4e80006f            j   1056c <atexit>
   10088:   00008067            ret

0001008c <_start>:
_start():
   1008c:   00002197            auipc   gp,0x2
   10090:   e1c18193            addi    gp,gp,-484 # 11ea8 <__global_pointer$>
   10094:   c3018513            addi    a0,gp,-976 # 11ad8 <completed.1>
   10098:   c4c18613            addi    a2,gp,-948 # 11af4 <__BSS_END__>
   1009c:   40a60633            sub a2,a2,a0
   100a0:   00000593            li  a1,0
   100a4:   270000ef            jal ra,10314 <memset>
   100a8:   00000517            auipc   a0,0x0
   100ac:   4c450513            addi    a0,a0,1220 # 1056c <atexit>
   100b0:   00050863            beqz    a0,100c0 <_start+0x34>
   100b4:   00000517            auipc   a0,0x0
   100b8:   45c50513            addi    a0,a0,1116 # 10510 <__libc_fini_array>
   100bc:   4b0000ef            jal ra,1056c <atexit>
   100c0:   1b8000ef            jal ra,10278 <__libc_init_array>
   100c4:   00012503            lw  a0,0(sp)
   100c8:   00410593            addi    a1,sp,4
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
veeYceeY
  • 1
  • 1
  • 1
    Under an OS (like GNU/Linux), the kernel creates a stack and initializes the user-space stack pointer. On entry to `_start` under Linux, the stack pointer points at `argc`, and above that is `argv[]` and `envp[]`. So the CRT start code can just use the existing stack. – Peter Cordes Sep 05 '20 at 07:34
  • OK thanks, then I will have to add some startup code in the begining to make it run as bare metal application – veeYceeY Sep 05 '20 at 14:49
  • 1
    Yeah. Use `gcc -ffreestanding`; IIRC it won't link the usual CRT startup code in the first place, because that code assumes an OS. (So does libc, if you statically linked the standard libc instead of using `-ffreestanding`) – Peter Cordes Sep 05 '20 at 15:06
  • Thanks , It worked , I initialized sp from startup file and compiled my code with -ffreestanding . And passed both to linker – veeYceeY Sep 06 '20 at 17:18

1 Answers1

2

If you are rolling your own, bare metal or not, you need to solve the stack pointer, .data and .bss initialization at a minimum. If you are booting a processor with this code you need to solve the vector/exception/boot solution for that processor as well. If you are not booting the processor then you can choose to roll the stack and data/bss init into the loader of your application, or you can put it in the application. You would then need essentially a file format as an operating system uses, to tell the loader where everything is/goes as well as the instructions/data itself.

Generally if doing bare-metal you have your own bootstrap and linker script (they are a married couple) to start up the code for the specific target (not just isa, but chip/loader/whatever, system/environment). If you want to use a C library then you have a lot more work on top of that to do since a C library is heavily dependent on a system so you have to create or fake an operating system to handle all the system calls (or just don't support a C library and make life much happier).

For starters though make something super simple, blink an led or if this is a sim just make a loop with an incrementing register and look at the core in the sim to see that it booted and made it to the loop (long before you need a stack pointer). If on a system with a loader, then perhaps the uart is initialized but the loader doesn't handle sp and bss/data, then you can loop code to poke at the uart tx buffer and see that the code booted and get your tools in order before you try something more complicated.

halfer
  • 19,824
  • 17
  • 99
  • 186
old_timer
  • 69,149
  • 8
  • 89
  • 168