* src/profiler.c: Delete.
[bpt/emacs.git] / src / unexmacosx.c
index b48c74b..8cd80a7 100644 (file)
@@ -1,6 +1,5 @@
 /* Dump Emacs in Mach-O format for use on Mac OS X.
-   Copyright (C) 2001, 2002, 2003, 2004, 2005,
-                 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
+   Copyright (C) 2001-2014 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -86,8 +85,21 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
    be changed accordingly.
 */
 
-#include <stdio.h>
+/* config.h #define:s malloc/realloc/free and then includes stdlib.h.
+   We want the undefined versions, but if config.h includes stdlib.h
+   with the #define:s in place, the prototypes will be wrong and we get
+   warnings.  To prevent that, include stdlib.h before config.h.  */
+
 #include <stdlib.h>
+#include <config.h>
+#undef malloc
+#undef realloc
+#undef free
+
+#include "unexec.h"
+#include "lisp.h"
+
+#include <stdio.h>
 #include <fcntl.h>
 #include <stdarg.h>
 #include <sys/types.h>
@@ -98,10 +110,6 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #if defined (__ppc__)
 #include <mach-o/ppc/reloc.h>
 #endif
-#include <config.h>
-#undef malloc
-#undef realloc
-#undef free
 #ifdef HAVE_MALLOC_MALLOC_H
 #include <malloc/malloc.h>
 #else
@@ -110,6 +118,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
@@ -218,7 +233,7 @@ unexec_write_zero (off_t dest, size_t count)
   char buf[UNEXEC_COPY_BUFSZ];
   ssize_t bytes;
 
-  bzero (buf, UNEXEC_COPY_BUFSZ);
+  memset (buf, 0, UNEXEC_COPY_BUFSZ);
   if (lseek (outfd, dest, SEEK_SET) != dest)
     return 0;
 
@@ -266,8 +281,8 @@ unexec_copy (off_t dest, off_t src, ssize_t count)
 
 /* Debugging and informational messages routines.  */
 
