objcodes.c 13 KB

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