1 define(`CC', `
2 dnl')
3
4 CC Copyright (C) 2023 Free Software Foundation, Inc.
5 CC
6 CC This file is part of GNU Emacs.
7 CC
8 CC GNU Emacs is free software: you can redistribute it and/or modify
9 CC it under the terms of the GNU General Public License as published
10 CC by the Free Software Foundation, either version 3 of the License,
11 CC or (at your option) any later version.
12 CC
13 CC GNU Emacs is distributed in the hope that it will be useful, but
14 CC WITHOUT ANY WARRANTY; without even the implied warranty of
15 CC MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 CC General Public License for more details.
17 CC
18 CC You should have received a copy of the GNU General Public License
19 CC along with GNU Emacs. If not, see <https:
20
21 .section .text
22 .global _start
23 _start:
24 dnl movl $162, %eax CC SYS_nanosleep
25 dnl leal timespec, %ebx
26 dnl xorl %ecx, %ecx
27 dnl int $0x80
28 leal 8(%esp), %ebp CC ebp = start of load area
29 subl $8, %esp CC (%esp) = primary fd, 4(%esp) = secondary fd
30 movl $-1, 4(%esp)
31 .next_action:
32 movl (%ebp), %edx CC edx = action number
33 andl $-17, %edx
34 cmpl $0, %edx CC open file?
35 je .open_file
36 cmpl $3, %edx CC jump?
37 je .rest_of_exec
38 cmpl $4, %edx CC anonymous mmap?
39 je .do_mmap_anon
40 .do_mmap:
41 subl $24, %esp
42 movl $90, %eax CC SYS_old_mmap
43 movl %esp, %ebx
44 movl 4(%ebp), %ecx CC address
45 movl %ecx, (%esp)
46 movl 16(%ebp), %ecx CC length
47 movl %ecx, 4(%esp)
48 movl 12(%ebp), %ecx CC protection
49 movl %ecx, 8(%esp)
50 movl 20(%ebp), %ecx CC flags
51 movl %ecx, 12(%esp)
52 testl $16, (%ebp) CC primary?
53 movl 28(%esp), %ecx
54 cmovzl 24(%esp), %ecx
55 movl %ecx, 16(%esp) CC fd
56 movl 8(%ebp), %ecx CC offset
57 movl %ecx, 20(%esp)
58 .do_mmap_1:
59 int $0x80
60 addl $24, %esp CC restore esp
61 cmpl $-1, %eax CC mmap failed?
62 je .perror
63 movl 24(%ebp), %ecx CC clear
64 testl %ecx, %ecx
65 jz .continue
66 movl 4(%ebp), %esi CC start of mapping
67 addl 16(%ebp), %esi CC end of mapping
68 subl %ecx, %esi CC start of clear area
69 .again:
70 testl %ecx, %ecx
71 jz .continue
72 subl $1, %ecx
73 movb $0, (%esi, %ecx, 1)
74 jmp .again
75 .continue:
76 leal 28(%ebp), %ebp
77 jmp .next_action
78 .do_mmap_anon:
79 subl $24, %esp
80 movl $90, %eax CC SYS_old_mmap
81 movl %esp, %ebx
82 movl 4(%ebp), %ecx CC address
83 movl %ecx, (%esp)
84 movl 16(%ebp), %ecx CC length
85 movl %ecx, 4(%esp)
86 movl 12(%ebp), %ecx CC protection
87 movl %ecx, 8(%esp)
88 movl 20(%ebp), %ecx CC flags
89 movl %ecx, 12(%esp)
90 movl $-1, 16(%esp) CC fd
91 movl 8(%ebp), %ecx CC offset
92 movl %ecx, 20(%esp)
93 jmp .do_mmap_1
94 .open_file:
95 movl $5, %eax CC SYS_open
96 leal 4(%ebp), %ebx CC ebx = %esp + 8
97 pushl %ebx
98 xorl %ecx, %ecx CC flags = O_RDONLY
99 xorl %edx, %edx CC mode = 0
100 int $0x80
101 cmpl $-1, %eax CC open failed?
102 jle .perror
103 movl %ebp, %esi CC (esi) = original action number
104 popl %ebp CC ebp = start of string
105 movl %ebp, %ecx CC char past separator
106 decl %ebp
107 .nextc:
108 incl %ebp
109 movb (%ebp), %dl CC dl = *ebp
110 cmpb $47, %dl CC dl == '\?'?
111 jne .nextc1
112 leal 1(%ebp), %ecx CC ecx = char past separator
113 .nextc1:
114 cmpb $0, %dl CC dl == 0?
115 jne .nextc
116 addl $4, %ebp CC adjust past ebp prior to rounding
117 andl $-4, %ebp CC round ebp up to the next long
118 testl $16, (%esi) CC original action number & 16?
119 jz .primary
120 movl %eax, 4(%esp) CC secondary fd = eax
121 jmp .next_action
122 .primary:
123 pushl %ebp
124 xorl %esi, %esi CC arg3
125 movl %eax, 4(%esp) CC primary fd = eax
126 xorl %edx, %edx CC arg2
127 movl $15, %ebx CC PR_SET_NAME, arg1 = ecx
128 xorl %edi, %edi CC arg4
129 movl $172, %eax CC SYS_prctl
130 xorl %ebp, %ebp CC arg5
131 int $0x80 CC syscall
132 popl %ebp
133 jmp .next_action
134 .perror:
135 movl %eax, %ebx
136 negl %ebx
137 movl $1, %eax
138 int $0x80
139 .rest_of_exec:
140 movl 8(%esp), %ecx CC ecx = original stack pointer
141 movl (%ecx), %esi CC esi = argc
142 leal 8(%ecx, %esi, 4), %ecx CC ecx = start of environ
143 .skip_environ:
144 movl (%ecx), %esi CC envp[N]
145 addl $4, %ecx
146 testl %esi, %esi CC envp[n] ?
147 jnz .skip_environ CC otherwise, esi is now at the start of auxv
148 .one_auxv:
149 movl (%ecx), %esi CC auxv type
150 leal 8(%ecx), %ecx CC skip to next auxv
151 testl %esi, %esi CC is 0?
152 jz .cleanup
153 cmpl $3, %esi CC is AT_PHDR
154 je .replace_phdr
155 cmpl $4, %esi CC is AT_PHENT?
156 je .replace_phent
157 cmpl $5, %esi CC is AT_PHNUM?
158 je .replace_phnum
159 cmpl $9, %esi CC is AT_ENTRY?
160 je .replace_entry
161 cmpl $7, %esi CC is AT_BASE
162 je .replace_base
163 jmp .one_auxv
164 .replace_phdr:
165 movl 20(%ebp), %esi
166 movl %esi, -4(%ecx)
167 jmp .one_auxv
168 .replace_phent:
169 movl 12(%ebp), %esi
170 movl %esi, -4(%ecx)
171 jmp .one_auxv
172 .replace_phnum:
173 movl 16(%ebp), %esi
174 movl %esi, -4(%ecx)
175 jmp .one_auxv
176 .replace_entry:
177 movl 8(%ebp), %esi
178 movl %esi, -4(%ecx)
179 jmp .one_auxv
180 .replace_base:
181 movl 24(%ebp), %esi
182 movl %esi, -4(%ecx)
183 jmp .one_auxv
184 .cleanup:
185 movl $6, %eax CC SYS_close
186 cmpl $-1, 4(%esp) CC see if interpreter fd is set
187 je .cleanup_1
188 movl 4(%esp), %ebx
189 int $0x80
190 movl $6, %eax CC SYS_close
191 .cleanup_1:
192 movl (%esp), %ebx
193 int $0x80
194 .enter:
195 pushl $0
196 popfl CC restore floating point state
197 movl 8(%esp), %esp CC restore initial stack pointer
198 xorl %edx, %edx CC clear rtld_fini
199 jmpl *4(%ebp) CC entry
200
201 timespec:
202 .long 10
203 .long 10