1 /* Copyright (C) 2001, 2009, 2010, 2011 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 #ifdef HAVE_SYS_MMAN_H
32 #include <sys/types.h>
36 #include <full-read.h>
42 /* SCM_OBJCODE_COOKIE, defined in _scm.h, is a magic value prepended
43 to objcode on disk but not in memory.
45 The length of the header must be a multiple of 8 bytes. */
46 verify (((sizeof (SCM_OBJCODE_COOKIE
) - 1) & 7) == 0);
54 verify_cookie (char *cookie
, struct stat
*st
, int map_fd
, void *map_addr
)
55 #define FUNC_NAME "make_objcode_from_file"
57 /* The cookie ends with a version of the form M.N, where M is the
58 major version and N is the minor version. For this Guile to be
59 able to load an objcode, M must be SCM_OBJCODE_MAJOR_VERSION, and N
60 must be less than or equal to SCM_OBJCODE_MINOR_VERSION. Since N
61 is the last character, we do a strict comparison on all but the
62 last, then a <= on the last one. */
63 if (memcmp (cookie
, SCM_OBJCODE_COOKIE
, strlen (SCM_OBJCODE_COOKIE
) - 1))
65 SCM args
= scm_list_1 (scm_from_latin1_stringn
66 (cookie
, strlen (SCM_OBJCODE_COOKIE
)));
69 (void) close (map_fd
);
70 #ifdef HAVE_SYS_MMAN_H
71 (void) munmap (map_addr
, st
->st_size
);
74 scm_misc_error (FUNC_NAME
, "bad header on object file: ~s", args
);
78 char minor_version
= cookie
[strlen (SCM_OBJCODE_COOKIE
) - 1];
80 if (minor_version
> SCM_OBJCODE_MINOR_VERSION_STRING
[0])
84 (void) close (map_fd
);
85 #ifdef HAVE_SYS_MMAN_H
86 (void) munmap (map_addr
, st
->st_size
);
90 scm_misc_error (FUNC_NAME
, "objcode minor version too new (~a > ~a)",
91 scm_list_2 (scm_from_latin1_stringn (&minor_version
, 1),
92 scm_from_latin1_string
93 (SCM_OBJCODE_MINOR_VERSION_STRING
)));
99 /* The words in an objcode SCM object are as follows:
100 - scm_tc7_objcode | type | flags
101 - the struct scm_objcode C object
102 - the parent of this objcode: either another objcode, a bytevector,
103 or, in the case of mmap types, file descriptors (as an inum)
104 - "native code" -- not currently used.
108 make_objcode_from_file (int fd
)
109 #define FUNC_NAME "make_objcode_from_file"
112 /* The SCM_OBJCODE_COOKIE is a string literal, and thus has an extra
113 trailing NUL, hence the - 1. */
114 char cookie
[sizeof (SCM_OBJCODE_COOKIE
) - 1];
117 ret
= fstat (fd
, &st
);
121 if (st
.st_size
<= sizeof (struct scm_objcode
) + sizeof cookie
)
122 scm_misc_error (FUNC_NAME
, "object file too small (~a bytes)",
123 scm_list_1 (SCM_I_MAKINUM (st
.st_size
)));
125 #ifdef HAVE_SYS_MMAN_H
128 struct scm_objcode
*data
;
130 addr
= mmap (0, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
132 if (addr
== MAP_FAILED
)
134 int errno_save
= errno
;
141 memcpy (cookie
, addr
, sizeof cookie
);
142 data
= (struct scm_objcode
*) (addr
+ sizeof cookie
);
145 verify_cookie (cookie
, &st
, fd
, addr
);
148 if (data
->len
+ data
->metalen
149 != (st
.st_size
- sizeof (*data
) - sizeof cookie
))
151 size_t total_len
= sizeof (*data
) + data
->len
+ data
->metalen
;
154 (void) munmap (addr
, st
.st_size
);
156 scm_misc_error (FUNC_NAME
, "bad length header (~a, ~a)",
157 scm_list_2 (scm_from_size_t (st
.st_size
),
158 scm_from_size_t (total_len
)));
161 /* FIXME: we leak ourselves and the file descriptor. but then again so does
163 return scm_permanent_object
164 (scm_double_cell (SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_MMAP
, 0),
165 (scm_t_bits
)(addr
+ strlen (SCM_OBJCODE_COOKIE
)),
166 SCM_UNPACK (scm_from_int (fd
)), 0));
170 SCM bv
= scm_c_make_bytevector (st
.st_size
- sizeof cookie
);
172 if (full_read (fd
, cookie
, sizeof cookie
) != sizeof cookie
173 || full_read (fd
, SCM_BYTEVECTOR_CONTENTS (bv
),
174 SCM_BYTEVECTOR_LENGTH (bv
)) != SCM_BYTEVECTOR_LENGTH (bv
))
176 int errno_save
= errno
;
184 verify_cookie (cookie
, &st
, -1, NULL
);
186 return scm_bytecode_to_objcode (bv
);
194 scm_c_make_objcode_slice (SCM parent
, const scm_t_uint8
*ptr
)
195 #define FUNC_NAME "make-objcode-slice"
197 const struct scm_objcode
*data
, *parent_data
;
198 const scm_t_uint8
*parent_base
;
200 SCM_VALIDATE_OBJCODE (1, parent
);
201 parent_data
= SCM_OBJCODE_DATA (parent
);
202 parent_base
= SCM_C_OBJCODE_BASE (parent_data
);
204 if (ptr
< parent_base
205 || ptr
>= (parent_base
+ parent_data
->len
+ parent_data
->metalen
206 - sizeof (struct scm_objcode
)))
208 (FUNC_NAME
, "offset out of bounds (~a vs ~a + ~a + ~a)",
209 scm_list_4 (scm_from_unsigned_integer ((scm_t_bits
) ptr
),
210 scm_from_unsigned_integer ((scm_t_bits
) parent_base
),
211 scm_from_uint32 (parent_data
->len
),
212 scm_from_uint32 (parent_data
->metalen
)));
214 /* Make sure bytecode for the objcode-meta is suitable aligned. Failing to
215 do so leads to SIGBUS/SIGSEGV on some arches (e.g., SPARC). */
216 assert ((((scm_t_bits
) ptr
) &
217 (alignof_type (struct scm_objcode
) - 1UL)) == 0);
219 data
= (struct scm_objcode
*) ptr
;
220 assert (SCM_C_OBJCODE_BASE (data
) + data
->len
+ data
->metalen
221 <= parent_base
+ parent_data
->len
+ parent_data
->metalen
);
223 return scm_double_cell (SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_SLICE
, 0),
224 (scm_t_bits
)data
, SCM_UNPACK (parent
), 0);
233 SCM_DEFINE (scm_objcode_p
, "objcode?", 1, 0, 0,
236 #define FUNC_NAME s_scm_objcode_p
238 return scm_from_bool (SCM_OBJCODE_P (obj
));
242 SCM_DEFINE (scm_objcode_meta
, "objcode-meta", 1, 0, 0,
245 #define FUNC_NAME s_scm_objcode_meta
247 SCM_VALIDATE_OBJCODE (1, objcode
);
249 if (SCM_OBJCODE_META_LEN (objcode
) == 0)
252 return scm_c_make_objcode_slice (objcode
, (SCM_OBJCODE_BASE (objcode
)
253 + SCM_OBJCODE_LEN (objcode
)));
257 SCM_DEFINE (scm_bytecode_to_objcode
, "bytecode->objcode", 1, 0, 0,
260 #define FUNC_NAME s_scm_bytecode_to_objcode
263 const scm_t_uint8
*c_bytecode
;
264 struct scm_objcode
*data
;
266 if (!scm_is_bytevector (bytecode
))
267 scm_wrong_type_arg (FUNC_NAME
, 1, bytecode
);
269 size
= SCM_BYTEVECTOR_LENGTH (bytecode
);
270 c_bytecode
= (const scm_t_uint8
*)SCM_BYTEVECTOR_CONTENTS (bytecode
);
272 SCM_ASSERT_RANGE (0, bytecode
, size
>= sizeof(struct scm_objcode
));
273 data
= (struct scm_objcode
*)c_bytecode
;
275 if (data
->len
+ data
->metalen
!= (size
- sizeof (*data
)))
276 scm_misc_error (FUNC_NAME
, "bad bytevector size (~a != ~a)",
277 scm_list_2 (scm_from_size_t (size
),
278 scm_from_uint32 (sizeof (*data
) + data
->len
+ data
->metalen
)));
280 /* foolishly, we assume that as long as bytecode is around, that c_bytecode
281 will be of the same length; perhaps a bad assumption? */
282 return scm_double_cell (SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_BYTEVECTOR
, 0),
283 (scm_t_bits
)data
, SCM_UNPACK (bytecode
), 0);
287 SCM_DEFINE (scm_load_objcode
, "load-objcode", 1, 0, 0,
290 #define FUNC_NAME s_scm_load_objcode
295 SCM_VALIDATE_STRING (1, file
);
297 c_file
= scm_to_locale_string (file
);
298 fd
= open (c_file
, O_RDONLY
);
300 if (fd
< 0) SCM_SYSERROR
;
302 return make_objcode_from_file (fd
);
306 SCM_DEFINE (scm_objcode_to_bytecode
, "objcode->bytecode", 1, 0, 0,
309 #define FUNC_NAME s_scm_objcode_to_bytecode
311 scm_t_int8
*s8vector
;
314 SCM_VALIDATE_OBJCODE (1, objcode
);
316 len
= sizeof (struct scm_objcode
) + SCM_OBJCODE_TOTAL_LEN (objcode
);
318 s8vector
= scm_malloc (len
);
319 memcpy (s8vector
, SCM_OBJCODE_DATA (objcode
), len
);
321 return scm_c_take_bytevector (s8vector
, len
);
325 SCM_DEFINE (scm_write_objcode
, "write-objcode", 2, 0, 0,
326 (SCM objcode
, SCM port
),
328 #define FUNC_NAME s_scm_write_objcode
330 SCM_VALIDATE_OBJCODE (1, objcode
);
331 SCM_VALIDATE_OUTPUT_PORT (2, port
);
333 scm_c_write (port
, SCM_OBJCODE_COOKIE
, strlen (SCM_OBJCODE_COOKIE
));
334 scm_c_write (port
, SCM_OBJCODE_DATA (objcode
),
335 sizeof (struct scm_objcode
) + SCM_OBJCODE_TOTAL_LEN (objcode
));
337 return SCM_UNSPECIFIED
;
342 scm_i_objcode_print (SCM objcode
, SCM port
, scm_print_state
*pstate
)
344 scm_puts ("#<objcode ", port
);
345 scm_uintprint ((scm_t_bits
)SCM_OBJCODE_BASE (objcode
), 16, port
);
346 scm_puts (">", port
);
351 scm_bootstrap_objcodes (void)
353 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION
,
355 (scm_t_extension_init_func
)scm_init_objcodes
, NULL
);
358 /* Before, we used __BYTE_ORDER, but that is not defined on all
359 systems. So punt and use automake, PDP endianness be damned. */
360 #ifdef WORDS_BIGENDIAN
361 #define SCM_BYTE_ORDER 4321
363 #define SCM_BYTE_ORDER 1234
367 scm_init_objcodes (void)
369 #ifndef SCM_MAGIC_SNARFER
370 #include "libguile/objcodes.x"
373 scm_c_define ("word-size", scm_from_size_t (sizeof(SCM
)));
374 scm_c_define ("byte-order", scm_from_uint16 (SCM_BYTE_ORDER
));