objcodes.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. #if HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <string.h>
  22. #include <fcntl.h>
  23. #include <unistd.h>
  24. #ifdef HAVE_SYS_MMAN_H
  25. #include <sys/mman.h>
  26. #endif
  27. #include <sys/stat.h>
  28. #include <sys/types.h>
  29. #include <assert.h>
  30. #include <alignof.h>
  31. #include <full-read.h>
  32. #include "_scm.h"
  33. #include "programs.h"
  34. #include "objcodes.h"
  35. /* SCM_OBJCODE_COOKIE, defined in _scm.h, is a magic value prepended
  36. to objcode on disk but not in memory.
  37. The length of the header must be a multiple of 8 bytes. */
  38. verify (((sizeof (SCM_OBJCODE_COOKIE) - 1) & 7) == 0);
  39. /*
  40. * Objcode type
  41. */
  42. static void
  43. verify_cookie (char *cookie, struct stat *st, int map_fd, void *map_addr)
  44. #define FUNC_NAME "make_objcode_from_file"
  45. {
  46. /* The cookie ends with a version of the form M.N, where M is the
  47. major version and N is the minor version. For this Guile to be
  48. able to load an objcode, M must be SCM_OBJCODE_MAJOR_VERSION, and N
  49. must be less than or equal to SCM_OBJCODE_MINOR_VERSION. Since N
  50. is the last character, we do a strict comparison on all but the
  51. last, then a <= on the last one. */
  52. if (memcmp (cookie, SCM_OBJCODE_COOKIE, strlen (SCM_OBJCODE_COOKIE) - 1))
  53. {
  54. SCM args = scm_list_1 (scm_from_latin1_stringn
  55. (cookie, strlen (SCM_OBJCODE_COOKIE)));
  56. if (map_fd >= 0)
  57. {
  58. (void) close (map_fd);
  59. #ifdef HAVE_SYS_MMAN_H
  60. (void) munmap (map_addr, st->st_size);
  61. #endif
  62. }
  63. scm_misc_error (FUNC_NAME, "bad header on object file: ~s", args);
  64. }
  65. {
  66. char minor_version = cookie[strlen (SCM_OBJCODE_COOKIE) - 1];
  67. if (minor_version > SCM_OBJCODE_MINOR_VERSION_STRING[0])
  68. {
  69. if (map_fd >= 0)
  70. {
  71. (void) close (map_fd);
  72. #ifdef HAVE_SYS_MMAN_H
  73. (void) munmap (map_addr, st->st_size);
  74. #endif
  75. }
  76. scm_misc_error (FUNC_NAME, "objcode minor version too new (~a > ~a)",
  77. scm_list_2 (scm_from_latin1_stringn (&minor_version, 1),
  78. scm_from_latin1_string
  79. (SCM_OBJCODE_MINOR_VERSION_STRING)));
  80. }
  81. }
  82. }
  83. #undef FUNC_NAME
  84. /* The words in an objcode SCM object are as follows:
  85. - scm_tc7_objcode | type | flags
  86. - the struct scm_objcode C object
  87. - the parent of this objcode: either another objcode, a bytevector,
  88. or, in the case of mmap types, file descriptors (as an inum)
  89. - "native code" -- not currently used.
  90. */
  91. static SCM
  92. make_objcode_from_file (int fd)
  93. #define FUNC_NAME "make_objcode_from_file"
  94. {
  95. int ret;
  96. /* The SCM_OBJCODE_COOKIE is a string literal, and thus has an extra
  97. trailing NUL, hence the - 1. */
  98. char cookie[sizeof (SCM_OBJCODE_COOKIE) - 1];
  99. struct stat st;
  100. ret = fstat (fd, &st);
  101. if (ret < 0)
  102. SCM_SYSERROR;
  103. if (st.st_size <= sizeof (struct scm_objcode) + sizeof cookie)
  104. scm_misc_error (FUNC_NAME, "object file too small (~a bytes)",
  105. scm_list_1 (SCM_I_MAKINUM (st.st_size)));
  106. #ifdef HAVE_SYS_MMAN_H
  107. {
  108. char *addr;
  109. struct scm_objcode *data;
  110. addr = mmap (0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  111. if (addr == MAP_FAILED)
  112. {
  113. int errno_save = errno;
  114. (void) close (fd);
  115. errno = errno_save;
  116. SCM_SYSERROR;
  117. }
  118. else
  119. {
  120. memcpy (cookie, addr, sizeof cookie);
  121. data = (struct scm_objcode *) (addr + sizeof cookie);
  122. }
  123. verify_cookie (cookie, &st, fd, addr);
  124. if (data->len + data->metalen
  125. != (st.st_size - sizeof (*data) - sizeof cookie))
  126. {
  127. size_t total_len = sizeof (*data) + data->len + data->metalen;
  128. (void) close (fd);
  129. (void) munmap (addr, st.st_size);
  130. scm_misc_error (FUNC_NAME, "bad length header (~a, ~a)",
  131. scm_list_2 (scm_from_size_t (st.st_size),
  132. scm_from_size_t (total_len)));
  133. }
  134. /* FIXME: we leak ourselves and the file descriptor. but then again so does
  135. dlopen(). */
  136. return scm_permanent_object
  137. (scm_double_cell (SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_MMAP, 0),
  138. (scm_t_bits)(addr + strlen (SCM_OBJCODE_COOKIE)),
  139. SCM_UNPACK (scm_from_int (fd)), 0));
  140. }
  141. #else
  142. {
  143. SCM bv = scm_c_make_bytevector (st.st_size - sizeof cookie);
  144. if (full_read (fd, cookie, sizeof cookie) != sizeof cookie
  145. || full_read (fd, SCM_BYTEVECTOR_CONTENTS (bv),
  146. SCM_BYTEVECTOR_LENGTH (bv)) != SCM_BYTEVECTOR_LENGTH (bv))
  147. {
  148. int errno_save = errno;
  149. (void) close (fd);
  150. errno = errno_save;
  151. SCM_SYSERROR;
  152. }
  153. (void) close (fd);
  154. verify_cookie (cookie, &st, -1, NULL);
  155. return scm_bytecode_to_objcode (bv);
  156. }
  157. #endif
  158. }
  159. #undef FUNC_NAME
  160. SCM
  161. scm_c_make_objcode_slice (SCM parent, const scm_t_uint8 *ptr)
  162. #define FUNC_NAME "make-objcode-slice"
  163. {
  164. const struct scm_objcode *data, *parent_data;
  165. const scm_t_uint8 *parent_base;
  166. SCM_VALIDATE_OBJCODE (1, parent);
  167. parent_data = SCM_OBJCODE_DATA (parent);
  168. parent_base = SCM_C_OBJCODE_BASE (parent_data);
  169. if (ptr < parent_base
  170. || ptr >= (parent_base + parent_data->len + parent_data->metalen
  171. - sizeof (struct scm_objcode)))
  172. scm_misc_error
  173. (FUNC_NAME, "offset out of bounds (~a vs ~a + ~a + ~a)",
  174. scm_list_4 (scm_from_unsigned_integer ((scm_t_bits) ptr),
  175. scm_from_unsigned_integer ((scm_t_bits) parent_base),
  176. scm_from_uint32 (parent_data->len),
  177. scm_from_uint32 (parent_data->metalen)));
  178. /* Make sure bytecode for the objcode-meta is suitable aligned. Failing to
  179. do so leads to SIGBUS/SIGSEGV on some arches (e.g., SPARC). */
  180. assert ((((scm_t_bits) ptr) &
  181. (alignof_type (struct scm_objcode) - 1UL)) == 0);
  182. data = (struct scm_objcode*) ptr;
  183. assert (SCM_C_OBJCODE_BASE (data) + data->len + data->metalen
  184. <= parent_base + parent_data->len + parent_data->metalen);
  185. return scm_double_cell (SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_SLICE, 0),
  186. (scm_t_bits)data, SCM_UNPACK (parent), 0);
  187. }
  188. #undef FUNC_NAME
  189. /*
  190. * Scheme interface
  191. */
  192. SCM_DEFINE (scm_objcode_p, "objcode?", 1, 0, 0,
  193. (SCM obj),
  194. "")
  195. #define FUNC_NAME s_scm_objcode_p
  196. {
  197. return scm_from_bool (SCM_OBJCODE_P (obj));
  198. }
  199. #undef FUNC_NAME
  200. SCM_DEFINE (scm_objcode_meta, "objcode-meta", 1, 0, 0,
  201. (SCM objcode),
  202. "")
  203. #define FUNC_NAME s_scm_objcode_meta
  204. {
  205. SCM_VALIDATE_OBJCODE (1, objcode);
  206. if (SCM_OBJCODE_META_LEN (objcode) == 0)
  207. return SCM_BOOL_F;
  208. else
  209. return scm_c_make_objcode_slice (objcode, (SCM_OBJCODE_BASE (objcode)
  210. + SCM_OBJCODE_LEN (objcode)));
  211. }
  212. #undef FUNC_NAME
  213. SCM_DEFINE (scm_bytecode_to_objcode, "bytecode->objcode", 1, 0, 0,
  214. (SCM bytecode),
  215. "")
  216. #define FUNC_NAME s_scm_bytecode_to_objcode
  217. {
  218. size_t size;
  219. const scm_t_uint8 *c_bytecode;
  220. struct scm_objcode *data;
  221. if (!scm_is_bytevector (bytecode))
  222. scm_wrong_type_arg (FUNC_NAME, 1, bytecode);
  223. size = SCM_BYTEVECTOR_LENGTH (bytecode);
  224. c_bytecode = (const scm_t_uint8*)SCM_BYTEVECTOR_CONTENTS (bytecode);
  225. SCM_ASSERT_RANGE (0, bytecode, size >= sizeof(struct scm_objcode));
  226. data = (struct scm_objcode*)c_bytecode;
  227. if (data->len + data->metalen != (size - sizeof (*data)))
  228. scm_misc_error (FUNC_NAME, "bad bytevector size (~a != ~a)",
  229. scm_list_2 (scm_from_size_t (size),
  230. scm_from_uint32 (sizeof (*data) + data->len + data->metalen)));
  231. /* foolishly, we assume that as long as bytecode is around, that c_bytecode
  232. will be of the same length; perhaps a bad assumption? */
  233. return scm_double_cell (SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_BYTEVECTOR, 0),
  234. (scm_t_bits)data, SCM_UNPACK (bytecode), 0);
  235. }
  236. #undef FUNC_NAME
  237. SCM_DEFINE (scm_load_objcode, "load-objcode", 1, 0, 0,
  238. (SCM file),
  239. "")
  240. #define FUNC_NAME s_scm_load_objcode
  241. {
  242. int fd;
  243. char *c_file;
  244. SCM_VALIDATE_STRING (1, file);
  245. c_file = scm_to_locale_string (file);
  246. fd = open (c_file, O_RDONLY);
  247. free (c_file);
  248. if (fd < 0) SCM_SYSERROR;
  249. return make_objcode_from_file (fd);
  250. }
  251. #undef FUNC_NAME
  252. SCM_DEFINE (scm_objcode_to_bytecode, "objcode->bytecode", 1, 0, 0,
  253. (SCM objcode),
  254. "")
  255. #define FUNC_NAME s_scm_objcode_to_bytecode
  256. {
  257. scm_t_uint32 len;
  258. SCM_VALIDATE_OBJCODE (1, objcode);
  259. len = sizeof (struct scm_objcode) + SCM_OBJCODE_TOTAL_LEN (objcode);
  260. return scm_c_take_bytevector ((scm_t_int8*)SCM_OBJCODE_DATA (objcode),
  261. len, objcode);
  262. }
  263. #undef FUNC_NAME
  264. SCM_DEFINE (scm_write_objcode, "write-objcode", 2, 0, 0,
  265. (SCM objcode, SCM port),
  266. "")
  267. #define FUNC_NAME s_scm_write_objcode
  268. {
  269. SCM_VALIDATE_OBJCODE (1, objcode);
  270. SCM_VALIDATE_OUTPUT_PORT (2, port);
  271. scm_c_write (port, SCM_OBJCODE_COOKIE, strlen (SCM_OBJCODE_COOKIE));
  272. scm_c_write (port, SCM_OBJCODE_DATA (objcode),
  273. sizeof (struct scm_objcode) + SCM_OBJCODE_TOTAL_LEN (objcode));
  274. return SCM_UNSPECIFIED;
  275. }
  276. #undef FUNC_NAME
  277. void
  278. scm_i_objcode_print (SCM objcode, SCM port, scm_print_state *pstate)
  279. {
  280. scm_puts ("#<objcode ", port);
  281. scm_uintprint ((scm_t_bits)SCM_OBJCODE_BASE (objcode), 16, port);
  282. scm_puts (">", port);
  283. }
  284. void
  285. scm_bootstrap_objcodes (void)
  286. {
  287. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  288. "scm_init_objcodes",
  289. (scm_t_extension_init_func)scm_init_objcodes, NULL);
  290. }
  291. /* Before, we used __BYTE_ORDER, but that is not defined on all
  292. systems. So punt and use automake, PDP endianness be damned. */
  293. #ifdef WORDS_BIGENDIAN
  294. #define SCM_BYTE_ORDER 4321
  295. #else
  296. #define SCM_BYTE_ORDER 1234
  297. #endif
  298. void
  299. scm_init_objcodes (void)
  300. {
  301. #ifndef SCM_MAGIC_SNARFER
  302. #include "libguile/objcodes.x"
  303. #endif
  304. scm_c_define ("word-size", scm_from_size_t (sizeof(SCM)));
  305. scm_c_define ("byte-order", scm_from_uint16 (SCM_BYTE_ORDER));
  306. }
  307. /*
  308. Local Variables:
  309. c-file-style: "gnu"
  310. End:
  311. */