Update copyright notices for 2013.
[bpt/emacs.git] / src / unexmacosx.c
index a61bbd8..8d4e636 100644 (file)
@@ -1,5 +1,5 @@
 /* Dump Emacs in Mach-O format for use on Mac OS X.
-   Copyright (C) 2001-2012 Free Software Foundation, Inc.
+   Copyright (C) 2001-2013 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -117,6 +117,13 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <assert.h>
 
+/* LC_DATA_IN_CODE is not defined in mach-o/loader.h on OS X 10.7.
+   But it is used if we build with "Command Line Tools for Xcode 4.5
+   (OS X Lion) - September 2012".  */
+#ifndef LC_DATA_IN_CODE
+#define LC_DATA_IN_CODE 0x29 /* table of non-instructions in __text */
+#endif
+
 #ifdef _LP64
 #define mach_header                    mach_header_64
 #define segment_command                        segment_command_64
@@ -197,8 +204,6 @@ static off_t data_segment_old_fileoff = 0;
 
 static struct segment_command *data_segment_scp;
 
-static void unexec_error (const char *format, ...) NO_RETURN;
-
 /* Read N bytes from infd into memory starting at address DEST.
    Return true if successful, false otherwise.  */
 static int
@@ -275,7 +280,7 @@ unexec_copy (off_t dest, off_t src, ssize_t count)
 
 /* Debugging and informational messages routines.  */
 
-static void
+static _Noreturn void
 unexec_error (const char *format, ...)
 {
   va_list ap;
@@ -396,7 +401,7 @@ build_region_list (void)
        }
       else
        {
-         r = (struct region_t *) malloc (sizeof (struct region_t));
+         r = malloc (sizeof *r);
 
          if (!r)
            unexec_error ("cannot allocate region structure");
@@ -608,6 +613,26 @@ print_load_command_name (int lc)
     case LC_FUNCTION_STARTS:
       printf ("LC_FUNCTION_STARTS");
       break;
+#endif
+#ifdef LC_MAIN
+    case LC_MAIN:
+      printf ("LC_MAIN          ");
+      break;
+#endif
+#ifdef LC_DATA_IN_CODE
+    case LC_DATA_IN_CODE:
+      printf ("LC_DATA_IN_CODE  ");
+      break;
+#endif
+#ifdef LC_SOURCE_VERSION
+    case LC_SOURCE_VERSION:
+      printf ("LC_SOURCE_VERSION");
+      break;
+#endif
+#ifdef LC_DYLIB_CODE_SIGN_DRS
+    case LC_DYLIB_CODE_SIGN_DRS:
+      printf ("LC_DYLIB_CODE_SIGN_DRS");
+      break;
 #endif
     default:
       printf ("unknown          ");
@@ -671,7 +696,7 @@ read_load_commands (void)
 #endif
 
   nlc = mh.ncmds;
-  lca = (struct load_command **) malloc (nlc * sizeof (struct load_command *));
+  lca = malloc (nlc * sizeof *lca);
 
   for (i = 0; i < nlc; i++)
     {
@@ -680,7 +705,7 @@ read_load_commands (void)
         size first and then read the rest.  */
       if (!unexec_read (&lc, sizeof (struct load_command)))
         unexec_error ("cannot read load command");
-      lca[i] = (struct load_command *) malloc (lc.cmdsize);
+      lca[i] = malloc (lc.cmdsize);
       memcpy (lca[i], &lc, sizeof (struct load_command));
       if (!unexec_read (lca[i] + 1, lc.cmdsize - sizeof (struct load_command)))
         unexec_error ("cannot read content of load command");
@@ -800,8 +825,24 @@ copy_data_segment (struct load_command *lc)
         file.  */
       if (strncmp (sectp->sectname, SECT_DATA, 16) == 0)
        {
-         if (!unexec_write (sectp->offset, (void *) sectp->addr, sectp->size))
+         extern char my_edata[];
+         unsigned long my_size;
+
+         /* The __data section is basically dumped from memory.  But
+            initialized data in statically linked libraries are
+            copied from the input file.  In particular,
+            add_image_hook.names and add_image_hook.pointers stored
+            by libarclite_macosx.a, are restored so that they will be
+            reinitialized when the dumped binary is executed.  */
+         my_size = (unsigned long)my_edata - sectp->addr;
+         if (!(sectp->addr <= (unsigned long)my_edata
+               && my_size <= sectp->size))
+           unexec_error ("my_edata is not in section %s", SECT_DATA);
+         if (!unexec_write (sectp->offset, (void *) sectp->addr, my_size))
            unexec_error ("cannot write section %s", SECT_DATA);
+         if (!unexec_copy (sectp->offset + my_size, old_file_offset + my_size,
+                           sectp->size - my_size))
+           unexec_error ("cannot copy section %s", SECT_DATA);
          if (!unexec_write (header_offset, sectp, sizeof (struct section)))
            unexec_error ("cannot write section %s's header", SECT_DATA);
        }
@@ -848,6 +889,8 @@ copy_data_segment (struct load_command *lc)
               || strncmp (sectp->sectname, "__cfstring", 16) == 0
               || strncmp (sectp->sectname, "__gcc_except_tab", 16) == 0
               || strncmp (sectp->sectname, "__program_vars", 16) == 0
+              || strncmp (sectp->sectname, "__mod_init_func", 16) == 0
+              || strncmp (sectp->sectname, "__mod_term_func", 16) == 0
               || strncmp (sectp->sectname, "__objc_", 7) == 0)
        {
          if (!unexec_copy (sectp->offset, old_file_offset, sectp->size))
@@ -1147,8 +1190,9 @@ copy_dyld_info (struct load_command *lc, long delta)
 #endif
 
 #ifdef LC_FUNCTION_STARTS
-/* Copy a LC_FUNCTION_STARTS load command from the input file to the
-   output file, adjusting the data offset field.  */
+/* Copy a LC_FUNCTION_STARTS/LC_DATA_IN_CODE/LC_DYLIB_CODE_SIGN_DRS
+   load command from the input file to the output file, adjusting the
+   data offset field.  */
 static void
 copy_linkedit_data (struct load_command *lc, long delta)
 {
@@ -1242,6 +1286,12 @@ dump_it (void)
 #endif
 #ifdef LC_FUNCTION_STARTS
       case LC_FUNCTION_STARTS:
+#ifdef LC_DATA_IN_CODE
+      case LC_DATA_IN_CODE:
+#endif
+#ifdef LC_DYLIB_CODE_SIGN_DRS
+      case LC_DYLIB_CODE_SIGN_DRS:
+#endif
        copy_linkedit_data (lca[i], linkedit_delta);
        break;
 #endif
@@ -1378,7 +1428,7 @@ unexec_realloc (void *old_ptr, size_t new_size)
          size_t old_size = ((unexec_malloc_header_t *) old_ptr)[-1].u.size;
          size_t size = new_size > old_size ? old_size : new_size;
 
-         p = (size_t *) malloc (new_size);
+         p = malloc (new_size);
          if (size)
            memcpy (p, old_ptr, size);
        }