Fix "occurrances" typos in getopt-long code and test
[bpt/guile.git] / libguile / objcodes.c
CommitLineData
4914fe19 1/* Copyright (C) 2001, 2009, 2010, 2011 Free Software Foundation, Inc.
8f5cfc81 2 *
560b9c25 3 * This library is free software; you can redistribute it and/or
53befeb7
NJ
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.
8f5cfc81 7 *
53befeb7
NJ
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
560b9c25
AW
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
8f5cfc81 12 *
560b9c25
AW
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
53befeb7
NJ
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
560b9c25 17 */
8f5cfc81 18
13c47753
AW
19#if HAVE_CONFIG_H
20# include <config.h>
21#endif
22
8f5cfc81
KN
23#include <string.h>
24#include <fcntl.h>
25#include <unistd.h>
13a78b0f
AW
26
27#ifdef HAVE_SYS_MMAN_H
8f5cfc81 28#include <sys/mman.h>
13a78b0f
AW
29#endif
30
8f5cfc81
KN
31#include <sys/stat.h>
32#include <sys/types.h>
054599f1 33#include <assert.h>
1119e493 34#include <alignof.h>
8f5cfc81 35
13a78b0f
AW
36#include <full-read.h>
37
560b9c25 38#include "_scm.h"
8f5cfc81
KN
39#include "programs.h"
40#include "objcodes.h"
41
4b02bf47
AW
42/* SCM_OBJCODE_COOKIE, defined in _scm.h, is a magic value prepended
43 to objcode on disk but not in memory.
44
45 The length of the header must be a multiple of 8 bytes. */
86cfb42d 46verify (((sizeof (SCM_OBJCODE_COOKIE) - 1) & 7) == 0);
8f5cfc81
KN
47
48\f
49/*
50 * Objcode type
51 */
52
13a78b0f
AW
53static void
54verify_cookie (char *cookie, struct stat *st, int map_fd, void *map_addr)
55#define FUNC_NAME "make_objcode_from_file"
56{
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))
64 {
65 SCM args = scm_list_1 (scm_from_latin1_stringn
66 (cookie, strlen (SCM_OBJCODE_COOKIE)));
67 if (map_fd >= 0)
68 {
69 (void) close (map_fd);
70#ifdef HAVE_SYS_MMAN_H
71 (void) munmap (map_addr, st->st_size);
72#endif
73 }
74 scm_misc_error (FUNC_NAME, "bad header on object file: ~s", args);
75 }
76
77 {
78 char minor_version = cookie[strlen (SCM_OBJCODE_COOKIE) - 1];
79
80 if (minor_version > SCM_OBJCODE_MINOR_VERSION_STRING[0])
81 {
82 if (map_fd >= 0)
83 {
84 (void) close (map_fd);
85#ifdef HAVE_SYS_MMAN_H
86 (void) munmap (map_addr, st->st_size);
87#endif
88 }
89
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)));
94 }
95 }
96}
97#undef FUNC_NAME
98
4b02bf47 99/* The words in an objcode SCM object are as follows:
6ce3666f 100 - scm_tc7_objcode | type | flags
4b02bf47 101 - the struct scm_objcode C object
6ce3666f
AW
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.
4b02bf47
AW
105 */
106
8f5cfc81 107static SCM
13a78b0f
AW
108make_objcode_from_file (int fd)
109#define FUNC_NAME "make_objcode_from_file"
8f5cfc81
KN
110{
111 int ret;
13a78b0f
AW
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];
8f5cfc81 115 struct stat st;
8f5cfc81
KN
116
117 ret = fstat (fd, &st);
0b5f0e49 118 if (ret < 0)
62082959 119 SCM_SYSERROR;
8f5cfc81 120
13a78b0f 121 if (st.st_size <= sizeof (struct scm_objcode) + sizeof cookie)
0b5f0e49 122 scm_misc_error (FUNC_NAME, "object file too small (~a bytes)",
da8b4747 123 scm_list_1 (SCM_I_MAKINUM (st.st_size)));
0b5f0e49 124
13a78b0f 125#ifdef HAVE_SYS_MMAN_H
e8ab529d 126 {
13a78b0f
AW
127 char *addr;
128 struct scm_objcode *data;
129
130 addr = mmap (0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
131
132 if (addr == MAP_FAILED)
133 {
134 int errno_save = errno;
135 (void) close (fd);
136 errno = errno_save;
137 SCM_SYSERROR;
138 }
139 else
140 {
141 memcpy (cookie, addr, sizeof cookie);
142 data = (struct scm_objcode *) (addr + sizeof cookie);
143 }
144
145 verify_cookie (cookie, &st, fd, addr);
146
147
148 if (data->len + data->metalen
149 != (st.st_size - sizeof (*data) - sizeof cookie))
150 {
151 size_t total_len = sizeof (*data) + data->len + data->metalen;
152
153 (void) close (fd);
154 (void) munmap (addr, st.st_size);
155
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)));
159 }
160
161 /* FIXME: we leak ourselves and the file descriptor. but then again so does
162 dlopen(). */
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));
e8ab529d 167 }
13a78b0f
AW
168#else
169 {
170 SCM bv = scm_c_make_bytevector (st.st_size - sizeof cookie);
e8ab529d 171
13a78b0f
AW
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))
175 {
176 int errno_save = errno;
177 (void) close (fd);
178 errno = errno_save;
179 SCM_SYSERROR;
180 }
53e28ed9 181
13a78b0f 182 (void) close (fd);
53e28ed9 183
13a78b0f 184 verify_cookie (cookie, &st, -1, NULL);
53e28ed9 185
13a78b0f
AW
186 return scm_bytecode_to_objcode (bv);
187 }
188#endif
8f5cfc81
KN
189}
190#undef FUNC_NAME
191
13a78b0f 192
53e28ed9 193SCM
b67cb286 194scm_c_make_objcode_slice (SCM parent, const scm_t_uint8 *ptr)
53e28ed9 195#define FUNC_NAME "make-objcode-slice"
8f5cfc81 196{
b67cb286 197 const struct scm_objcode *data, *parent_data;
3dbbe28d 198 const scm_t_uint8 *parent_base;
53e28ed9
AW
199
200 SCM_VALIDATE_OBJCODE (1, parent);
201 parent_data = SCM_OBJCODE_DATA (parent);
3dbbe28d
LC
202 parent_base = SCM_C_OBJCODE_BASE (parent_data);
203
204 if (ptr < parent_base
205 || ptr >= (parent_base + parent_data->len + parent_data->metalen
53e28ed9 206 - sizeof (struct scm_objcode)))
3d27ef4b
AW
207 scm_misc_error
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)));
53e28ed9 213
e3c9c676
LC
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). */
1119e493
LC
216 assert ((((scm_t_bits) ptr) &
217 (alignof_type (struct scm_objcode) - 1UL)) == 0);
53e28ed9 218
3dbbe28d
LC
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);
53e28ed9 222
f9654187 223 return scm_double_cell (SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_SLICE, 0),
6f3b0cc2 224 (scm_t_bits)data, SCM_UNPACK (parent), 0);
8f5cfc81
KN
225}
226#undef FUNC_NAME
227
228\f
229/*
230 * Scheme interface
231 */
232
233SCM_DEFINE (scm_objcode_p, "objcode?", 1, 0, 0,
234 (SCM obj),
235 "")
236#define FUNC_NAME s_scm_objcode_p
237{
5c8cefe5 238 return scm_from_bool (SCM_OBJCODE_P (obj));
8f5cfc81
KN
239}
240#undef FUNC_NAME
241
1f1ec13b
AW
242SCM_DEFINE (scm_objcode_meta, "objcode-meta", 1, 0, 0,
243 (SCM objcode),
244 "")
245#define FUNC_NAME s_scm_objcode_meta
246{
247 SCM_VALIDATE_OBJCODE (1, objcode);
248
249 if (SCM_OBJCODE_META_LEN (objcode) == 0)
250 return SCM_BOOL_F;
251 else
252 return scm_c_make_objcode_slice (objcode, (SCM_OBJCODE_BASE (objcode)
253 + SCM_OBJCODE_LEN (objcode)));
254}
255#undef FUNC_NAME
256
53e28ed9
AW
257SCM_DEFINE (scm_bytecode_to_objcode, "bytecode->objcode", 1, 0, 0,
258 (SCM bytecode),
8f5cfc81
KN
259 "")
260#define FUNC_NAME s_scm_bytecode_to_objcode
261{
262 size_t size;
b6368dbb 263 const scm_t_uint8 *c_bytecode;
53e28ed9 264 struct scm_objcode *data;
8f5cfc81 265
a2689737 266 if (!scm_is_bytevector (bytecode))
054599f1 267 scm_wrong_type_arg (FUNC_NAME, 1, bytecode);
8f5cfc81 268
a2689737
AW
269 size = SCM_BYTEVECTOR_LENGTH (bytecode);
270 c_bytecode = (const scm_t_uint8*)SCM_BYTEVECTOR_CONTENTS (bytecode);
271
272 SCM_ASSERT_RANGE (0, bytecode, size >= sizeof(struct scm_objcode));
53e28ed9 273 data = (struct scm_objcode*)c_bytecode;
ac47d5f6 274
ac47d5f6 275 if (data->len + data->metalen != (size - sizeof (*data)))
a2689737 276 scm_misc_error (FUNC_NAME, "bad bytevector size (~a != ~a)",
da8b4747
LC
277 scm_list_2 (scm_from_size_t (size),
278 scm_from_uint32 (sizeof (*data) + data->len + data->metalen)));
a2689737 279
53e28ed9
AW
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? */
f9654187 282 return scm_double_cell (SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_BYTEVECTOR, 0),
6f3b0cc2 283 (scm_t_bits)data, SCM_UNPACK (bytecode), 0);
8f5cfc81
KN
284}
285#undef FUNC_NAME
286
287SCM_DEFINE (scm_load_objcode, "load-objcode", 1, 0, 0,
288 (SCM file),
289 "")
290#define FUNC_NAME s_scm_load_objcode
291{
292 int fd;
2d80426a 293 char *c_file;
8f5cfc81
KN
294
295 SCM_VALIDATE_STRING (1, file);
296
2d80426a
LC
297 c_file = scm_to_locale_string (file);
298 fd = open (c_file, O_RDONLY);
299 free (c_file);
8f5cfc81
KN
300 if (fd < 0) SCM_SYSERROR;
301
13a78b0f 302 return make_objcode_from_file (fd);
8f5cfc81
KN
303}
304#undef FUNC_NAME
305
9bb8012d 306SCM_DEFINE (scm_objcode_to_bytecode, "objcode->bytecode", 1, 0, 0,
8f5cfc81
KN
307 (SCM objcode),
308 "")
9bb8012d 309#define FUNC_NAME s_scm_objcode_to_bytecode
8f5cfc81 310{
a2689737 311 scm_t_int8 *s8vector;
53e28ed9 312 scm_t_uint32 len;
054599f1 313
8f5cfc81 314 SCM_VALIDATE_OBJCODE (1, objcode);
054599f1 315
9a690dfb 316 len = sizeof (struct scm_objcode) + SCM_OBJCODE_TOTAL_LEN (objcode);
d7e7a02a 317
a2689737
AW
318 s8vector = scm_malloc (len);
319 memcpy (s8vector, SCM_OBJCODE_DATA (objcode), len);
054599f1 320
a2689737 321 return scm_c_take_bytevector (s8vector, len);
8f5cfc81
KN
322}
323#undef FUNC_NAME
324
53e28ed9
AW
325SCM_DEFINE (scm_write_objcode, "write-objcode", 2, 0, 0,
326 (SCM objcode, SCM port),
8f5cfc81 327 "")
53e28ed9 328#define FUNC_NAME s_scm_write_objcode
8f5cfc81 329{
8f5cfc81 330 SCM_VALIDATE_OBJCODE (1, objcode);
53e28ed9
AW
331 SCM_VALIDATE_OUTPUT_PORT (2, port);
332
86cfb42d 333 scm_c_write (port, SCM_OBJCODE_COOKIE, strlen (SCM_OBJCODE_COOKIE));
53e28ed9 334 scm_c_write (port, SCM_OBJCODE_DATA (objcode),
1f1ec13b 335 sizeof (struct scm_objcode) + SCM_OBJCODE_TOTAL_LEN (objcode));
53e28ed9
AW
336
337 return SCM_UNSPECIFIED;
8f5cfc81
KN
338}
339#undef FUNC_NAME
340
6f3b0cc2
AW
341void
342scm_i_objcode_print (SCM objcode, SCM port, scm_print_state *pstate)
343{
344 scm_puts ("#<objcode ", port);
345 scm_uintprint ((scm_t_bits)SCM_OBJCODE_BASE (objcode), 16, port);
346 scm_puts (">", port);
347}
348
8f5cfc81
KN
349\f
350void
07e56b27 351scm_bootstrap_objcodes (void)
8f5cfc81 352{
44602b08
AW
353 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
354 "scm_init_objcodes",
60ae5ca2 355 (scm_t_extension_init_func)scm_init_objcodes, NULL);
07e56b27
AW
356}
357
8992a9e3
AW
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
362#else
363#define SCM_BYTE_ORDER 1234
364#endif
365
07e56b27
AW
366void
367scm_init_objcodes (void)
368{
8f5cfc81 369#ifndef SCM_MAGIC_SNARFER
aeeff258 370#include "libguile/objcodes.x"
8f5cfc81 371#endif
53e28ed9
AW
372
373 scm_c_define ("word-size", scm_from_size_t (sizeof(SCM)));
8992a9e3 374 scm_c_define ("byte-order", scm_from_uint16 (SCM_BYTE_ORDER));
8f5cfc81
KN
375}
376
377/*
378 Local Variables:
379 c-file-style: "gnu"
380 End:
381*/