Merge branch 'master' of git://git.savannah.gnu.org/guile into elisp
[bpt/guile.git] / libguile / objcodes.c
1 /* Copyright (C) 2001, 2009 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 #include <sys/mman.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <assert.h>
30
31 #include "_scm.h"
32 #include "vm-bootstrap.h"
33 #include "programs.h"
34 #include "objcodes.h"
35
36 /* SCM_OBJCODE_COOKIE is defined in _scm.h */
37 /* The length of the header must be a multiple of 8 bytes. */
38 verify (((sizeof (SCM_OBJCODE_COOKIE) - 1) & 7) == 0);
39
40 \f
41 /*
42 * Objcode type
43 */
44
45 scm_t_bits scm_tc16_objcode;
46
47 static SCM
48 make_objcode_by_mmap (int fd)
49 #define FUNC_NAME "make_objcode_by_mmap"
50 {
51 int ret;
52 char *addr;
53 struct stat st;
54 SCM sret = SCM_BOOL_F;
55 struct scm_objcode *data;
56
57 ret = fstat (fd, &st);
58 if (ret < 0)
59 SCM_SYSERROR;
60
61 if (st.st_size <= sizeof (struct scm_objcode) + strlen (SCM_OBJCODE_COOKIE))
62 scm_misc_error (FUNC_NAME, "object file too small (~a bytes)",
63 scm_list_1 (SCM_I_MAKINUM (st.st_size)));
64
65 addr = mmap (0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
66 if (addr == MAP_FAILED)
67 {
68 (void) close (fd);
69 SCM_SYSERROR;
70 }
71
72 if (memcmp (addr, SCM_OBJCODE_COOKIE, strlen (SCM_OBJCODE_COOKIE)))
73 {
74 SCM args = scm_list_1 (scm_from_locale_stringn
75 (addr, strlen (SCM_OBJCODE_COOKIE)));
76 (void) close (fd);
77 (void) munmap (addr, st.st_size);
78 scm_misc_error (FUNC_NAME, "bad header on object file: ~s", args);
79 }
80
81 data = (struct scm_objcode*)(addr + strlen (SCM_OBJCODE_COOKIE));
82
83 if (data->len + data->metalen != (st.st_size - sizeof (*data) - strlen (SCM_OBJCODE_COOKIE)))
84 {
85 (void) close (fd);
86 (void) munmap (addr, st.st_size);
87 scm_misc_error (FUNC_NAME, "bad length header (~a, ~a)",
88 scm_list_2 (scm_from_size_t (st.st_size),
89 scm_from_uint32 (sizeof (*data) + data->len
90 + data->metalen)));
91 }
92
93 SCM_NEWSMOB3 (sret, scm_tc16_objcode, addr + strlen (SCM_OBJCODE_COOKIE),
94 SCM_PACK (SCM_BOOL_F), fd);
95 SCM_SET_SMOB_FLAGS (sret, SCM_F_OBJCODE_IS_MMAP);
96
97 /* FIXME: we leak ourselves and the file descriptor. but then again so does
98 dlopen(). */
99 return scm_permanent_object (sret);
100 }
101 #undef FUNC_NAME
102
103 SCM
104 scm_c_make_objcode_slice (SCM parent, const scm_t_uint8 *ptr)
105 #define FUNC_NAME "make-objcode-slice"
106 {
107 const struct scm_objcode *data, *parent_data;
108 SCM ret;
109
110 SCM_VALIDATE_OBJCODE (1, parent);
111 parent_data = SCM_OBJCODE_DATA (parent);
112
113 if (ptr < parent_data->base
114 || ptr >= (parent_data->base + parent_data->len + parent_data->metalen
115 - sizeof (struct scm_objcode)))
116 scm_misc_error (FUNC_NAME, "offset out of bounds (~a vs ~a + ~a + ~a)",
117 scm_list_4 (scm_from_ulong ((unsigned long)ptr),
118 scm_from_ulong ((unsigned long)parent_data->base),
119 scm_from_uint32 (parent_data->len),
120 scm_from_uint32 (parent_data->metalen)));
121
122 #ifdef __GNUC__ /* we need `__alignof__' */
123 /* Make sure bytecode for the objcode-meta is suitable aligned. Failing to
124 do so leads to SIGBUS/SIGSEGV on some arches (e.g., SPARC). */
125 assert ((((scm_t_bits) ptr) & (__alignof__ (struct scm_objcode) - 1UL)) == 0);
126 #endif
127
128 data = (struct scm_objcode*)ptr;
129 if (data->base + data->len + data->metalen > parent_data->base + parent_data->len + parent_data->metalen)
130 abort ();
131
132 SCM_NEWSMOB2 (ret, scm_tc16_objcode, data, parent);
133 SCM_SET_SMOB_FLAGS (ret, SCM_F_OBJCODE_IS_SLICE);
134 return ret;
135 }
136 #undef FUNC_NAME
137
138 static SCM
139 objcode_mark (SCM obj)
140 {
141 return SCM_SMOB_OBJECT_2 (obj);
142 }
143
144 \f
145 /*
146 * Scheme interface
147 */
148
149 SCM_DEFINE (scm_objcode_p, "objcode?", 1, 0, 0,
150 (SCM obj),
151 "")
152 #define FUNC_NAME s_scm_objcode_p
153 {
154 return SCM_BOOL (SCM_OBJCODE_P (obj));
155 }
156 #undef FUNC_NAME
157
158 SCM_DEFINE (scm_objcode_meta, "objcode-meta", 1, 0, 0,
159 (SCM objcode),
160 "")
161 #define FUNC_NAME s_scm_objcode_meta
162 {
163 SCM_VALIDATE_OBJCODE (1, objcode);
164
165 if (SCM_OBJCODE_META_LEN (objcode) == 0)
166 return SCM_BOOL_F;
167 else
168 return scm_c_make_objcode_slice (objcode, (SCM_OBJCODE_BASE (objcode)
169 + SCM_OBJCODE_LEN (objcode)));
170 }
171 #undef FUNC_NAME
172
173 SCM_DEFINE (scm_bytecode_to_objcode, "bytecode->objcode", 1, 0, 0,
174 (SCM bytecode),
175 "")
176 #define FUNC_NAME s_scm_bytecode_to_objcode
177 {
178 size_t size;
179 ssize_t increment;
180 scm_t_array_handle handle;
181 const scm_t_uint8 *c_bytecode;
182 struct scm_objcode *data;
183 SCM objcode;
184
185 if (scm_is_false (scm_u8vector_p (bytecode)))
186 scm_wrong_type_arg (FUNC_NAME, 1, bytecode);
187
188 c_bytecode = scm_u8vector_elements (bytecode, &handle, &size, &increment);
189 data = (struct scm_objcode*)c_bytecode;
190 SCM_NEWSMOB2 (objcode, scm_tc16_objcode, data, bytecode);
191 scm_array_handle_release (&handle);
192
193 SCM_ASSERT_RANGE (0, bytecode, size >= sizeof(struct scm_objcode));
194 if (data->len + data->metalen != (size - sizeof (*data)))
195 scm_misc_error (FUNC_NAME, "bad u8vector size (~a != ~a)",
196 scm_list_2 (scm_from_size_t (size),
197 scm_from_uint32 (sizeof (*data) + data->len + data->metalen)));
198 assert (increment == 1);
199 SCM_SET_SMOB_FLAGS (objcode, SCM_F_OBJCODE_IS_U8VECTOR);
200
201 /* foolishly, we assume that as long as bytecode is around, that c_bytecode
202 will be of the same length; perhaps a bad assumption? */
203
204 return objcode;
205 }
206 #undef FUNC_NAME
207
208 SCM_DEFINE (scm_load_objcode, "load-objcode", 1, 0, 0,
209 (SCM file),
210 "")
211 #define FUNC_NAME s_scm_load_objcode
212 {
213 int fd;
214 char *c_file;
215
216 SCM_VALIDATE_STRING (1, file);
217
218 c_file = scm_to_locale_string (file);
219 fd = open (c_file, O_RDONLY);
220 free (c_file);
221 if (fd < 0) SCM_SYSERROR;
222
223 return make_objcode_by_mmap (fd);
224 }
225 #undef FUNC_NAME
226
227 SCM_DEFINE (scm_objcode_to_bytecode, "objcode->bytecode", 1, 0, 0,
228 (SCM objcode),
229 "")
230 #define FUNC_NAME s_scm_objcode_to_bytecode
231 {
232 scm_t_uint8 *u8vector;
233 scm_t_uint32 len;
234
235 SCM_VALIDATE_OBJCODE (1, objcode);
236
237 len = sizeof(struct scm_objcode) + SCM_OBJCODE_TOTAL_LEN (objcode);
238 /* FIXME: Is `gc_malloc' ok here? */
239 u8vector = scm_gc_malloc (len, "objcode-u8vector");
240 memcpy (u8vector, SCM_OBJCODE_DATA (objcode), len);
241
242 return scm_take_u8vector (u8vector, len);
243 }
244 #undef FUNC_NAME
245
246 SCM_DEFINE (scm_write_objcode, "write-objcode", 2, 0, 0,
247 (SCM objcode, SCM port),
248 "")
249 #define FUNC_NAME s_scm_write_objcode
250 {
251 SCM_VALIDATE_OBJCODE (1, objcode);
252 SCM_VALIDATE_OUTPUT_PORT (2, port);
253
254 scm_c_write (port, SCM_OBJCODE_COOKIE, strlen (SCM_OBJCODE_COOKIE));
255 scm_c_write (port, SCM_OBJCODE_DATA (objcode),
256 sizeof (struct scm_objcode) + SCM_OBJCODE_TOTAL_LEN (objcode));
257
258 return SCM_UNSPECIFIED;
259 }
260 #undef FUNC_NAME
261
262 \f
263 void
264 scm_bootstrap_objcodes (void)
265 {
266 scm_tc16_objcode = scm_make_smob_type ("objcode", 0);
267 scm_set_smob_mark (scm_tc16_objcode, objcode_mark);
268 scm_c_register_extension ("libguile", "scm_init_objcodes",
269 (scm_t_extension_init_func)scm_init_objcodes, NULL);
270 }
271
272 /* Before, we used __BYTE_ORDER, but that is not defined on all
273 systems. So punt and use automake, PDP endianness be damned. */
274 #ifdef WORDS_BIGENDIAN
275 #define SCM_BYTE_ORDER 4321
276 #else
277 #define SCM_BYTE_ORDER 1234
278 #endif
279
280 void
281 scm_init_objcodes (void)
282 {
283 scm_bootstrap_vm ();
284
285 #ifndef SCM_MAGIC_SNARFER
286 #include "libguile/objcodes.x"
287 #endif
288
289 scm_c_define ("word-size", scm_from_size_t (sizeof(SCM)));
290 scm_c_define ("byte-order", scm_from_uint16 (SCM_BYTE_ORDER));
291 }
292
293 /*
294 Local Variables:
295 c-file-style: "gnu"
296 End:
297 */