]> code.delx.au - gnu-emacs/blobdiff - src/unexmacosx.c
Update copyright year to 2015
[gnu-emacs] / src / unexmacosx.c
index b2b7c5f8e900d907bec32a9c2b246c7c4204eab2..fe6637e2ef5ca4b28ce3ab4aefdc41ecca95b2b5 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-2015 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -97,6 +97,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #undef free
 
 #include "unexec.h"
+#include "lisp.h"
 
 #include <stdio.h>
 #include <fcntl.h>
@@ -106,9 +107,6 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <mach/mach.h>
 #include <mach-o/loader.h>
 #include <mach-o/reloc.h>
-#if defined (__ppc__)
-#include <mach-o/ppc/reloc.h>
-#endif
 #ifdef HAVE_MALLOC_MALLOC_H
 #include <malloc/malloc.h>
 #else
@@ -117,6 +115,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 +202,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 +278,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 +399,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");
@@ -614,6 +617,11 @@ print_load_command_name (int lc)
       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");
@@ -686,7 +694,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++)
     {
@@ -695,7 +703,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");
@@ -815,7 +823,6 @@ copy_data_segment (struct load_command *lc)
         file.  */
       if (strncmp (sectp->sectname, SECT_DATA, 16) == 0)
        {
-         extern char my_edata[];
          unsigned long my_size;
 
          /* The __data section is basically dumped from memory.  But
@@ -840,13 +847,12 @@ 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)
        {
-         extern char *my_endbss_static;
          unsigned long my_size;
 
          sectp->flags = S_REGULAR;
@@ -860,15 +866,36 @@ 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 %.16s's header", sectp->sectname);
+       }
+      else if (strncmp (sectp->sectname, "__bss", 5) == 0
+              || strncmp (sectp->sectname, "__pu_bss", 8) == 0)
+       {
+         sectp->flags = S_REGULAR;
+
+         /* These sections are produced by GCC 4.6+.
+
+            FIXME: We possibly ought to clear uninitialized local
+            variables in statically linked libraries like for
+            SECT_BSS (__bss) above, but setting up the markers we
+            need in lastfile.c would be rather messy. See
+            darwin_output_aligned_bss () in gcc/config/darwin.c for
+            the root of the problem, keeping in mind that the
+            sections are numbered by their alignment in GCC 4.6, but
+            by log2(alignment) in GCC 4.7. */
+
+         if (!unexec_write (sectp->offset, (void *) sectp->addr, sectp->size))
+           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 if (strncmp (sectp->sectname, "__la_symbol_ptr", 16) == 0
               || strncmp (sectp->sectname, "__nl_symbol_ptr", 16) == 0
@@ -879,15 +906,19 @@ 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, "__static_data", 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),
@@ -999,17 +1030,8 @@ unrelocate (const char *name, off_t reloff, int nrel, vm_address_t base)
                          name, i, reloc_info.r_type);
          }
       else
-       switch (sc_reloc_info->r_type)
-         {
-#if defined (__ppc__)
-         case PPC_RELOC_PB_LA_PTR:
-           /* nothing to do for prebound lazy pointer */
-           break;
-#endif
-         default:
-           unexec_error ("unrelocate: %s:%d cannot handle scattered type = %d",
-                         name, i, sc_reloc_info->r_type);
-         }
+        unexec_error ("unrelocate: %s:%d cannot handle scattered type = %d",
+                      name, i, sc_reloc_info->r_type);
     }
 
   if (nrel > 0)
@@ -1017,35 +1039,6 @@ unrelocate (const char *name, off_t reloff, int nrel, vm_address_t base)
            unreloc_count, nrel, name);
 }
 
