0.8.21.20
[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 #define LANGUAGE_ASSEMBLY
50 #include \"sbcl.h\""
51
52 #!+sparc "
53 #ifdef LISP_FEATURE_SPARC
54 #include \"sparc-funcdef.h\"
55 #endif
56         .text"
57
58 #!+(or x86 x86-64) "
59 #define LDSO_STUBIFY(fct)                       \\
60         .align 16 ;                             \\
61 .globl ldso_stub__ ## fct ;                     \\
62         .type    ldso_stub__ ## fct,@function ; \\
63 ldso_stub__ ## fct: ;                           \\
64         jmp fct ;                               \\
65 .L ## fct ## e1: ;                              \\
66         .size    ldso_stub__ ## fct,.L ## fct ## e1-ldso_stub__ ## fct ;"
67
68 ;;; osf1 has ancient cpp that doesn't do ##
69 #!+(and osf1 alpha) "
70 #define LDSO_STUBIFY(fct)                       \\
71 .globl ldso_stub__/**/fct ;                     \\
72 ldso_stub__/**/fct: ;                           \\
73         jmp fct ;                               \\
74 .L/**/fct/**/e1: ;"
75
76 ;;; but there's no reason we need to put up with that on modern (Linux) OSes
77 #!+(and linux alpha) "
78 #define LDSO_STUBIFY(fct)                       \\
79 .globl ldso_stub__ ## fct ;                     \\
80         .type    ldso_stub__ ## fct,@function ; \\
81 ldso_stub__ ## fct: ;                           \\
82         jmp fct ;                               \\
83 .L ## fct ## e1: ;                              \\
84         .size    ldso_stub__ ## fct,.L ## fct ## e1-ldso_stub__ ## fct ;"
85
86 #!+(and linux ppc) "
87 #define LDSO_STUBIFY(fct)                       \\
88 .globl ldso_stub__ ## fct ;                     \\
89         .type    ldso_stub__ ## fct,@function ; \\
90 ldso_stub__ ## fct: ;                           \\
91         b fct ;                                 \\
92 .L ## fct ## e1: ;                              \\
93         .size    ldso_stub__ ## fct,.L ## fct ## e1-ldso_stub__ ## fct ;"
94
95 #!+(and darwin ppc) "
96 #define LDSO_STUBIFY(fct)                       @\\
97 .text                                           @\\
98 .globl  ldso_stub___ ## fct                     @\\
99 ldso_stub___ ## fct:                            @\\
100         b ldso_stub__ ## fct ## stub            @\\
101 .symbol_stub ldso_stub__ ## fct ## stub:        @\\
102 .indirect_symbol _ ## fct                       @\\
103         lis     r11,ha16(ldso_stub__ ## fct ## $lazy_ptr)       @\\
104         lwz     r12,lo16(ldso_stub__ ## fct ## $lazy_ptr)(r11)  @\\
105         mtctr   r12                             @\\
106         addi    r11,r11,lo16(ldso_stub__ ## fct ## $lazy_ptr)   @\\
107         bctr                                    @\\
108 .lazy_symbol_pointer                            @\\
109 ldso_stub__ ## fct ## $lazy_ptr:                @\\
110         .indirect_symbol _ ## fct               @\\
111         .long dyld_stub_binding_helper"
112         
113 ;;; KLUDGE: set up the vital fifth argument, passed on the 
114 ;;; stack.  Do this unconditionally, even if the stub is for a
115 ;;; function with few arguments: it can't hurt.  We only do this for
116 ;;; the fifth argument, as the first four are passed in registers
117 ;;; and we apparently don't ever need to pass six arguments to a
118 ;;; libc function.  -- CSR, 2003-10-29
119 ;;; Expanded to 8 arguments regardless.  -- ths, 2005-03-24
120 #!+mips "
121 #define LDSO_STUBIFY(fct)                      \\
122         .globl  ldso_stub__ ## fct ;           \\
123         .type   ldso_stub__ ## fct,@function ; \\
124         .ent    ldso_stub__ ## fct ;           \\
125 ldso_stub__ ## fct: ;                  \\
126         addiu $29,-48 ;                \\
127         sw $28,40($29) ;               \\
128         sw $31,44($29) ;               \\
129         lw $25,64($29) ;               \\
130         sw $25,16($29) ;               \\
131         lw $25,68($29) ;               \\
132         sw $25,20($29) ;               \\
133         lw $25,72($29) ;               \\
134         sw $25,24($29) ;               \\
135         lw $25,76($29) ;               \\
136         sw $25,28($29) ;               \\
137         la $25, fct ;                  \\
138         jalr $25 ;                     \\
139         lw $31,44($29) ;               \\
140         lw $28,40($29) ;               \\
141         addiu $29,48 ;                 \\
142         jr $31 ;                       \\
143         .end    ldso_stub__ ## fct ;   \\
144         .size   ldso_stub__ ## fct,.-ldso_stub__ ## fct ;"))
145
146 (defvar *stubs* (append
147                  '("accept"
148                    "access"
149                    "acos"
150                    "acosh"
151                    "asin"
152                    "asinh"
153                    "atanh"
154                    "bind"
155                    "cfgetispeed"
156                    "cfgetospeed"
157                    "cfsetispeed"
158                    "cfsetospeed"
159                    "chmod"
160                    "chown"
161                    "close"
162                    "closedir"
163                    "connect"
164                    "cosh"
165                    "creat"
166                    "dup"
167                    "dup2"
168                    "execve"
169                    "exit"
170                    "fchmod"
171                    "fchown"
172                    "fcntl"
173                    "fork"
174                    "free"
175                    "fstat"
176                    "fsync"
177                    "ftruncate"
178                    "getcwd"
179                    "getdtablesize"
180                    "getegid"
181                    "getenv"
182                    "getgid"
183                    "gethostbyaddr"
184                    "gethostbyname"
185                    "gethostname"
186                    "getitimer"
187                    "getpagesize"
188                    "getpeername"
189                    "getpgrp"
190                    "getpid"
191                    "getppid"
192                    "getrusage"
193                    "getsockname"
194                    "gettimeofday"
195                    "getuid"
196                    "hypot"
197                    "ioctl"
198                    "isatty"
199                    "kill"
200                    "killpg"
201                    "link"
202                    "listen"
203                    "log1p"
204                    "lseek"
205                    "lstat"
206                    "malloc"
207                    "memmove"
208                    "mkdir"
209                    "nanosleep"
210                    "nl_langinfo"
211                    "open"
212                    "opendir"
213                    "pipe"
214                    "pow"
215                    "read"
216                    "readdir"
217                    "readlink"
218                    "recv"
219                    "rename"
220                    "rmdir"
221                    "select"
222                    "send"
223                    "setitimer"
224                    "setpgrp"
225                    "setsid"
226                    "sinh"
227                    "socket"
228                    "stat"
229                    "strerror"
230                    "strlen"
231                    "symlink"
232                    "sync"
233                    "tanh"
234                    "tcdrain"
235                    "tcflow"
236                    "tcflush"
237                    "tcgetattr"
238                    "tcsendbreak"
239                    "tcsetattr"
240                    "truncate"
241                    "ttyname"
242                    "tzname"
243                    "unlink"
244                    "utimes"
245                    "wait3"      
246                    "write")
247                  ;; These aren't needed on the X86 because they're microcoded into the
248                  ;; FPU, so the Lisp VOPs can implement them directly without having to
249                  ;; call C code.
250                  ;;
251                  ;; Note: There might be some other functions in this category as well.
252                  ;; E.g. I notice tanh() and acos() in the list above.. -- WHN 2001-06-07
253                  #!-x86 
254                  '("sin"
255                    "cos" 
256                    "tan"      
257                    "atan" 
258                    "atan2" 
259                    "exp"      
260                    "log" 
261                    "log10" 
262                    "sqrt")
263                  #!+alpha
264                  '("ieee_get_fp_control"
265                    "ieee_set_fp_control")
266                  #!-darwin
267                  '("dlclose"
268                    "dlerror"
269                    "dlopen"
270                    "dlsym")
271                  #!+os-provides-dladdr 
272                  '("dladdr")
273                  #!-(and sparc sunos) ;; !defined(SVR4)
274                  '("sigsetmask")))
275
276 (with-open-file (f "src/runtime/ldso-stubs.S" :direction :output :if-exists :supersede)
277   (assert (= (length *preludes*) 2))
278   (dolist (pre *preludes*)
279     (write-line pre f))
280   (dolist (stub *stubs*)
281     (check-type stub string)
282     (ldso-stubify stub f)))
283