0.9.2.26: refactoring internals of foreign linkage
[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 #!+(or x86 x86-64) "
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 linux 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 ;;; KLUDGE: set up the vital fifth argument, passed on the 
116 ;;; stack.  Do this unconditionally, even if the stub is for a
117 ;;; function with few arguments: it can't hurt.  We only do this for
118 ;;; the fifth argument, as the first four are passed in registers
119 ;;; and we apparently don't ever need to pass six arguments to a
120 ;;; libc function.  -- CSR, 2003-10-29
121 ;;; Expanded to 8 arguments regardless.  -- ths, 2005-03-24
122 #!+mips "
123 #define LDSO_STUBIFY(fct)                      \\
124         .globl  ldso_stub__ ## fct ;           \\
125         .type   ldso_stub__ ## fct,@function ; \\
126         .ent    ldso_stub__ ## fct ;           \\
127 ldso_stub__ ## fct: ;                  \\
128         .set noat ;                    \\
129         addiu $29,-48 ;                \\
130         sw $28,40($29) ;               \\
131         sw $31,44($29) ;               \\
132         lw $25,64($29) ;               \\
133         lw  $1,68($29) ;               \\
134         sw $25,16($29) ;               \\
135         sw  $1,20($29) ;               \\
136         lw $25,72($29) ;               \\
137         lw  $1,76($29) ;               \\
138         sw $25,24($29) ;               \\
139         sw  $1,28($29) ;               \\
140         .set at ;                      \\
141         la $25, fct ;                  \\
142         jalr $25 ;                     \\
143         lw $31,44($29) ;               \\
144         lw $28,40($29) ;               \\
145         addiu $29,48 ;                 \\
146         jr $31 ;                       \\
147         .end    ldso_stub__ ## fct ;   \\
148         .size   ldso_stub__ ## fct,.-ldso_stub__ ## fct ;"))
149
150 (defvar *stubs* (append
151                  '("accept"
152                    "access"
153                    "acos"
154                    "acosh"
155                    "asin"
156                    "asinh"
157                    "atanh"
158                    "bind"
159                    "cfgetispeed"
160                    "cfgetospeed"
161                    "cfsetispeed"
162                    "cfsetospeed"
163                    "chmod"
164                    "chown"
165                    "close"
166                    "closedir"
167                    "connect"
168                    "cosh"
169                    "creat"
170                    "dup"
171                    "dup2"
172                    "execve"
173                    "exit"
174                    "fchmod"
175                    "fchown"
176                    "fcntl"
177                    "fork"
178                    "free"
179                    "fstat"
180                    "fsync"
181                    "ftruncate"
182                    "getcwd"
183                    "getdtablesize"
184                    "getegid"
185                    "getenv"
186                    "getgid"
187                    "gethostbyaddr"
188                    "gethostbyname"
189                    "gethostname"
190                    "getitimer"
191                    "getpagesize"
192                    "getpeername"
193                    "getpgrp"
194                    "getpid"
195                    "getppid"
196                    "getrusage"
197                    "getsockname"
198                    "gettimeofday"
199                    "getuid"
200                    "hypot"
201                    "ioctl"
202                    "isatty"
203                    "kill"
204                    "killpg"
205                    "link"
206                    "listen"
207                    "log1p"
208                    "lseek"
209                    "lstat"
210                    "malloc"
211                    "memmove"
212                    "mkdir"
213                    "nanosleep"
214                    "nl_langinfo"
215                    "open"
216                    "opendir"
217                    "pipe"
218                    "pow"
219                    "read"
220                    "readdir"
221                    "readlink"
222                    "recv"
223                    "rename"
224                    "rmdir"
225                    "select"
226                    "send"
227                    "setitimer"
228                    "setpgrp"
229                    "setsid"
230                    "sinh"
231                    "socket"
232                    "stat"
233                    "strerror"
234                    "strlen"
235                    "symlink"
236                    "sync"
237                    "tanh"
238                    "tcdrain"
239                    "tcflow"
240                    "tcflush"
241                    "tcgetattr"
242                    "tcsendbreak"
243                    "tcsetattr"
244                    "truncate"
245                    "ttyname"
246                    "tzname"
247                    "unlink"
248                    "utimes"
249                    "wait3"      
250                    "write")
251                  ;; These aren't needed on the X86 because they're microcoded into the
252                  ;; FPU, so the Lisp VOPs can implement them directly without having to
253                  ;; call C code.
254                  ;;
255                  ;; Note: There might be some other functions in this category as well.
256                  ;; E.g. I notice tanh() and acos() in the list above.. -- WHN 2001-06-07
257                  #!-x86 
258                  '("sin"
259                    "cos" 
260                    "tan"      
261                    "atan" 
262                    "atan2" 
263                    "exp"      
264                    "log" 
265                    "log10" 
266                    "sqrt")
267                  #!+alpha
268                  '("ieee_get_fp_control"
269                    "ieee_set_fp_control")
270                  #!-darwin
271                  '("dlclose"
272                    "dlerror"
273                    "dlopen"
274                    "dlsym")
275                  #!+os-provides-dladdr 
276                  '("dladdr")
277                  #!-(and sparc sunos) ;; !defined(SVR4)
278                  '("sigsetmask")))
279
280 (with-open-file (f "src/runtime/ldso-stubs.S" :direction :output :if-exists :supersede)
281   (assert (= (length *preludes*) 2))
282   (dolist (pre *preludes*)
283     (write-line pre f))
284   (dolist (stub *stubs*)
285     (check-type stub string)
286     (ldso-stubify stub f)))
287