22
22
. section .text
23
23
. global _start
24
24
_start:
25
- // mov x8 , 101 // SYS_nanosleep
26
- //adr x0 , timespec // req
27
- // mov x1 , # 0 // rem
28
- //svc # 0 // syscall
25
+ // mov x8 , 101 // SYS_nanosleep
26
+ // adr x0 , timespec // req
27
+ // mov x1 , # 0 // rem
28
+ // svc # 0 // syscall
29
29
mov x20 , sp // x20 = sp
30
30
ldr x10 , [ x20 ] // x10 = original SP
31
31
add x20 , x20 , # 16 // x20 = start of load area
32
32
mov x28 , # - 1 // x28 = secondary fd
33
- . next_action:
33
+ next_action:
34
34
ldr x11 , [ x20 ] // action number
35
35
and x12 , x11 , # - 17 // actual action number
36
- cbz x12 , . open_file // open file?
36
+ cbz x12 , open_file // open file?
37
37
cmp x12 , # 3 // jump?
38
- beq . rest_of_exec
38
+ beq rest_of_exec
39
39
cmp x12 , # 4 // anonymous mmap?
40
- beq . do_mmap_anon
41
- . do_mmap:
40
+ beq do_mmap_anon
41
+ do_mmap:
42
42
ldr x0 , [ x20 , 8 ] // vm_address
43
43
ldr x1 , [ x20 , 32 ] // length
44
44
ldr x2 , [ x20 , 24 ] // protection
45
45
ldr x3 , [ x20 , 40 ] // flags
46
46
tst x11 , # 16 // primary fd?
47
47
mov x4 , x29 // primary fd
48
- beq . do_mmap_1
48
+ beq do_mmap_1
49
49
mov x4 , x28 // secondary fd
50
- . do_mmap_1:
50
+ do_mmap_1:
51
51
mov x8 , # 222 // SYS_mmap
52
52
ldr x5 , [ x20 , 16 ] // file_offset
53
53
svc # 0 // syscall
54
54
ldr x9 , [ x20 , 8 ] // length
55
55
cmp x0 , x9 // mmap result
56
- bne . perror // print error
56
+ bne perror // print error
57
57
ldr x3 , [ x20 , 48 ] // clear
58
58
add x1 , x1 , x0 // x1 = vm_address + end
59
59
sub x3 , x1 , x3 // x3 = x1 - clear
60
60
mov x0 , # 0 // x0 = 0
61
- . fill64:
61
+ fill64:
62
62
sub x2 , x1 , x3 // x2 = x1 - x3
63
63
cmp x2 , # 63 // x2 >= 64 ?
64
- ble . fillb // start filling bytes
64
+ ble fillb // start filling bytes
65
65
stp x0 , x0 , [ x3 ] // x3 [ 0 ] = 0 , x3 [ 1 ] = 0
66
66
stp x0 , x0 , [ x3 , 16 ] // x3 [ 2 ] = 0 , x3 [ 3 ] = 0
67
67
stp x0 , x0 , [ x3 , 32 ] // x3 [ 4 ] = 0 , x3 [ 5 ] = 0
68
68
stp x0 , x0 , [ x3 , 48 ] // x3 [ 6 ] = 0 , x3 [ 7 ] = 0
69
69
add x3 , x3 , # 64 // x3 + = 8
70
- b . fill64
71
- . fillb:
70
+ b fill64
71
+ fillb:
72
72
cmp x1 , x3 // x1 == x3?
73
- beq . continue // done
73
+ beq continue // done
74
74
strb w0 , [ x3 ], # 1 // ((char * ) x3) ++ = 0
75
- b . fillb
76
- . continue:
75
+ b fillb
76
+ continue:
77
77
add x20 , x20 , # 56 // next action
78
- b . next_action
79
- . do_mmap_anon:
78
+ b next_action
79
+ do_mmap_anon:
80
80
ldr x0 , [ x20 , 8 ] // vm_address
81
81
ldr x1 , [ x20 , 32 ] // length
82
82
ldr x2 , [ x20 , 24 ] // protection
83
83
ldr x3 , [ x20 , 40 ] // flags
84
84
mov x4 , # - 1 // fd
85
- b . do_mmap_1
86
- . open_file:
85
+ b do_mmap_1
86
+ open_file:
87
87
mov x8 , # 56 // SYS_open at
88
88
mov x0 , # - 100 // AT_FDCWD
89
89
add x1 , x20 , # 8 // file name
90
90
mov x2 , # 0 // O_RDONLY
91
91
mov x3 , # 0 // mode
92
92
svc # 0 // syscall
93
93
cmp x0 , # - 1 // rc < 0 ?
94
- ble . perror
94
+ ble perror
95
95
mov x19 , x1 // x19 == x1
96
- . nextc:
96
+ nextc:
97
97
ldrb w2 , [ x1 ], # 1 // b = * x1 ++
98
98
cmp w2 , # 47 // dir separator?
99
- bne . nextc1 // not dir separator
99
+ bne nextc1 // not dir separator
100
100
mov x19 , x1 // x19 = char past separator
101
- . nextc1:
102
- cbnz w2 , . nextc // b?
101
+ nextc1:
102
+ cbnz w2 , nextc // b?
103
103
add x1 , x1 , # 7 // round up x1
104
104
and x20 , x1 , # - 8 // mask for round , set x20
105
105
tst x11 , # 16 // primary fd?
106
- bne . secondary // secondary fd
106
+ bne secondary // secondary fd
107
107
mov x29 , x0 // primary fd
108
108
mov x8 , # 167 // SYS_prctl
109
109
mov x0 , # 15 // PR_SET_NAME
@@ -113,75 +113,117 @@ _start:
113
113
mov x4 , # 0 // arg4
114
114
mov x5 , # 0 // arg5
115
115
svc # 0 // syscall
116
- b . next_action // next action
117
- . secondary:
116
+ b next_action // next action
117
+ secondary:
118
118
mov x28 , x0 // secondary fd
119
- b . next_action // next action.
120
- . perror:
119
+ b next_action // next action.
120
+ perror:
121
121
mov x8 , # 93 // SYS_exit
122
122
mvn x0 , x0 // x1 = ~x0
123
123
add x0 , x0 , 1 // x1 + = 1
124
124
svc # 0 // exit
125
- . rest_of_exec:
125
+ rest_of_exec:
126
126
mov x7 , x20 // x7 = x20
127
- mov x20 , x10 // x20 = x10
128
- ldr x9 , [ x20 ] // argc
129
- add x9 , x9 , # 2 // x9 + = 2
127
+ mov x8 , x10 // x8 = x10
128
+ ldr x9 , [ x8 ], # 16 // (void * ) x8 + = 2
130
129
lsl x9 , x9 , # 3 // argc * 8
131
- add x20 , x20 , x9 // now past argv
132
- .skipenv:
133
- ldr x9 , [ x20 ], # 8 // x9 = * envp ++
134
- cbnz x9 , .skipenv // x9?
135
- .one_auxv:
136
- ldr x9 , [ x20 ], # 16 // x9 = * sp , sp + = 2
137
- cbz x9 , .cleanup // !x9?
138
- cmp x9 , # 3 // is AT_PHDR?
139
- beq .replace_phdr // replace
140
- cmp x9 , # 4 // is AT_PHENT?
141
- beq .replace_phent // replace
142
- cmp x9 , # 5 // is AT_PHNUM?
143
- beq .replace_phnum // replace
144
- cmp x9 , # 9 // is AT_ENTRY?
145
- beq .replace_entry // replace
146
- cmp x9 , # 7 // is AT_BASE?
147
- beq .replace_base // replace
148
- b .one_auxv // next auxv
149
- .replace_phdr:
150
- ldr x9 , [ x7 , 40 ] // at_phdr
151
- str x9 , [ x20 , - 8 ] // store value
152
- b .one_auxv
153
- .replace_phent:
154
- ldr x9 , [ x7 , 24 ] // at_phent
155
- str x9 , [ x20 , - 8 ] // store value
156
- b .one_auxv
157
- .replace_phnum:
158
- ldr x9 , [ x7 , 32 ] // at_phnum
159
- str x9 , [ x20 , - 8 ] // store value
160
- b .one_auxv
161
- .replace_entry:
162
- ldr x9 , [ x7 , 16 ] // at_entry
163
- str x9 , [ x20 , - 8 ] // store value
164
- b .one_auxv
165
- .replace_base:
166
- ldr x9 , [ x7 , 48 ] // at_base
167
- str x9 , [ x20 , - 8 ] // store value
168
- b .one_auxv
169
- .cleanup:
170
- cmp x28 , # - 1 // is secondary fd set?
171
- bne .cleanup1 // not set
130
+ add x8 , x8 , x9 // now past argv
131
+ skip_environ:
132
+ ldr x9 , [ x8 ], # 8 // x9 = * envp ++
133
+ cbnz x9 , skip_environ // x9?
134
+ // Skip the auxiliary vector.
135
+ 1 : ldp x11 , x12 , [ x8 ], # 16 // a_type , a_un.a_val
136
+ cbnz x11 , 1b // a_type != NULL
137
+ // Prepare sufficient space at x20 for the file name string.
138
+ // Load the aforesaid string , and its length.
139
+ ldr x6 , [ x7 , 56 ] // string length
140
+ add x6 , x6 , 1
141
+ add x5 , x7 , 64 // string pointer
142
+ sub x4 , x10 , x8 // number of elements to copy
143
+ sub x7 , x8 , x6 // AT_EXECFN location
144
+ and x7 , x7 , - 8 // align value
145
+ add x4 , x7 , x4 // destination argc
146
+ and x4 , x4 , - 16 // align destination argc
147
+ // Load values th at must be into registers x14 - x19.
148
+ // x14 = cmd - >entry
149
+ // x15 = cmd - >at_entry
150
+ // x16 = cmd - >at_phent
151
+ // x17 = cmd - >at_phnum
152
+ // x18 = cmd - >at_phdr
153
+ // x19 = cmd - >at_base
154
+ ldp x14 , x15 , [ x20 , 8 ]
155
+ ldp x16 , x17 , [ x20 , 24 ]
156
+ ldp x18 , x19 , [ x20 , 40 ]
157
+ // Move the string to a safe location , if necessary.
158
+ sub x3 , x4 , x5 // distance from dest to string
159
+ cmp x3 , x6 // distance > length
160
+ bge copy_env_and_args // not necessary
161
+ mov x2 , x5 // src
162
+ sub x5 , x4 , x6 // backup string
163
+ mov x1 , x5 // dst
164
+ add x9 , x2 , x6 // src end
165
+ cmp x2 , x9
166
+ bcs copy_env_and_args
167
+ 1 : ldrb w3 , [ x2 ], # 1
168
+ strb w3 , [ x1 ], # 1
169
+ cmp x2 , x9
170
+ bls 1b
171
+ copy_env_and_args:
172
+ // Copy argc and the environment array.
173
+ mov x8 , x10
174
+ mov x10 , x4
175
+ 1 : ldr x9 , [ x8 ], # 8 // envp
176
+ str x9 , [ x4 ], # 8
177
+ cbnz x9 , 1b
178
+ 1 : ldr x9 , [ x8 ], # 8 // environ
179
+ str x9 , [ x4 ], # 8
180
+ cbnz x9 , 1b
181
+ copy_auxv:
182
+ ldp x11 , x12 , [ x8 ], # 16 // a_type , a_un.a_val
183
+ stp x11 , x12 , [ x4 ], # 16 // write value
184
+ cbz x11 , cleanup // AT_NULL
185
+ cmp x11 , # 3 // AT_PHDR
186
+ csel x12 , x18 , x12 , eq
187
+ cmp x11 , # 4 // AT_PHENT
188
+ csel x12 , x16 , x12 , eq
189
+ cmp x11 , # 5 // AT_PHNUM
190
+ csel x12 , x17 , x12 , eq
191
+ cmp x11 , # 9 // AT_ENTRY
192
+ csel x12 , x15 , x12 , eq
193
+ cmp x11 , # 7 // AT_BASE
194
+ csel x12 , x19 , x12 , eq
195
+ cmp x11 , # 31 // AT_EXECFN
196
+ csel x12 , x7 , x12 , eq
197
+ str x12 , [ x4 , - 8 ] // replace value
198
+ b copy_auxv
199
+ cleanup:
200
+ // Copy the filename.
201
+ add x9 , x5 , x6 // end
202
+ cmp x5 , x9
203
+ bcs 2f
204
+ 1 : ldrb w3 , [ x5 ], # 1
205
+ strb w3 , [ x7 ], # 1
206
+ cmp x5 , x9
207
+ bls 1b
208
+ // Close file descriptors.
209
+ 2 : cmp x28 , # - 1 // is secondary fd set?
210
+ beq cleanup1 // not set
172
211
mov x8 , # 57 // SYS_close
173
212
mov x0 , x28 // secondary fd
174
213
svc # 0 // syscall
175
- . cleanup1:
214
+ cleanup1:
176
215
mov x8 , # 57 // SYS_close
177
216
mov x0 , x29 // primary fd
178
217
svc # 0 // syscall
179
- . enter :
218
+ enter :
180
219
mov sp , x10 // restore original SP
181
220
mov x0 , # 0 // clear rtld_fini
182
- ldr x1 , [ x7 , 8 ] // branch to code
183
- br x1
221
+ br x14
184
222
185
- timespec:
186
- .quad 10
187
- .quad 10
223
+ // timespec:
224
+ // .quad 10
225
+ // .quad 10
226
+
227
+ // Local Variables:
228
+ // asm - comment - char: ?/
229
+ // End:
0 commit comments