(:timer :parallel-unschedule): mark :broken-on :win32.
[sbcl.git] / tools-for-build / ldso-stubs.lisp
blob7521d427985063974dbe3dfa35a1245127e40109
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.
24 #!-sparc
25 (defun ldso-stubify (fct stream)
26 (format stream "LDSO_STUBIFY(~A)~%" fct))
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 stream)
34 (apply #'format stream "
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)))
45 #!+hppa
46 (defun ldso-stubify (fct stream)
47 (let ((stub (format nil "ldso_stub__~a" fct)))
48 (apply #'format stream (list
49 " .export ~A
50 ~A:
51 .proc
52 .callinfo
53 b,n ~a
54 .procend
55 .import ~a,code~%" stub stub fct fct))))
57 (defvar *preludes* '("
58 /* This is an automatically generated file, please do not hand-edit it.
59 * See the program tools-for-build/ldso-stubs.lisp. */
61 #ifndef LANGUAGE_ASSEMBLY
62 #define LANGUAGE_ASSEMBLY
63 #endif
64 #include \"sbcl.h\""
66 #!+arm "
67 #define LDSO_STUBIFY(fct) \\
68 .align ; \\
69 .global ldso_stub__ ## fct ; \\
70 .type ldso_stub__ ## fct, %function ; \\
71 ldso_stub__ ## fct: ; \\
72 ldr r8, =fct ; \\
73 bx r8 ; \\
74 .size ldso_stub__ ## fct, .-ldso_stub__ ## fct"
75 #!+arm64"
76 #define LDSO_STUBIFY(fct) \\
77 .align ; \\
78 .global ldso_stub__ ## fct ; \\
79 .type ldso_stub__ ## fct, %function ; \\
80 ldso_stub__ ## fct: ; \\
81 ldr x8, =fct ; \\
82 br x8 ; \\
83 .size ldso_stub__ ## fct, .-ldso_stub__ ## fct"
85 #!+sparc "
86 #ifdef LISP_FEATURE_SPARC
87 #include \"sparc-funcdef.h\"
88 #endif
89 .text"
91 #!+(and (or x86 x86-64) (not darwin)) "
92 #define LDSO_STUBIFY(fct) \\
93 .align 16 ; \\
94 .globl ldso_stub__ ## fct ; \\
95 .type ldso_stub__ ## fct,@function ; \\
96 ldso_stub__ ## fct: ; \\
97 jmp fct ; \\
98 .L ## fct ## e1: ; \\
99 .size ldso_stub__ ## fct,.L ## fct ## e1-ldso_stub__ ## fct ;"
101 ;;; osf1 has ancient cpp that doesn't do ##
102 #!+(and osf1 alpha) "
103 #define LDSO_STUBIFY(fct) \\
104 .globl ldso_stub__/**/fct ; \\
105 ldso_stub__/**/fct: ; \\
106 jmp fct ; \\
107 .L/**/fct/**/e1: ;"
109 ;;; but there's no reason we need to put up with that on modern (Linux) OSes
110 #!+(and linux alpha) "
111 #define LDSO_STUBIFY(fct) \\
112 .globl ldso_stub__ ## fct ; \\
113 .type ldso_stub__ ## fct,@function ; \\
114 ldso_stub__ ## fct: ; \\
115 jmp fct ; \\
116 .L ## fct ## e1: ; \\
117 .size ldso_stub__ ## fct,.L ## fct ## e1-ldso_stub__ ## fct ;"
119 #!+hppa "
120 .level 2.0
121 .text"
123 #!+(and (not darwin) ppc) "
124 #define LDSO_STUBIFY(fct) \\
125 .globl ldso_stub__ ## fct ; \\
126 .type ldso_stub__ ## fct,@function ; \\
127 ldso_stub__ ## fct: ; \\
128 b fct ; \\
129 .L ## fct ## e1: ; \\
130 .size ldso_stub__ ## fct,.L ## fct ## e1-ldso_stub__ ## fct ;"
132 #!+(and darwin ppc) "
133 #define LDSO_STUBIFY(fct) @\\
134 .text @\\
135 .globl _ldso_stub__ ## fct @\\
136 _ldso_stub__ ## fct: @\\
137 b _ldso_stub__ ## fct ## stub @\\
138 .symbol_stub _ldso_stub__ ## fct ## stub: @\\
139 .indirect_symbol _ ## fct @\\
140 lis r11,ha16(_ldso_stub__ ## fct ## $lazy_ptr) @\\
141 lwz r12,lo16(_ldso_stub__ ## fct ## $lazy_ptr)(r11) @\\
142 mtctr r12 @\\
143 addi r11,r11,lo16(_ldso_stub__ ## fct ## $lazy_ptr) @\\
144 bctr @\\
145 .lazy_symbol_pointer @\\
146 _ldso_stub__ ## fct ## $lazy_ptr: @\\
147 .indirect_symbol _ ## fct @\\
148 .long dyld_stub_binding_helper"
150 ;;; darwin x86 assembler is weird and follows the ppc assembler syntax
151 #!+(and darwin x86) "
152 #define LDSO_STUBIFY(fct) \\
153 .text ; \\
154 .align 4 ; \\
155 .globl _ldso_stub__ ## fct ; \\
156 _ldso_stub__ ## fct: ; \\
157 jmp L ## fct ## $stub ; \\
158 .section __IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5 ; \\
159 L ## fct ## $stub: ; \\
160 .indirect_symbol _ ## fct ; \\
161 hlt ; \\
162 hlt ; \\
163 hlt ; \\
164 hlt ; \\
165 hlt ; \\
166 .subsections_via_symbols ; "
168 ;;; darwin x86-64
169 #!+(and darwin x86-64) "
170 #define LDSO_STUBIFY(fct) \\
171 .align 4 ; \\
172 .globl _ldso_stub__ ## fct ; \\
173 _ldso_stub__ ## fct: ; \\
174 jmp _ ## fct ; \\
175 .L ## fct ## e1: ; "
177 ;;; KLUDGE: set up the vital fifth argument, passed on the
178 ;;; stack. Do this unconditionally, even if the stub is for a
179 ;;; function with few arguments: it can't hurt. We only do this for
180 ;;; the fifth argument, as the first four are passed in registers
181 ;;; and we apparently don't ever need to pass six arguments to a
182 ;;; libc function. -- CSR, 2003-10-29
183 ;;; Expanded to 8 arguments regardless. -- ths, 2005-03-24
184 #!+mips "
185 #define LDSO_STUBIFY(fct) \\
186 .globl ldso_stub__ ## fct ; \\
187 .type ldso_stub__ ## fct,@function ; \\
188 .ent ldso_stub__ ## fct ; \\
189 ldso_stub__ ## fct: ; \\
190 .set noat ; \\
191 addiu $29,-48 ; \\
192 sw $28,40($29) ; \\
193 sw $31,44($29) ; \\
194 lw $25,64($29) ; \\
195 lw $1,68($29) ; \\
196 sw $25,16($29) ; \\
197 sw $1,20($29) ; \\
198 lw $25,72($29) ; \\
199 lw $1,76($29) ; \\
200 sw $25,24($29) ; \\
201 sw $1,28($29) ; \\
202 .set at ; \\
203 la $25, fct ; \\
204 jalr $25 ; \\
205 lw $31,44($29) ; \\
206 lw $28,40($29) ; \\
207 addiu $29,48 ; \\
208 jr $31 ; \\
209 .end ldso_stub__ ## fct ; \\
210 .size ldso_stub__ ## fct,.-ldso_stub__ ## fct ;"))
212 (defvar *stubs* (append
213 '("_exit"
214 "accept"
215 "access"
216 "acos"
217 "acosh"
218 "asin"
219 "asinh"
220 "atanh"
221 "bind"
222 "chmod"
223 "chown"
224 "close"
225 "closedir"
226 "connect"
227 "cosh"
228 "creat"
229 "dup"
230 "dup2"
231 "execve"
232 "exit"
233 "fchmod"
234 "fchown"
235 "fcntl"
236 "fork"
237 "free"
238 "fstat"
239 #!+inode64 "fstat$INODE64"
240 "fsync"
241 "ftruncate"
242 "getcwd"
243 "getdtablesize"
244 "getegid"
245 "getenv"
246 "getgid"
247 "gethostbyaddr"
248 "gethostbyname"
249 "gethostname"
250 "getitimer"
251 "getpeername"
252 "getpgrp"
253 "getpid"
254 "getppid"
255 "getrusage"
256 "getsockname"
257 "gettimeofday"
258 "getuid"
259 "hypot"
260 "ioctl"
261 "isatty"
262 "kill"
263 "killpg"
264 "link"
265 "listen"
266 "log1p"
267 "lseek"
268 "lstat"
269 #!+inode64 "lstat$INODE64"
270 "malloc"
271 #!+(or x86 x86-64) "memcmp"
272 "memmove"
273 "mkdir"
274 "nanosleep"
275 "open"
276 "opendir"
277 "pipe"
278 "poll"
279 "pow"
280 "read"
281 "readdir"
282 "readlink"
283 "realpath"
284 "recv"
285 "rename"
286 "rmdir"
287 "select"
288 "send"
289 "setitimer"
290 "setpgrp"
291 "setsid"
292 "sinh"
293 "socket"
294 "stat"
295 #!+inode64 "stat$INODE64"
296 "strerror"
297 "strlen"
298 "symlink"
299 "sync"
300 "tanh"
301 "truncate"
302 "ttyname"
303 #!-hpux "tzname"
304 "unlink"
305 "utimes"
306 "wait3"
307 "waitpid"
308 "write")
309 ;; These aren't needed on the X86 because they're microcoded into the
310 ;; FPU, so the Lisp VOPs can implement them directly without having to
311 ;; call C code.
313 ;; Note: There might be some other functions in this category as well.
314 ;; E.g. I notice tanh() and acos() in the list above.. -- WHN 2001-06-07
315 #!-x86
316 '("sin"
317 "cos"
318 "tan"
319 "atan"
320 "atan2"
321 "exp"
322 "log"
323 "log10"
324 "sqrt")
325 #!+alpha
326 '("ieee_get_fp_control"
327 "ieee_set_fp_control")
328 ;; FIXME: After 1.0 this should be made
329 ;; #!-linkage-table, as we only need these stubs if
330 ;; we don't have linkage-table. Done this way now to
331 ;; cut down on the number of ports affected.
332 #!-(or win32 darwin freebsd netbsd openbsd)
333 '("ptsname"
334 #!-android "grantpt"
335 "unlockpt")
336 #!+(or openbsd freebsd dragonfly)
337 '("openpty")
338 '("dlclose"
339 "dlerror"
340 "dlopen"
341 "dlsym")
342 #!+bsd
343 '("sysctl")
344 #!+darwin
345 '("sysctlbyname")
346 #!+os-provides-dladdr
347 '("dladdr")
348 #!-android
349 '("nl_langinfo"
350 "getpagesize"
351 "cfgetispeed"
352 "cfgetospeed"
353 "cfsetispeed"
354 "cfsetospeed"
355 "tcdrain"
356 "tcflow"
357 "tcflush"
358 "tcgetattr"
359 "tcsendbreak"
360 "tcsetattr")))
362 (with-open-file (f "src/runtime/ldso-stubs.S" :direction :output :if-exists :supersede)
363 (assert (= (length *preludes*) 2))
364 (dolist (pre *preludes*)
365 (write-line pre f))
366 (dolist (stub *stubs*)
367 (check-type stub string)
368 (ldso-stubify stub f))
369 (format f "
370 #ifdef __ELF__
371 // Mark the object as not requiring an executable stack.
372 .section .note.GNU-stack,\"\",%progbits
373 #endif~%"))