-static void
-unexec_error (char *format, ...)
+static _Noreturn void
+unexec_error (const char *format, ...)
 {
   va_list ap;
 
@@ -305,7 +320,7 @@ print_region (vm_address_t address, vm_size_t size, vm_prot_t prot,
 }
 
 static void
-print_region_list ()
+print_region_list (void)
 {
   struct region_t *r;
 
@@ -316,7 +331,7 @@ print_region_list ()
 }
 
 static void
-print_regions ()
+print_regions (void)
 {
   task_t target_task = mach_task_self ();
   vm_address_t address = (vm_address_t) 0;
@@ -346,7 +361,7 @@ print_regions ()
    cannot be omitted because they some regions created at run time are
    read-only.  */
 static void
-build_region_list ()
+build_region_list (void)
 {
   task_t target_task = mach_task_self ();
   vm_address_t address = (vm_address_t) 0;
@@ -387,7 +402,7 @@ build_region_list ()
        }
       else
        {
-         r = (struct region_t *) malloc (sizeof (struct region_t));
+         r = malloc (sizeof *r);
 
          if (!r)
            unexec_error ("cannot allocate region structure");
@@ -465,11 +480,11 @@ unexec_reader (task_t task, vm_address_t address, vm_size_t size, void **ptr)
 }
 
 static void
-find_emacs_zone_regions ()
+find_emacs_zone_regions (void)
 {
   num_unexec_regions = 0;
 
-  emacs_zone->introspect->enumerator (mach_task_self(), 0,
+  emacs_zone->introspect->enumerator (mach_task_self (), 0,
                                      MALLOC_PTR_REGION_RANGE_TYPE
                                      | MALLOC_ADMIN_REGION_RANGE_TYPE,
                                      (vm_address_t) emacs_zone,
@@ -495,7 +510,7 @@ unexec_regions_sort_compare (const void *a, const void *b)
 }
 
 static void
-unexec_regions_merge ()
+unexec_regions_merge (void)
 {
   int i, n;
   unexec_region_info r;
@@ -599,6 +614,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          ");
@@ -637,7 +672,7 @@ print_load_command (struct load_command *lc)
    the global array lca.  Store the total number of load commands in
    global variable nlc.  */
 static void
-read_load_commands ()
+read_load_commands (void)
 {
   int i;
 
@@ -662,7 +697,7 @@ read_load_commands ()
 #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++)
     {
@@ -671,7 +706,7 @@ read_load_commands ()
         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");
@@ -694,8 +729,8 @@ read_load_commands ()
        }
     }
 
-  printf ("Highest address of load commands in input file: %#8x\n",
-         infile_lc_highest_addr);
+  printf ("Highest address of load commands in input file: %#8lx\n",
+         (unsigned long)infile_lc_highest_addr);
 
   printf ("Lowest offset of all sections in __TEXT segment: %#8lx\n",
          text_seg_lowest_offset);
@@ -791,8 +826,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);
        }
@@ -800,9 +851,9 @@ copy_data_segment (struct load_command *lc)
        {
          sectp->flags = S_REGULAR;
          if (!unexec_write (sectp->offset, (void *) sectp->addr, sectp->size))
-           unexec_error ("cannot write section %s", sectp->sectname);
+           unexec_error ("cannot write section %.16s", sectp->sectname);
          if (!unexec_write (header_offset, sectp, sizeof (struct section)))
-           unexec_error ("cannot write section %s's header", sectp->sectname);
+           unexec_error ("cannot write section %.16s's header", sectp->sectname);
        }
       else if (strncmp (sectp->sectname, SECT_BSS, 16) == 0)
        {
@@ -820,15 +871,15 @@ copy_data_segment (struct load_command *lc)
          my_size = (unsigned long)my_endbss_static - sectp->addr;
          if (!(sectp->addr <= (unsigned long)my_endbss_static
                && my_size <= sectp->size))
-           unexec_error ("my_endbss_static is not in section %s",
+           unexec_error ("my_endbss_static is not in section %.16s",
                          sectp->sectname);
          if (!unexec_write (sectp->offset, (void *) sectp->addr, my_size))
-           unexec_error ("cannot write section %s", sectp->sectname);
+           unexec_error ("cannot write section %.16s", sectp->sectname);
          if (!unexec_write_zero (sectp->offset + my_size,
                                  sectp->size - my_size))
-           unexec_error ("cannot write section %s", sectp->sectname);
+           unexec_error ("cannot write section %.16s", sectp->sectname);
          if (!unexec_write (header_offset, sectp, sizeof (struct section)))
-           unexec_error ("cannot write section %s's header", sectp->sectname);
+           unexec_error ("cannot write section %.16s's header", sectp->sectname);
        }
       else if (strncmp (sectp->sectname, "__la_symbol_ptr", 16) == 0
               || strncmp (sectp->sectname, "__nl_symbol_ptr", 16) == 0
@@ -839,15 +890,18 @@ 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))
-           unexec_error ("cannot copy section %s", sectp->sectname);
+           unexec_error ("cannot copy section %.16s", sectp->sectname);
          if (!unexec_write (header_offset, sectp, sizeof (struct section)))
-           unexec_error ("cannot write section %s's header", sectp->sectname);
+           unexec_error ("cannot write section %.16s's header", sectp->sectname);
        }
       else
-       unexec_error ("unrecognized section name in __DATA segment");
+       unexec_error ("unrecognized section %.16s in __DATA segment",
+                     sectp->sectname);
 
       printf ("        section %-16.16s at %#8lx - %#8lx (sz: %#8lx)\n",
              sectp->sectname, (long) (sectp->offset),
@@ -1137,8 +1191,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)
 {
@@ -1176,7 +1231,7 @@ copy_other (struct load_command *lc)
 /* Loop through all load commands and dump them.  Then write the Mach
    header.  */
 static void
-dump_it ()
+dump_it (void)
 {
   int i;
   long linkedit_delta = 0;
@@ -1232,6 +1287,12 @@ dump_it ()
 #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
@@ -1256,23 +1317,22 @@ dump_it ()
    and infile, respectively.  The three other parameters are
    ignored.  */
 void
-unexec (char *outfile, char *infile, void *start_data, void *start_bss,
-        void *entry_address)
+unexec (const char *outfile, const char *infile)
 {
   if (in_dumped_exec)
     unexec_error ("Unexec from a dumped executable is not supported.");
 
   pagesize = getpagesize ();
-  infd = open (infile, O_RDONLY, 0);
+  infd = emacs_open (infile, O_RDONLY, 0);
   if (infd < 0)
     {
       unexec_error ("cannot open input file `%s'", infile);
     }
 
-  outfd = open (outfile, O_WRONLY | O_TRUNC | O_CREAT, 0755);
+  outfd = emacs_open (outfile, O_WRONLY | O_TRUNC | O_CREAT, 0755);
   if (outfd < 0)
     {
-      close (infd);
+      emacs_close (infd);
       unexec_error ("cannot open output file `%s'", outfile);
     }
 
@@ -1286,12 +1346,12 @@ unexec (char *outfile, char *infile, void *start_data, void *start_bss,
 
   dump_it ();
 
-  close (outfd);
+  emacs_close (outfd);
 }
 
 
 void
-unexec_init_emacs_zone ()
+unexec_init_emacs_zone (void)
 {
   emacs_zone = malloc_create_zone (0, 0);
   malloc_set_zone_name (emacs_zone, "EmacsZone");
@@ -1369,7 +1429,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);
        }
@@ -1411,6 +1471,3 @@ unexec_free (void *ptr)
   else
     malloc_zone_free (emacs_zone, (unexec_malloc_header_t *) ptr - 1);
 }
-
-/* arch-tag: 1a784f7b-a184-4c4f-9544-da8619593d72
-   (do not change this comment) */