-#if __ppc64__
-/* Rebase r_address in the relocation table.  */
-static void
-rebase_reloc_address (off_t reloff, int nrel, long linkedit_delta, long diff)
-{
-  int i;
-  struct relocation_info reloc_info;
-  struct scattered_relocation_info *sc_reloc_info
-    = (struct scattered_relocation_info *) &reloc_info;
-
-  for (i = 0; i < nrel; i++, reloff += sizeof (reloc_info))
-    {
-      if (lseek (infd, reloff - linkedit_delta, L_SET)
-         != reloff - linkedit_delta)
-       unexec_error ("rebase_reloc_table: cannot seek to reloc_info");
-      if (!unexec_read (&reloc_info, sizeof (reloc_info)))
-       unexec_error ("rebase_reloc_table: cannot read reloc_info");
-
-      if (sc_reloc_info->r_scattered == 0
-         && reloc_info.r_type == GENERIC_RELOC_VANILLA)
-       {
-         reloc_info.r_address -= diff;
-         if (!unexec_write (reloff, &reloc_info, sizeof (reloc_info)))
-           unexec_error ("rebase_reloc_table: cannot write reloc_info");
-       }
-    }
-}
-#endif
-
 /* Copy a LC_DYSYMTAB load command from the input file to the output
    file, adjusting the file offset fields.  */
 static void
@@ -1055,28 +1048,8 @@ copy_dysymtab (struct load_command *lc, long delta)
   vm_address_t base;
 
 #ifdef _LP64
-#if __ppc64__
-  {
-    int i;
-
-    base = 0;
-    for (i = 0; i < nlc; i++)
-      if (lca[i]->cmd == LC_SEGMENT)
-       {
-         struct segment_command *scp = (struct segment_command *) lca[i];
-
-         if (scp->vmaddr + scp->vmsize > 0x100000000
-             && (scp->initprot & VM_PROT_WRITE) != 0)
-           {
-             base = data_segment_scp->vmaddr;
-             break;
-           }
-       }
-  }
-#else
   /* First writable segment address.  */
   base = data_segment_scp->vmaddr;
-#endif
 #else
   /* First segment address in the file (unless MH_SPLIT_SEGS set). */
   base = 0;
@@ -1102,29 +1075,6 @@ copy_dysymtab (struct load_command *lc, long delta)
     unexec_error ("cannot write symtab command to header");
 
   curr_header_offset += lc->cmdsize;
-
-#if __ppc64__
-  /* Check if the relocation base needs to be changed.  */
-  if (base == 0)
-    {
-      vm_address_t newbase = 0;
-      int i;
-
-      for (i = 0; i < num_unexec_regions; i++)
-       if (unexec_regions[i].range.address + unexec_regions[i].range.size
-           > 0x100000000)
-         {
-           newbase = data_segment_scp->vmaddr;
-           break;
-         }
-
-      if (newbase)
-       {
-         rebase_reloc_address (dstp->locreloff, dstp->nlocrel, delta, newbase);
-         rebase_reloc_address (dstp->extreloff, dstp->nextrel, delta, newbase);
-       }
-    }
-#endif
 }
 
 /* Copy a LC_TWOLEVEL_HINTS load command from the input file to the output
@@ -1177,9 +1127,9 @@ copy_dyld_info (struct load_command *lc, long delta)
 #endif
 
 #ifdef LC_FUNCTION_STARTS
-/* Copy a LC_FUNCTION_STARTS/LC_DYLIB_CODE_SIGN_DRS 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)
 {
@@ -1273,6 +1223,9 @@ 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
@@ -1285,7 +1238,9 @@ dump_it (void)
       }
 
   if (curr_header_offset > text_seg_lowest_offset)
-    unexec_error ("not enough room for load commands for new __DATA segments");
+    unexec_error ("not enough room for load commands for new __DATA segments"
+                 " (increase headerpad_extra in configure.in to at least %lX)",
+                 num_unexec_regions * sizeof (struct segment_command));
 
   printf ("%ld unused bytes follow Mach-O header\n",
          text_seg_lowest_offset - curr_header_offset);
@@ -1306,16 +1261,16 @@ unexec (const char *outfile, const char *infile)
     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, 0777);
   if (outfd < 0)
     {
-      close (infd);
+      emacs_close (infd);
       unexec_error ("cannot open output file `%s'", outfile);
     }
 
@@ -1329,7 +1284,7 @@ unexec (const char *outfile, const char *infile)
 
   dump_it ();
 
-  close (outfd);
+  emacs_close (outfd);
 }
 
 
@@ -1412,7 +1367,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);
        }