-
Notifications
You must be signed in to change notification settings - Fork 4
/
crt0.S
78 lines (68 loc) · 2.54 KB
/
crt0.S
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#if __riscv_xlen == 32
.equ xsize, 4
#define LOAD lw
#define STORE sw
#elif __riscv_xlen == 64
.equ xsize, 8
#define LOAD ld
#define STORE sd
#else
# error Unsupported configuration
#endif
.equ hdr_data_load_start_offset, 0
.equ hrd_data_start_offset, hdr_data_load_start_offset + xsize
.equ hdr_data_size_offset, hrd_data_start_offset + xsize
.equ hrd_bss_start_offset, hdr_data_size_offset + xsize
.equ hdr_bss_size_offset, hrd_bss_start_offset + xsize
.equ hdr_rela_dyn_start_offset, hdr_bss_size_offset + xsize
.equ hdr_rela_dyn_size_offset, hdr_rela_dyn_start_offset + xsize
.equ rela_entry_size, 3 * xsize
.equ flash_mem_start, 0x80000000
.text
.globl _start
_start:
addi sp, sp, -(4*xsize)
STORE ra, 3*xsize(sp)
STORE s0, 2*xsize(sp)
STORE s1, 1*xsize(sp)
STORE s2, 0*xsize(sp)
mv s0, a0
mv s1, a1
mv s2, a2
LOAD a0, hrd_data_start_offset(s0) // a0 <- hdr->data_start
add a0, a0, a2 // a0 <- hdr->data_start + mem_offset
LOAD a1, hdr_data_load_start_offset(s0) // a1 <- hdr->data_load_start
add a1, a1, s1 // a1 <- hdr->data_load_start + flash_offset
LOAD a2, hdr_data_size_offset(s0) // a2 <- hdr->data_size
call memcpy // memcpy(data_start + mem_offset, data_load_start + flash_offset, hdr->data_size);
LOAD a0, hrd_bss_start_offset(s0) // a0 <- hdr->bss_start
add a0, a0, s2 // a0 <- hdr->bss_start + mem_offset
li a1, 0 // a1 <- 0
LOAD a2, hdr_bss_size_offset(s0) // a2 <- hdr->bss_size
call memset // memset(bss_start + mem_offset, 0, hdr->bss_size);
la t0, __rela_dyn_end
la t4, __rela_dyn_start
li t3, flash_mem_start
1:
LOAD t5, 0*xsize(t4) // t5 <- rel_data_entry->r_offset
add t5, t5, s2 // t5 <- rel_data_entry->r_offset + mem_offset
LOAD t6, 2*xsize(t4) // t6 <- rel_data_entry->r_addend
bgeu t6, t3, 2f
add t6, t6, s2 // t6 <- rel_data_entry->r_addend + mem_offset
j 3f
2:
add t6, t6, s1 // t6 <- rel_data_entry->r_addend + flash_offset
3:
STORE t6, 0(t5)
addi t4, t4, rela_entry_size
bltu t4, t0, 1b
mv a2, s2
mv a1, s1
mv a0, s0
call main
LOAD ra, 3*xsize(sp)
LOAD s0, 2*xsize(sp)
LOAD s1, 1*xsize(sp)
LOAD s2, 0*xsize(sp)
addi sp, sp, 4*xsize
ret