Merge branch 'master' into boehm-demers-weiser-gc
[bpt/guile.git] / libguile / objcodes.c
index 231d2f4..33ba296 100644 (file)
@@ -1,43 +1,20 @@
-/* Copyright (C) 2001 Free Software Foundation, Inc.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2, or (at your option)
- * any later version.
- * 
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+/* Copyright (C) 2001, 2009 Free Software Foundation, Inc.
  * 
- * You should have received a copy of the GNU General Public License
- * along with this software; see the file COPYING.  If not, write to
- * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
- * Boston, MA 02111-1307 USA
- *
- * As a special exception, the Free Software Foundation gives permission
- * for additional uses of the text contained in its release of GUILE.
- *
- * The exception is that, if you link the GUILE library with other files
- * to produce an executable, this does not by itself cause the
- * resulting executable to be covered by the GNU General Public License.
- * Your use of that executable is in no way restricted on account of
- * linking the GUILE library code into it.
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3 of
+ * the License, or (at your option) any later version.
  *
- * This exception does not however invalidate any other reasons why
- * the executable file might be covered by the GNU General Public License.
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
  *
- * This exception applies only to the code released by the
- * Free Software Foundation under the name GUILE.  If you copy
- * code from other Free Software Foundation releases into a copy of
- * GUILE, as the General Public License permits, the exception does
- * not apply to the code that you add in this way.  To avoid misleading
- * anyone as to the status of such modified files, you must delete
- * this exception notice from them.
- *
- * If you write modifications of your own for GUILE, it is your choice
- * whether to permit this exception to apply to your modifications.
- * If you do not wish that, delete this exception notice.  */
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
 
 #if HAVE_CONFIG_H
 #  include <config.h>
 #include <sys/types.h>
 #include <assert.h>
 
+#include "_scm.h"
 #include "vm-bootstrap.h"
 #include "programs.h"
 #include "objcodes.h"
 
-/* nb, the length of the header should be a multiple of 8 bytes */
-#define OBJCODE_COOKIE "GOOF-0.5"
+/* SCM_OBJCODE_COOKIE is defined in _scm.h */
+/* The length of the header must be a multiple of 8 bytes.  */
+verify (((sizeof (SCM_OBJCODE_COOKIE) - 1) & 7) == 0);
 
 \f
 /*
@@ -79,25 +58,39 @@ make_objcode_by_mmap (int fd)
   if (ret < 0)
     SCM_SYSERROR;
 
-  if (st.st_size <= sizeof (struct scm_objcode) + strlen (OBJCODE_COOKIE))
+  if (st.st_size <= sizeof (struct scm_objcode) + strlen (SCM_OBJCODE_COOKIE))
     scm_misc_error (FUNC_NAME, "object file too small (~a bytes)",
-                   SCM_LIST1 (SCM_I_MAKINUM (st.st_size)));
+                   scm_list_1 (SCM_I_MAKINUM (st.st_size)));
 
   addr = mmap (0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
   if (addr == MAP_FAILED)
-    SCM_SYSERROR;
-
-  if (memcmp (addr, OBJCODE_COOKIE, strlen (OBJCODE_COOKIE)))
-    SCM_SYSERROR;
-
-  data = (struct scm_objcode*)(addr + strlen (OBJCODE_COOKIE));
-
-  if (data->len + data->metalen != (st.st_size - sizeof (*data) - strlen (OBJCODE_COOKIE)))
-    scm_misc_error (FUNC_NAME, "bad length header (~a, ~a)",
-                   SCM_LIST2 (scm_from_size_t (st.st_size),
-                               scm_from_uint32 (sizeof (*data) + data->len + data->metalen)));
-
-  SCM_NEWSMOB3 (sret, scm_tc16_objcode, addr + strlen (OBJCODE_COOKIE),
+    {
+      (void) close (fd);
+      SCM_SYSERROR;
+    }
+
+  if (memcmp (addr, SCM_OBJCODE_COOKIE, strlen (SCM_OBJCODE_COOKIE)))
+    {
+      SCM args = scm_list_1 (scm_from_locale_stringn
+                             (addr, strlen (SCM_OBJCODE_COOKIE)));
+      (void) close (fd);
+      (void) munmap (addr, st.st_size);
+      scm_misc_error (FUNC_NAME, "bad header on object file: ~s", args);
+    }
+
+  data = (struct scm_objcode*)(addr + strlen (SCM_OBJCODE_COOKIE));
+
+  if (data->len + data->metalen != (st.st_size - sizeof (*data) - strlen (SCM_OBJCODE_COOKIE)))
+    {
+      (void) close (fd);
+      (void) munmap (addr, st.st_size);
+      scm_misc_error (FUNC_NAME, "bad length header (~a, ~a)",
+                     scm_list_2 (scm_from_size_t (st.st_size),
+                                 scm_from_uint32 (sizeof (*data) + data->len
+                                                  + data->metalen)));
+    }
+
+  SCM_NEWSMOB3 (sret, scm_tc16_objcode, addr + strlen (SCM_OBJCODE_COOKIE),
                 SCM_PACK (SCM_BOOL_F), fd);
   SCM_SET_SMOB_FLAGS (sret, SCM_F_OBJCODE_IS_MMAP);
 
@@ -108,10 +101,10 @@ make_objcode_by_mmap (int fd)
 #undef FUNC_NAME
 
 SCM
-scm_c_make_objcode_slice (SCM parent, scm_t_uint8 *ptr)
+scm_c_make_objcode_slice (SCM parent, const scm_t_uint8 *ptr)
 #define FUNC_NAME "make-objcode-slice"
 {
-  struct scm_objcode *data, *parent_data;
+  const struct scm_objcode *data, *parent_data;
   SCM ret;
 
   SCM_VALIDATE_OBJCODE (1, parent);
@@ -121,10 +114,16 @@ scm_c_make_objcode_slice (SCM parent, scm_t_uint8 *ptr)
       || ptr >= (parent_data->base + parent_data->len + parent_data->metalen
                  - sizeof (struct scm_objcode)))
     scm_misc_error (FUNC_NAME, "offset out of bounds (~a vs ~a + ~a + ~a)",
-                   SCM_LIST4 (scm_from_ulong ((unsigned long)ptr),
-                               scm_from_ulong ((unsigned long)parent_data->base),
-                               scm_from_uint32 (parent_data->len),
-                               scm_from_uint32 (parent_data->metalen)));
+                   scm_list_4 (scm_from_ulong ((unsigned long)ptr),
+                               scm_from_ulong ((unsigned long)parent_data->base),
+                               scm_from_uint32 (parent_data->len),
+                               scm_from_uint32 (parent_data->metalen)));
+
+#ifdef __GNUC__ /* we need `__alignof__' */
+  /* Make sure bytecode for the objcode-meta is suitable aligned.  Failing to
+     do so leads to SIGBUS/SIGSEGV on some arches (e.g., SPARC).  */
+  assert ((((scm_t_bits) ptr) & (__alignof__ (struct scm_objcode) - 1UL)) == 0);
+#endif
 
   data = (struct scm_objcode*)ptr;
   if (data->base + data->len + data->metalen > parent_data->base + parent_data->len + parent_data->metalen)
