| 209 | | ) ;; #+(or linux bsd svr4) |
| | 209 | ) ;; #+(or linux (and bsd (not darwin)) svr4) |
| | 210 | |
| | 211 | |
| | 212 | |
| | 213 | ;; Darwin loading of foreign code. This uses the dlopen shims and thus |
| | 214 | ;; appears like ELF to the rest of the code in this file. However testing |
| | 215 | ;; for shared libs obviously needs to test for Mach-O dylibs, and not |
| | 216 | ;; ELF shared libraries... |
| | 217 | #+darwin |
| | 218 | (progn |
| | 219 | |
| | 220 | (alien:def-alien-type machheader |
| | 221 | (alien:struct nil |
| | 222 | (magic (alien:unsigned 32)) |
| | 223 | (cputype (alien:signed 32)) |
| | 224 | (cpusubtype (alien:signed 32)) |
| | 225 | (filetype (alien:unsigned 32)) |
| | 226 | (ncmds (alien:unsigned 32)) |
| | 227 | (sizeofcmds (alien:unsigned 32)) |
| | 228 | (flags (alien:unsigned 32)))) |
| | 229 | |
| | 230 | ;; values for magic |
| | 231 | (defconstant mh-magic #xfeedface) |
| | 232 | |
| | 233 | ;; values for filetype |
| | 234 | (defconstant mh-object #x1) |
| | 235 | (defconstant mh-execute #x2) |
| | 236 | (defconstant mh-fvmlib #x3) |
| | 237 | (defconstant mh-core #x4) |
| | 238 | (defconstant mh-preload #x5) |
| | 239 | (defconstant mh-dylib #x6) |
| | 240 | (defconstant mh-dylinker #x7) |
| | 241 | (defconstant mh-bundle #x8) |
| | 242 | (defconstant mh-dylib-stub #x9) |
| | 243 | |
| | 244 | (defun mach-o-p (h) |
| | 245 | "Make sure the header starts with the mach-o magic value." |
| | 246 | (eql (alien:slot h 'magic) mh-magic)) |
| | 247 | |
| | 248 | (defun file-shared-library-p (pathname) |
| | 249 | (with-open-file (obj pathname |
| | 250 | :direction :input |
| | 251 | :element-type '(unsigned-byte 8)) |
| | 252 | (let ((fd (lisp::fd-stream-fd obj))) |
| | 253 | (alien:with-alien ((header machheader)) |
| | 254 | (unix:unix-read fd (alien:alien-sap header) |
| | 255 | (alien:alien-size machheader :bytes)) |
| | 256 | (when (mach-o-p header) |
| | 257 | (or (eql mh-dylib (alien:slot header 'filetype)) |
| | 258 | (eql mh-bundle (alien:slot header 'filetype)))))))) |
| | 259 | ) ; #+darwin |