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