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
28 #include <sys/types.h>
36 /* SCM_OBJCODE_COOKIE, defined in _scm.h, is a magic value prepended
37 to objcode on disk but not in memory.
39 The length of the header must be a multiple of 8 bytes. */
40 verify (((sizeof (SCM_OBJCODE_COOKIE
) - 1) & 7) == 0);
47 /* The words in an objcode SCM object are as follows:
48 - scm_tc7_objcode | type | flags
49 - the struct scm_objcode C object
50 - the parent of this objcode: either another objcode, a bytevector,
51 or, in the case of mmap types, file descriptors (as an inum)
52 - "native code" -- not currently used.
56 make_objcode_by_mmap (int fd
)
57 #define FUNC_NAME "make_objcode_by_mmap"
62 SCM sret
= SCM_BOOL_F
;
63 struct scm_objcode
*data
;
65 ret
= fstat (fd
, &st
);
69 if (st
.st_size
<= sizeof (struct scm_objcode
) + strlen (SCM_OBJCODE_COOKIE
))
70 scm_misc_error (FUNC_NAME
, "object file too small (~a bytes)",
71 scm_list_1 (SCM_I_MAKINUM (st
.st_size
)));
73 addr
= mmap (0, st
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
74 if (addr
== MAP_FAILED
)
80 /* The cookie ends with a version of the form M.N, where M is the
81 major version and N is the minor version. For this Guile to be
82 able to load an objcode, M must be SCM_OBJCODE_MAJOR_VERSION, and N
83 must be less than or equal to SCM_OBJCODE_MINOR_VERSION. Since N
84 is the last character, we do a strict comparison on all but the
85 last, then a <= on the last one. */
86 if (memcmp (addr
, SCM_OBJCODE_COOKIE
, strlen (SCM_OBJCODE_COOKIE
) - 1))
88 SCM args
= scm_list_1 (scm_from_latin1_stringn
89 (addr
, strlen (SCM_OBJCODE_COOKIE
)));
91 (void) munmap (addr
, st
.st_size
);
92 scm_misc_error (FUNC_NAME
, "bad header on object file: ~s", args
);
96 char minor_version
= addr
[strlen (SCM_OBJCODE_COOKIE
) - 1];
98 if (minor_version
> SCM_OBJCODE_MINOR_VERSION_STRING
[0])
99 scm_misc_error (FUNC_NAME
, "objcode minor version too new (~a > ~a)",
100 scm_list_2 (scm_from_latin1_stringn (&minor_version
, 1),
101 scm_from_latin1_string
102 (SCM_OBJCODE_MINOR_VERSION_STRING
)));
105 data
= (struct scm_objcode
*)(addr
+ strlen (SCM_OBJCODE_COOKIE
));
107 if (data
->len
+ data
->metalen
!= (st
.st_size
- sizeof (*data
) - strlen (SCM_OBJCODE_COOKIE
)))
110 (void) munmap (addr
, st
.st_size
);
111 scm_misc_error (FUNC_NAME
, "bad length header (~a, ~a)",
112 scm_list_2 (scm_from_size_t (st
.st_size
),
113 scm_from_uint32 (sizeof (*data
) + data
->len
117 sret
= scm_double_cell (SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_MMAP
, 0),
118 (scm_t_bits
)(addr
+ strlen (SCM_OBJCODE_COOKIE
)),
119 SCM_UNPACK (scm_from_int (fd
)), 0);
121 /* FIXME: we leak ourselves and the file descriptor. but then again so does
123 return scm_permanent_object (sret
);
128 scm_c_make_objcode_slice (SCM parent
, const scm_t_uint8
*ptr
)
129 #define FUNC_NAME "make-objcode-slice"
131 const struct scm_objcode
*data
, *parent_data
;
132 const scm_t_uint8
*parent_base
;
134 SCM_VALIDATE_OBJCODE (1, parent
);
135 parent_data
= SCM_OBJCODE_DATA (parent
);
136 parent_base
= SCM_C_OBJCODE_BASE (parent_data
);
138 if (ptr
< parent_base
139 || ptr
>= (parent_base
+ parent_data
->len
+ parent_data
->metalen
140 - sizeof (struct scm_objcode
)))
142 (FUNC_NAME
, "offset out of bounds (~a vs ~a + ~a + ~a)",
143 scm_list_4 (scm_from_unsigned_integer ((scm_t_bits
) ptr
),
144 scm_from_unsigned_integer ((scm_t_bits
) parent_base
),
145 scm_from_uint32 (parent_data
->len
),
146 scm_from_uint32 (parent_data
->metalen
)));
148 /* Make sure bytecode for the objcode-meta is suitable aligned. Failing to
149 do so leads to SIGBUS/SIGSEGV on some arches (e.g., SPARC). */
150 assert ((((scm_t_bits
) ptr
) &
151 (alignof_type (struct scm_objcode
) - 1UL)) == 0);
153 data
= (struct scm_objcode
*) ptr
;
154 assert (SCM_C_OBJCODE_BASE (data
) + data
->len
+ data
->metalen
155 <= parent_base
+ parent_data
->len
+ parent_data
->metalen
);
157 return scm_double_cell (SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_SLICE
, 0),
158 (scm_t_bits
)data
, SCM_UNPACK (parent
), 0);
167 SCM_DEFINE (scm_objcode_p
, "objcode?", 1, 0, 0,
170 #define FUNC_NAME s_scm_objcode_p
172 return scm_from_bool (SCM_OBJCODE_P (obj
));
176 SCM_DEFINE (scm_objcode_meta
, "objcode-meta", 1, 0, 0,
179 #define FUNC_NAME s_scm_objcode_meta
181 SCM_VALIDATE_OBJCODE (1, objcode
);
183 if (SCM_OBJCODE_META_LEN (objcode
) == 0)
186 return scm_c_make_objcode_slice (objcode
, (SCM_OBJCODE_BASE (objcode
)
187 + SCM_OBJCODE_LEN (objcode
)));
191 SCM_DEFINE (scm_bytecode_to_objcode
, "bytecode->objcode", 1, 0, 0,
194 #define FUNC_NAME s_scm_bytecode_to_objcode
197 const scm_t_uint8
*c_bytecode
;
198 struct scm_objcode
*data
;
200 if (!scm_is_bytevector (bytecode
))
201 scm_wrong_type_arg (FUNC_NAME
, 1, bytecode
);
203 size
= SCM_BYTEVECTOR_LENGTH (bytecode
);
204 c_bytecode
= (const scm_t_uint8
*)SCM_BYTEVECTOR_CONTENTS (bytecode
);
206 SCM_ASSERT_RANGE (0, bytecode
, size
>= sizeof(struct scm_objcode
));
207 data
= (struct scm_objcode
*)c_bytecode
;
209 if (data
->len
+ data
->metalen
!= (size
- sizeof (*data
)))
210 scm_misc_error (FUNC_NAME
, "bad bytevector size (~a != ~a)",
211 scm_list_2 (scm_from_size_t (size
),
212 scm_from_uint32 (sizeof (*data
) + data
->len
+ data
->metalen
)));
214 /* foolishly, we assume that as long as bytecode is around, that c_bytecode
215 will be of the same length; perhaps a bad assumption? */
216 return scm_double_cell (SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_BYTEVECTOR
, 0),
217 (scm_t_bits
)data
, SCM_UNPACK (bytecode
), 0);
221 SCM_DEFINE (scm_load_objcode
, "load-objcode", 1, 0, 0,
224 #define FUNC_NAME s_scm_load_objcode
229 SCM_VALIDATE_STRING (1, file
);
231 c_file
= scm_to_locale_string (file
);
232 fd
= open (c_file
, O_RDONLY
);
234 if (fd
< 0) SCM_SYSERROR
;
236 return make_objcode_by_mmap (fd
);
240 SCM_DEFINE (scm_objcode_to_bytecode
, "objcode->bytecode", 1, 0, 0,
243 #define FUNC_NAME s_scm_objcode_to_bytecode
245 scm_t_int8
*s8vector
;
248 SCM_VALIDATE_OBJCODE (1, objcode
);
250 len
= sizeof (struct scm_objcode
) + SCM_OBJCODE_TOTAL_LEN (objcode
);
252 s8vector
= scm_malloc (len
);
253 memcpy (s8vector
, SCM_OBJCODE_DATA (objcode
), len
);
255 return scm_c_take_bytevector (s8vector
, len
);
259 SCM_DEFINE (scm_write_objcode
, "write-objcode", 2, 0, 0,
260 (SCM objcode
, SCM port
),
262 #define FUNC_NAME s_scm_write_objcode
264 SCM_VALIDATE_OBJCODE (1, objcode
);
265 SCM_VALIDATE_OUTPUT_PORT (2, port
);
267 scm_c_write (port
, SCM_OBJCODE_COOKIE
, strlen (SCM_OBJCODE_COOKIE
));
268 scm_c_write (port
, SCM_OBJCODE_DATA (objcode
),
269 sizeof (struct scm_objcode
) + SCM_OBJCODE_TOTAL_LEN (objcode
));
271 return SCM_UNSPECIFIED
;
276 scm_i_objcode_print (SCM objcode
, SCM port
, scm_print_state
*pstate
)
278 scm_puts ("#<objcode ", port
);
279 scm_uintprint ((scm_t_bits
)SCM_OBJCODE_BASE (objcode
), 16, port
);
280 scm_puts (">", port
);
285 scm_bootstrap_objcodes (void)
287 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION
,
289 (scm_t_extension_init_func
)scm_init_objcodes
, NULL
);
292 /* Before, we used __BYTE_ORDER, but that is not defined on all
293 systems. So punt and use automake, PDP endianness be damned. */
294 #ifdef WORDS_BIGENDIAN
295 #define SCM_BYTE_ORDER 4321
297 #define SCM_BYTE_ORDER 1234
301 scm_init_objcodes (void)
303 #ifndef SCM_MAGIC_SNARFER
304 #include "libguile/objcodes.x"
307 scm_c_define ("word-size", scm_from_size_t (sizeof(SCM
)));
308 scm_c_define ("byte-order", scm_from_uint16 (SCM_BYTE_ORDER
));