Merge from emacs-23 branch
[bpt/emacs.git] / src / unexmacosx.c
index 57f70f8..0751eea 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 Free Software Foundation, Inc.
+   Copyright (C) 2001-2011 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -86,8 +85,20 @@ 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 <stdio.h>
 #include <fcntl.h>
 #include <stdarg.h>
 #include <sys/types.h>
@@ -98,10 +109,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
@@ -190,6 +197,8 @@ 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
@@ -218,7 +227,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;
 
@@ -267,7 +276,7 @@ unexec_copy (off_t dest, off_t src, ssize_t count)
 /* Debugging and informational messages routines.  */
 
 static void
-unexec_error (char *format, ...)
+unexec_error (const char *format, ...)
 {
   va_list ap;
 
@@ -305,7 +314,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 +325,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 +355,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;
@@ -465,7 +474,7 @@ 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;
 
@@ -495,7 +504,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;
@@ -581,6 +590,24 @@ print_load_command_name (int lc)
     case LC_UUID:
       printf ("LC_UUID          ");
       break;
+#endif
+#ifdef LC_DYLD_INFO
+    case LC_DYLD_INFO:
+      printf ("LC_DYLD_INFO     ");
+      break;
+    case LC_DYLD_INFO_ONLY:
+      printf ("LC_DYLD_INFO_ONLY");
+      break;
+#endif
+#ifdef LC_VERSION_MIN_MACOSX
+    case LC_VERSION_MIN_MACOSX:
+      printf ("LC_VERSION_MIN_MACOSX");
+      break;
+#endif
+#ifdef LC_FUNCTION_STARTS
+    case LC_FUNCTION_STARTS:
+      printf ("LC_FUNCTION_STARTS");
+      break;
 #endif
     default:
       printf ("unknown          ");
@@ -619,7 +646,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;
 
@@ -676,8 +703,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);
@@ -814,11 +841,13 @@ copy_data_segment (struct load_command *lc)
        }
       else if (strncmp (sectp->sectname, "__la_symbol_ptr", 16) == 0
               || strncmp (sectp->sectname, "__nl_symbol_ptr", 16) == 0
+              || strncmp (sectp->sectname, "__got", 16) == 0
               || strncmp (sectp->sectname, "__la_sym_ptr2", 16) == 0
               || strncmp (sectp->sectname, "__dyld", 16) == 0
               || strncmp (sectp->sectname, "__const", 16) == 0
               || strncmp (sectp->sectname, "__cfstring", 16) == 0
               || strncmp (sectp->sectname, "__gcc_except_tab", 16) == 0
+              || strncmp (sectp->sectname, "__program_vars", 16) == 0
               || strncmp (sectp->sectname, "__objc_", 7) == 0)
        {
          if (!unexec_copy (sectp->offset, old_file_offset, sectp->size))
@@ -1086,6 +1115,58 @@ copy_twolevelhints (struct load_command *lc, long delta)
   curr_header_offset += lc->cmdsize;
 }
 
+#ifdef LC_DYLD_INFO
+/* Copy a LC_DYLD_INFO(_ONLY) load command from the input file to the output
+   file, adjusting the file offset fields.  */
+static void
+copy_dyld_info (struct load_command *lc, long delta)
+{
+  struct dyld_info_command *dip = (struct dyld_info_command *) lc;
+
+  if (dip->rebase_off > 0)
+    dip->rebase_off += delta;
+  if (dip->bind_off > 0)
+    dip->bind_off += delta;
+  if (dip->weak_bind_off > 0)
+    dip->weak_bind_off += delta;
+  if (dip->lazy_bind_off > 0)
+    dip->lazy_bind_off += delta;
+  if (dip->export_off > 0)
+    dip->export_off += delta;
+
+  printf ("Writing ");
+  print_load_command_name (lc->cmd);
+  printf (" command\n");
+
+  if (!unexec_write (curr_header_offset, lc, lc->cmdsize))
+    unexec_error ("cannot write dyld info command to header");
+
+  curr_header_offset += lc->cmdsize;
+}
+#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.  */
+static void
+copy_linkedit_data (struct load_command *lc, long delta)
+{
+  struct linkedit_data_command *ldp = (struct linkedit_data_command *) lc;
+
+  if (ldp->dataoff > 0)
+    ldp->dataoff += delta;
+
+  printf ("Writing ");
+  print_load_command_name (lc->cmd);
+  printf (" command\n");
+
+  if (!unexec_write (curr_header_offset, lc, lc->cmdsize))
+    unexec_error ("cannot write linkedit data command to header");
+
+  curr_header_offset += lc->cmdsize;
+}
+#endif
+
 /* Copy other kinds of load commands from the input file to the output
    file, ones that do not require adjustments of file offsets.  */
 static void
@@ -1104,7 +1185,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;
@@ -1152,6 +1233,17 @@ dump_it ()
       case LC_TWOLEVEL_HINTS:
        copy_twolevelhints (lca[i], linkedit_delta);
        break;
+#ifdef LC_DYLD_INFO
+      case LC_DYLD_INFO:
+      case LC_DYLD_INFO_ONLY:
+       copy_dyld_info (lca[i], linkedit_delta);
+       break;
+#endif
+#ifdef LC_FUNCTION_STARTS
+      case LC_FUNCTION_STARTS:
+       copy_linkedit_data (lca[i], linkedit_delta);
+       break;
+#endif
       default:
        copy_other (lca[i]);
        break;
@@ -1173,8 +1265,7 @@ 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.");
@@ -1208,7 +1299,7 @@ unexec (char *outfile, char *infile, void *start_data, void *start_bss,
 
 
 void
-unexec_init_emacs_zone ()
+unexec_init_emacs_zone (void)
 {
   emacs_zone = malloc_create_zone (0, 0);
   malloc_set_zone_name (emacs_zone, "EmacsZone");
@@ -1328,6 +1419,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) */