@@ -188,8 +187,8 @@ SCM_DEFINE (scm_bytecode_to_objcode, "bytecode->objcode", 1, 0, 0,
   SCM_ASSERT_RANGE (0, bytecode, size >= sizeof(struct scm_objcode));
   if (data->len + data->metalen != (size - sizeof (*data)))
     scm_misc_error (FUNC_NAME, "bad u8vector size (~a != ~a)",
-                   SCM_LIST2 (scm_from_size_t (size),
-                               scm_from_uint32 (sizeof (*data) + data->len + data->metalen)));
+                   scm_list_2 (scm_from_size_t (size),
+                               scm_from_uint32 (sizeof (*data) + data->len + data->metalen)));
   assert (increment == 1);
   SCM_SET_SMOB_FLAGS (objcode, SCM_F_OBJCODE_IS_U8VECTOR);
   
@@ -246,7 +245,7 @@ SCM_DEFINE (scm_write_objcode, "write-objcode", 2, 0, 0,
   SCM_VALIDATE_OBJCODE (1, objcode);
   SCM_VALIDATE_OUTPUT_PORT (2, port);
   
-  scm_c_write (port, OBJCODE_COOKIE, strlen (OBJCODE_COOKIE));
+  scm_c_write (port, SCM_OBJCODE_COOKIE, strlen (SCM_OBJCODE_COOKIE));
   scm_c_write (port, SCM_OBJCODE_DATA (objcode),
                sizeof (struct scm_objcode) + SCM_OBJCODE_TOTAL_LEN (objcode));
 
@@ -259,6 +258,8 @@ void
 scm_bootstrap_objcodes (void)
 {
   scm_tc16_objcode = scm_make_smob_type ("objcode", 0);
+  scm_c_register_extension ("libguile", "scm_init_objcodes",
+                            (scm_t_extension_init_func)scm_init_objcodes, NULL);
 }
 
 /* Before, we used __BYTE_ORDER, but that is not defined on all