1.0.4.13: refactor trap handling
[sbcl.git] / tools-for-build / ldso-stubs.lisp
1 ;;;; Generate stubs for C-linkage library functions which we need to refer to
2 ;;;; from Lisp.
3 ;;;;
4 ;;;; (But note this is only the Linux version, as per the FIXME
5 ;;;; note in the BSD version in undefineds.h.)
6 ;;;;
7 ;;;; These stubs exist for the benefit of Lisp code that needs to refer
8 ;;;; to foreign symbols when dlsym() is not available (i.e. when dumping
9 ;;;; cold-sbcl.core, when we may be running in a host that's not SBCL,
10 ;;;; or on platforms that don't have it at all). If the runtime is
11 ;;;; dynamically linked, library functions won't be linked into it, so
12 ;;;; the map file won't show them. So, we need a bunch of stubs that
13 ;;;; nm(1) _can_ see.
14 ;;;;
15 ;;;; This software is part of the SBCL system. See the README file for
16 ;;;; more information.
17 ;;;;
18 ;;;; This software is derived from the CMU CL system, which was
19 ;;;; written at Carnegie Mellon University and released into the
20 ;;;; public domain. The software is in the public domain and is
21 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
22 ;;;; files for more information.
23
24 #!-sparc
25 (defun ldso-stubify (fct str)
26   (format str "LDSO_STUBIFY(~A)~%" fct))
27
28 ;;; This is an attempt to follow DB's hint of sbcl-devel
29 ;;; 2001-09-18. -- CSR
30 ;;;
31 ;;; And an attempt to work around the Sun toolchain... --ns
32 #!+sparc
33 (defun ldso-stubify (fct str)
34   (apply #'format str "
35 .globl ldso_stub__~A ;                          \\
36         FUNCDEF(ldso_stub__~A) ;                \\
37 ldso_stub__~A: ;                                \\
38         sethi %hi(~A),%g1       ;               \\
39         jmpl %g1+%lo(~A),%g0    ;               \\
40         nop /* delay slot*/     ;               \\
41 .L~Ae1: ;                                       \\
42         .size    ldso_stub__~A,.L~Ae1-ldso_stub__~A ;~%"
43           (make-list 9 :initial-element fct)))
44
45 (defvar *preludes* '("
46 /* This is an automatically generated file, please do not hand-edit it.
47  * See the program tools-for-build/ldso-stubs.lisp. */
48
49 #ifndef LANGUAGE_ASSEMBLY
50 #define LANGUAGE_ASSEMBLY
51 #endif
52 #include \"sbcl.h\""
53
54 #!+sparc "
55 #ifdef LISP_FEATURE_SPARC
56 #include \"sparc-funcdef.h\"
57 #endif
58         .text"
59
60 #!+(and (or x86 x86-64) (not darwin)) "
61 #define LDSO_STUBIFY(fct)                       \\
62         .align 16 ;                             \\
63 .globl ldso_stub__ ## fct ;                     \\
64         .type    ldso_stub__ ## fct,@function ; \\
65 ldso_stub__ ## fct: ;                           \\
66         jmp fct ;                               \\
67 .L ## fct ## e1: ;                              \\
68         .size    ldso_stub__ ## fct,.L ## fct ## e1-ldso_stub__ ## fct ;"
69
70 ;;; osf1 has ancient cpp that doesn't do ##
71 #!+(and osf1 alpha) "
72 #define LDSO_STUBIFY(fct)                       \\
73 .globl ldso_stub__/**/fct ;                     \\
74 ldso_stub__/**/fct: ;                           \\
75         jmp fct ;                               \\
76 .L/**/fct/**/e1: ;"
77
78 ;;; but there's no reason we need to put up with that on modern (Linux) OSes
79 #!+(and linux alpha) "
80 #define LDSO_STUBIFY(fct)                       \\
81 .globl ldso_stub__ ## fct ;                     \\
82         .type    ldso_stub__ ## fct,@function ; \\
83 ldso_stub__ ## fct: ;                           \\
84         jmp fct ;                               \\
85 .L ## fct ## e1: ;                              \\
86         .size    ldso_stub__ ## fct,.L ## fct ## e1-ldso_stub__ ## fct ;"
87
88 #!+(and (not darwin) ppc) "
89 #define LDSO_STUBIFY(fct)                       \\
90 .globl ldso_stub__ ## fct ;                     \\
91         .type    ldso_stub__ ## fct,@function ; \\
92 ldso_stub__ ## fct: ;                           \\
93         b fct ;                                 \\
94 .L ## fct ## e1: ;                              \\
95         .size    ldso_stub__ ## fct,.L ## fct ## e1-ldso_stub__ ## fct ;"
96
97 #!+(and darwin ppc) "
98 #define LDSO_STUBIFY(fct)                       @\\
99 .text                                           @\\
100 .globl  ldso_stub___ ## fct                     @\\
101 ldso_stub___ ## fct:                            @\\
102         b ldso_stub__ ## fct ## stub            @\\
103 .symbol_stub ldso_stub__ ## fct ## stub:        @\\
104 .indirect_symbol _ ## fct                       @\\
105         lis     r11,ha16(ldso_stub__ ## fct ## $lazy_ptr)       @\\
106         lwz     r12,lo16(ldso_stub__ ## fct ## $lazy_ptr)(r11)  @\\
107         mtctr   r12                             @\\
108         addi    r11,r11,lo16(ldso_stub__ ## fct ## $lazy_ptr)   @\\
109         bctr                                    @\\
110 .lazy_symbol_pointer                            @\\
111 ldso_stub__ ## fct ## $lazy_ptr:                @\\
112         .indirect_symbol _ ## fct               @\\
113         .long dyld_stub_binding_helper"
114
115 ;;; darwin x86 assembler is weird and follows the ppc assembler syntax
116 #!+(and darwin x86) "
117 #define LDSO_STUBIFY(fct)                       \\
118 .text                           ;               \\
119         .align 4 ;                              \\
120 .globl ldso_stub___ ## fct ;                    \\
121 ldso_stub___ ## fct: ;                          \\
122         jmp L ## fct ## $stub ;                 \\
123         .section __IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5 ;   \\
124 L ## fct ## $stub: ;                    \\
125         .indirect_symbol _ ## fct ;     \\
126         hlt                       ;     \\
127         hlt                       ;     \\
128         hlt                       ;     \\
129         hlt                       ;     \\
130         hlt                       ;     \\
131         .subsections_via_symbols  ;    "
132
133 ;;; darwin x86-64
134 #!+(and darwin x86-64) "
135 #define LDSO_STUBIFY(fct)                       \\
136         .align 4 ;                              \\
137 .globl ldso_stub___ ## fct ;                    \\
138 ldso_stub___ ## fct: ;                          \\
139         jmp _ ## fct ;                          \\
140 .L ## fct ## e1: ;                            "
141
142 ;;; KLUDGE: set up the vital fifth argument, passed on the
143 ;;; stack.  Do this unconditionally, even if the stub is for a
144 ;;; function with few arguments: it can't hurt.  We only do this for
145 ;;; the fifth argument, as the first four are passed in registers
146 ;;; and we apparently don't ever need to pass six arguments to a
147 ;;; libc function.  -- CSR, 2003-10-29
148 ;;; Expanded to 8 arguments regardless.  -- ths, 2005-03-24
149 #!+mips "
150 #define LDSO_STUBIFY(fct)                      \\
151         .globl  ldso_stub__ ## fct ;           \\
152         .type   ldso_stub__ ## fct,@function ; \\
153         .ent    ldso_stub__ ## fct ;           \\
154 ldso_stub__ ## fct: ;                  \\
155         .set noat ;                    \\
156         addiu $29,-48 ;                \\
157         sw $28,40($29) ;               \\
158         sw $31,44($29) ;               \\
159         lw $25,64($29) ;               \\
160         lw  $1,68($29) ;               \\
161         sw $25,16($29) ;               \\
162         sw  $1,20($29) ;               \\
163         lw $25,72($29) ;               \\
164         lw  $1,76($29) ;               \\
165         sw $25,24($29) ;               \\
166         sw  $1,28($29) ;               \\
167         .set at ;                      \\
168         la $25, fct ;                  \\
169         jalr $25 ;                     \\
170         lw $31,44($29) ;               \\
171         lw $28,40($29) ;               \\
172         addiu $29,48 ;                 \\
173         jr $31 ;                       \\
174         .end    ldso_stub__ ## fct ;   \\
175         .size   ldso_stub__ ## fct,.-ldso_stub__ ## fct ;"))
176
177 (defvar *stubs* (append
178                  '("accept"
179                    "access"
180                    "acos"
181                    "acosh"
182                    "asin"
183                    "asinh"
184                    "atanh"
185                    "bind"
186                    "cfgetispeed"
187                    "cfgetospeed"
188                    "cfsetispeed"
189                    "cfsetospeed"
190                    "chmod"
191                    "chown"
192                    "close"
193                    "closedir"
194                    "connect"
195                    "cosh"
196                    "creat"
197                    "dup"
198                    "dup2"
199                    "execve"
200                    "exit"
201                    "fchmod"
202                    "fchown"
203                    "fcntl"
204                    "fork"
205                    "free"
206                    "fstat"
207                    "fsync"
208                    "ftruncate"
209                    "getcwd"
210                    "getdtablesize"
211                    "getegid"
212                    "getenv"
213                    "getgid"
214                    "gethostbyaddr"
215                    "gethostbyname"
216                    "gethostname"
217                    "getitimer"
218                    "getpagesize"
219                    "getpeername"
220                    "getpgrp"
221                    "getpid"
222                    "getppid"
223                    "getrusage"
224                    "getsockname"
225                    "gettimeofday"
226                    "getuid"
227                    "hypot"
228                    "ioctl"
229                    "isatty"
230                    "kill"
231                    "killpg"
232                    "link"
233                    "listen"
234                    "log1p"
235                    "lseek"
236                    "lstat"
237                    "malloc"
238                    "memmove"
239                    "mkdir"
240                    "nanosleep"
241                    "nl_langinfo"
242                    "open"
243                    "opendir"
244                    "pipe"
245                    "pow"
246                    "read"
247                    "readdir"
248                    "readlink"
249                    "recv"
250                    "rename"
251                    "rmdir"
252                    "select"
253                    "send"
254                    "setitimer"
255                    "setpgrp"
256                    "setsid"
257                    "sinh"
258                    "socket"
259                    "stat"
260                    "strerror"
261                    "strlen"
262                    "symlink"
263                    "sync"
264                    "tanh"
265                    "tcdrain"
266                    "tcflow"
267                    "tcflush"
268                    "tcgetattr"
269                    "tcsendbreak"
270                    "tcsetattr"
271                    "truncate"
272                    "ttyname"
273                    "tzname"
274                    "unlink"
275                    "utimes"
276                    "wait3"
277                    "write")
278                  ;; These aren't needed on the X86 because they're microcoded into the
279                  ;; FPU, so the Lisp VOPs can implement them directly without having to
280                  ;; call C code.
281                  ;;
282                  ;; Note: There might be some other functions in this category as well.
283                  ;; E.g. I notice tanh() and acos() in the list above.. -- WHN 2001-06-07
284                  #!-x86
285                  '("sin"
286                    "cos"
287                    "tan"
288                    "atan"
289                    "atan2"
290                    "exp"
291                    "log"
292                    "log10"
293                    "sqrt")
294                  #!+alpha
295                  '("ieee_get_fp_control"
296                    "ieee_set_fp_control")
297                  ;; FIXME: After 1.0 this should be made
298                  ;; #!-linkage-table, as we only need these stubs if
299                  ;; we don't have linkage-table. Done this way now to
300                  ;; cut down on the number of ports affected.
301                  #!-(or win32 darwin freebsd)
302                  '("ptsname"
303                    "grantpt"
304                    "unlockpt")
305                  #!-darwin
306                  '("dlclose"
307                    "dlerror"
308                    "dlopen"
309                    "dlsym")
310                  #!+os-provides-dladdr
311                  '("dladdr")
312                  #!-sunos ;; !defined(SVR4)
313                  '("sigsetmask")))
314
315 (with-open-file (f "src/runtime/ldso-stubs.S" :direction :output :if-exists :supersede)
316   (assert (= (length *preludes*) 2))
317   (dolist (pre *preludes*)
318     (write-line pre f))
319   (dolist (stub *stubs*)
320     (check-type stub string)
321     (ldso-stubify stub f)))
322