fix scm_protects deprecation warning
[bpt/guile.git] / libguile / objcodes.c
1 /* Copyright (C) 2001, 2009, 2010, 2011 Free Software Foundation, Inc.
2 *
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.
7 *
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.
12 *
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
16 * 02110-1301 USA
17 */
18
19 #if HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <string.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26
27 #ifdef HAVE_SYS_MMAN_H
28 #include <sys/mman.h>
29 #endif
30
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <assert.h>
34 #include <alignof.h>
35 #include <byteswap.h>
36
37 #include <full-read.h>
38
39 #include "_scm.h"
40 #include "programs.h"
41 #include "objcodes.h"
42
43 /* SCM_OBJCODE_COOKIE, defined in _scm.h, is a magic value prepended
44 to objcode on disk but not in memory.
45
46 The length of the header must be a multiple of 8 bytes. */
47 verify (((sizeof (SCM_OBJCODE_COOKIE) - 1) & 7) == 0);
48
49 /* Endianness and word size of the compilation target. */
50 static SCM target_endianness_var = SCM_BOOL_F;
51 static SCM target_word_size_var = SCM_BOOL_F;
52
53 \f
54 /*
55 * Objcode type
56 */
57
58 /* Endianness of the build machine. */
59 #ifdef WORDS_BIGENDIAN
60 # define NATIVE_ENDIANNESS 'B'
61 #else
62 # define NATIVE_ENDIANNESS 'L'
63 #endif
64
65 /* Return the endianness of the compilation target. */
66 static char
67 target_endianness (void)
68 {
69 if (scm_is_true (target_endianness_var))
70 return scm_is_eq (scm_call_0 (scm_variable_ref (target_endianness_var)),
71 scm_endianness_big) ? 'B' : 'L';
72 else
73 return NATIVE_ENDIANNESS;
74 }
75
76 /* Return the word size in bytes of the compilation target. */
77 static size_t
78 target_word_size (void)
79 {
80 if (scm_is_true (target_word_size_var))
81 return scm_to_size_t (scm_call_0
82 (scm_variable_ref (target_word_size_var)));
83 else
84 return sizeof (void *);
85 }
86
87 /* Convert X, which is in byte order ENDIANNESS, to its native
88 representation. */
89 static inline uint32_t
90 to_native_order (uint32_t x, char endianness)
91 {
92 if (endianness == NATIVE_ENDIANNESS)
93 return x;
94 else
95 return bswap_32 (x);
96 }
97
98 static void
99 verify_cookie (char *cookie, struct stat *st, int map_fd, void *map_addr)
100 #define FUNC_NAME "make_objcode_from_file"
101 {
102 /* The cookie ends with a version of the form M.N, where M is the
103 major version and N is the minor version. For this Guile to be
104 able to load an objcode, M must be SCM_OBJCODE_MAJOR_VERSION, and N
105 must be less than or equal to SCM_OBJCODE_MINOR_VERSION. Since N
106 is the last character, we do a strict comparison on all but the
107 last, then a <= on the last one. */
108 if (memcmp (cookie, SCM_OBJCODE_COOKIE, strlen (SCM_OBJCODE_COOKIE) - 1))
109 {
110 SCM args = scm_list_1 (scm_from_latin1_stringn
111 (cookie, strlen (SCM_OBJCODE_COOKIE)));
112 if (map_fd >= 0)
113 {
114 (void) close (map_fd);
115 #ifdef HAVE_SYS_MMAN_H
116 (void) munmap (map_addr, st->st_size);
117 #endif
118 }
119 scm_misc_error (FUNC_NAME, "bad header on object file: ~s", args);
120 }
121
122 {
123 char minor_version = cookie[strlen (SCM_OBJCODE_COOKIE) - 1];
124
125 if (minor_version > SCM_OBJCODE_MINOR_VERSION_STRING[0])
126 {
127 if (map_fd >= 0)
128 {
129 (void) close (map_fd);
130 #ifdef HAVE_SYS_MMAN_H
131 (void) munmap (map_addr, st->st_size);
132 #endif
133 }
134
135 scm_misc_error (FUNC_NAME, "objcode minor version too new (~a > ~a)",
136 scm_list_2 (scm_from_latin1_stringn (&minor_version, 1),
137 scm_from_latin1_string
138 (SCM_OBJCODE_MINOR_VERSION_STRING)));
139 }
140 }
141 }
142 #undef FUNC_NAME
143
144 /* The words in an objcode SCM object are as follows:
145 - scm_tc7_objcode | type | flags
146 - the struct scm_objcode C object
147 - the parent of this objcode: either another objcode, a bytevector,
148 or, in the case of mmap types, file descriptors (as an inum)
149 - "native code" -- not currently used.
150 */
151
152 static SCM
153 make_objcode_from_file (int fd)
154 #define FUNC_NAME "make_objcode_from_file"
155 {
156 int ret;
157 /* The SCM_OBJCODE_COOKIE is a string literal, and thus has an extra
158 trailing NUL, hence the - 1. */
159 char cookie[sizeof (SCM_OBJCODE_COOKIE) - 1];
160 struct stat st;
161
162 ret = fstat (fd, &st);
163 if (ret < 0)
164 SCM_SYSERROR;
165
166 if (st.st_size <= sizeof (struct scm_objcode) + sizeof cookie)
167 scm_misc_error (FUNC_NAME, "object file too small (~a bytes)",
168 scm_list_1 (SCM_I_MAKINUM (st.st_size)));
169
170 #ifdef HAVE_SYS_MMAN_H
171 {
172 char *addr;
173 struct scm_objcode *data;
174
175 addr = mmap (0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
176
177 if (addr == MAP_FAILED)
178 {
179 int errno_save = errno;
180 (void) close (fd);
181 errno = errno_save;
182 SCM_SYSERROR;
183 }
184 else
185 {
186 memcpy (cookie, addr, sizeof cookie);
187 data = (struct scm_objcode *) (addr + sizeof cookie);
188 }
189
190 verify_cookie (cookie, &st, fd, addr);
191
192
193 if (data->len + data->metalen
194 != (st.st_size - sizeof (*data) - sizeof cookie))
195 {
196 size_t total_len = sizeof (*data) + data->len + data->metalen;
197
198 (void) close (fd);
199 (void) munmap (addr, st.st_size);
200
201 scm_misc_error (FUNC_NAME, "bad length header (~a, ~a)",
202 scm_list_2 (scm_from_size_t (st.st_size),
203 scm_from_size_t (total_len)));
204 }
205
206 /* FIXME: we leak ourselves and the file descriptor. but then again so does
207 dlopen(). */
208 return scm_permanent_object
209 (scm_double_cell (SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_MMAP, 0),
210 (scm_t_bits)(addr + strlen (SCM_OBJCODE_COOKIE)),
211 SCM_UNPACK (scm_from_int (fd)), 0));
212 }
213 #else
214 {
215 SCM bv = scm_c_make_bytevector (st.st_size - sizeof cookie);
216
217 if (full_read (fd, cookie, sizeof cookie) != sizeof cookie
218 || full_read (fd, SCM_BYTEVECTOR_CONTENTS (bv),
219 SCM_BYTEVECTOR_LENGTH (bv)) != SCM_BYTEVECTOR_LENGTH (bv))
220 {
221 int errno_save = errno;
222 (void) close (fd);
223 errno = errno_save;
224 SCM_SYSERROR;
225 }
226
227 (void) close (fd);
228
229 verify_cookie (cookie, &st, -1, NULL);
230
231 return scm_bytecode_to_native_objcode (bv);
232 }
233 #endif
234 }
235 #undef FUNC_NAME
236
237
238 SCM
239 scm_c_make_objcode_slice (SCM parent, const scm_t_uint8 *ptr)
240 #define FUNC_NAME "make-objcode-slice"
241 {
242 const struct scm_objcode *data, *parent_data;
243 const scm_t_uint8 *parent_base;
244
245 SCM_VALIDATE_OBJCODE (1, parent);
246 parent_data = SCM_OBJCODE_DATA (parent);
247 parent_base = SCM_C_OBJCODE_BASE (parent_data);
248
249 if (ptr < parent_base
250 || ptr >= (parent_base + parent_data->len + parent_data->metalen
251 - sizeof (struct scm_objcode)))
252 scm_misc_error
253 (FUNC_NAME, "offset out of bounds (~a vs ~a + ~a + ~a)",
254 scm_list_4 (scm_from_unsigned_integer ((scm_t_bits) ptr),
255 scm_from_unsigned_integer ((scm_t_bits) parent_base),
256 scm_from_uint32 (parent_data->len),
257 scm_from_uint32 (parent_data->metalen)));
258
259 /* Make sure bytecode for the objcode-meta is suitable aligned. Failing to
260 do so leads to SIGBUS/SIGSEGV on some arches (e.g., SPARC). */
261 assert ((((scm_t_bits) ptr) &
262 (alignof_type (struct scm_objcode) - 1UL)) == 0);
263
264 data = (struct scm_objcode*) ptr;
265 assert (SCM_C_OBJCODE_BASE (data) + data->len + data->metalen
266 <= parent_base + parent_data->len + parent_data->metalen);
267
268 return scm_double_cell (SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_SLICE, 0),
269 (scm_t_bits)data, SCM_UNPACK (parent), 0);
270 }
271 #undef FUNC_NAME
272
273 \f
274 /*
275 * Scheme interface
276 */
277
278 SCM_DEFINE (scm_objcode_p, "objcode?", 1, 0, 0,
279 (SCM obj),
280 "")
281 #define FUNC_NAME s_scm_objcode_p
282 {
283 return scm_from_bool (SCM_OBJCODE_P (obj));
284 }
285 #undef FUNC_NAME
286
287 SCM_DEFINE (scm_objcode_meta, "objcode-meta", 1, 0, 0,
288 (SCM objcode),
289 "")
290 #define FUNC_NAME s_scm_objcode_meta
291 {
292 SCM_VALIDATE_OBJCODE (1, objcode);
293
294 if (SCM_OBJCODE_META_LEN (objcode) == 0)
295 return SCM_BOOL_F;
296 else
297 return scm_c_make_objcode_slice (objcode, (SCM_OBJCODE_BASE (objcode)
298 + SCM_OBJCODE_LEN (objcode)));
299 }
300 #undef FUNC_NAME
301
302 /* Turn BYTECODE into objcode encoded for ENDIANNESS and WORD_SIZE. */
303 static SCM
304 bytecode_to_objcode (SCM bytecode, char endianness, size_t word_size)
305 #define FUNC_NAME "bytecode->objcode"
306 {
307 size_t size, len, metalen;
308 const scm_t_uint8 *c_bytecode;
309 struct scm_objcode *data;
310
311 if (!scm_is_bytevector (bytecode))
312 scm_wrong_type_arg (FUNC_NAME, 1, bytecode);
313
314 size = SCM_BYTEVECTOR_LENGTH (bytecode);
315 c_bytecode = (const scm_t_uint8*)SCM_BYTEVECTOR_CONTENTS (bytecode);
316
317 SCM_ASSERT_RANGE (0, bytecode, size >= sizeof(struct scm_objcode));
318 data = (struct scm_objcode*)c_bytecode;
319
320 len = to_native_order (data->len, endianness);
321 metalen = to_native_order (data->metalen, endianness);
322
323 if (len + metalen != (size - sizeof (*data)))
324 scm_misc_error (FUNC_NAME, "bad bytevector size (~a != ~a)",
325 scm_list_2 (scm_from_size_t (size),
326 scm_from_uint32 (sizeof (*data) + len + metalen)));
327
328 /* foolishly, we assume that as long as bytecode is around, that c_bytecode
329 will be of the same length; perhaps a bad assumption? */
330 return scm_double_cell (SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_BYTEVECTOR, 0),
331 (scm_t_bits)data, SCM_UNPACK (bytecode), 0);
332 }
333 #undef FUNC_NAME
334
335 SCM_DEFINE (scm_bytecode_to_objcode, "bytecode->objcode", 1, 0, 0,
336 (SCM bytecode),
337 "")
338 #define FUNC_NAME s_scm_bytecode_to_objcode
339 {
340 /* Assume we're called from Scheme, which known that to do with
341 `target-type'. */
342 return bytecode_to_objcode (bytecode, target_endianness (),
343 target_word_size ());
344 }
345 #undef FUNC_NAME
346
347 /* Like `bytecode->objcode', but ignore the `target-type' fluid. This
348 is useful for native compilation that happens lazily---e.g., direct
349 calls to this function from libguile itself. */
350 SCM
351 scm_bytecode_to_native_objcode (SCM bytecode)
352 {
353 return bytecode_to_objcode (bytecode, NATIVE_ENDIANNESS, sizeof (void *));
354 }
355
356 SCM_DEFINE (scm_load_objcode, "load-objcode", 1, 0, 0,
357 (SCM file),
358 "")
359 #define FUNC_NAME s_scm_load_objcode
360 {
361 int fd;
362 char *c_file;
363
364 SCM_VALIDATE_STRING (1, file);
365
366 c_file = scm_to_locale_string (file);
367 fd = open (c_file, O_RDONLY | O_CLOEXEC);
368 free (c_file);
369 if (fd < 0) SCM_SYSERROR;
370
371 return make_objcode_from_file (fd);
372 }
373 #undef FUNC_NAME
374
375 SCM_DEFINE (scm_objcode_to_bytecode, "objcode->bytecode", 1, 0, 0,
376 (SCM objcode),
377 "")
378 #define FUNC_NAME s_scm_objcode_to_bytecode
379 {
380 scm_t_int8 *s8vector;
381 scm_t_uint32 len;
382
383 SCM_VALIDATE_OBJCODE (1, objcode);
384
385 len = sizeof (struct scm_objcode) + SCM_OBJCODE_TOTAL_LEN (objcode);
386
387 s8vector = scm_gc_malloc_pointerless (len, FUNC_NAME);
388 memcpy (s8vector, SCM_OBJCODE_DATA (objcode), len);
389
390 return scm_c_take_gc_bytevector (s8vector, len);
391 }
392 #undef FUNC_NAME
393
394 SCM_DEFINE (scm_write_objcode, "write-objcode", 2, 0, 0,
395 (SCM objcode, SCM port),
396 "")
397 #define FUNC_NAME s_scm_write_objcode
398 {
399 char cookie[sizeof (SCM_OBJCODE_COOKIE) - 1];
400 char endianness, word_size;
401 size_t total_size;
402
403 SCM_VALIDATE_OBJCODE (1, objcode);
404 SCM_VALIDATE_OUTPUT_PORT (2, port);
405 endianness = target_endianness ();
406 switch (target_word_size ())
407 {
408 case 4:
409 word_size = '4';
410 break;
411 case 8:
412 word_size = '8';
413 break;
414 default:
415 abort ();
416 }
417
418 memcpy (cookie, SCM_OBJCODE_COOKIE, strlen (SCM_OBJCODE_COOKIE));
419 cookie[SCM_OBJCODE_ENDIANNESS_OFFSET] = endianness;
420 cookie[SCM_OBJCODE_WORD_SIZE_OFFSET] = word_size;
421
422 total_size =
423 to_native_order (SCM_OBJCODE_LEN (objcode), target_endianness ())
424 + to_native_order (SCM_OBJCODE_META_LEN (objcode), target_endianness ());
425
426 scm_c_write (port, cookie, strlen (SCM_OBJCODE_COOKIE));
427 scm_c_write (port, SCM_OBJCODE_DATA (objcode),
428 sizeof (struct scm_objcode) + total_size);
429
430 return SCM_UNSPECIFIED;
431 }
432 #undef FUNC_NAME
433
434 void
435 scm_i_objcode_print (SCM objcode, SCM port, scm_print_state *pstate)
436 {
437 scm_puts ("#<objcode ", port);
438 scm_uintprint ((scm_t_bits)SCM_OBJCODE_BASE (objcode), 16, port);
439 scm_puts (">", port);
440 }
441
442 \f
443 void
444 scm_bootstrap_objcodes (void)
445 {
446 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
447 "scm_init_objcodes",
448 (scm_t_extension_init_func)scm_init_objcodes, NULL);
449 }
450
451 /* Before, we used __BYTE_ORDER, but that is not defined on all
452 systems. So punt and use automake, PDP endianness be damned. */
453 #ifdef WORDS_BIGENDIAN
454 #define SCM_BYTE_ORDER 4321
455 #else
456 #define SCM_BYTE_ORDER 1234
457 #endif
458
459 void
460 scm_init_objcodes (void)
461 {
462 #ifndef SCM_MAGIC_SNARFER
463 #include "libguile/objcodes.x"
464 #endif
465
466 scm_c_define ("word-size", scm_from_size_t (sizeof(SCM)));
467 scm_c_define ("byte-order", scm_from_uint16 (SCM_BYTE_ORDER));
468
469 target_endianness_var = scm_c_public_variable ("system base target",
470 "target-endianness");
471 target_word_size_var = scm_c_public_variable ("system base target",
472 "target-word-size");
473 }
474
475 /*
476 Local Variables:
477 c-file-style: "gnu"
478 End:
479 */