objcodes.c 8.0 KB

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