]> code.delx.au - gnu-emacs/blobdiff - src/unexmacosx.c
Fix previous ChangeLog date fix (no need to merge to trunk)
[gnu-emacs] / src / unexmacosx.c
index a1950f9445ecb81b23e8e14375403378b7defb6f..b48c74bbe2cdaeba951c9b105267af7f13710b01 100644 (file)
@@ -1,13 +1,13 @@
 /* Dump Emacs in Mach-O format for use on Mac OS X.
    Copyright (C) 2001, 2002, 2003, 2004, 2005,
-                 2006 Free Software Foundation, Inc.
+                 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
-GNU Emacs is free software; you can redistribute it and/or modify
+GNU Emacs 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.
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
 
 GNU Emacs is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -15,9 +15,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with GNU Emacs; see the file COPYING.  If not, write to
-the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-Boston, MA 02110-1301, USA.  */
+along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 /* Contributed by Andrew Choi (akochoi@mac.com).  */
 
@@ -69,10 +67,10 @@ Boston, MA 02110-1301, USA.  */
    fact, the earliest one starts a few hundred bytes beyond the end of
    the last load command.  The linker option -headerpad controls the
    minimum size of this padding.  Its setting can be changed in
-   s/darwin.h.  A value of 0x300, e.g., leaves room for about 15
-   additional load commands for the newly created __DATA segments (at
-   56 bytes each).  Unexec fails if there is not enough room for these
-   new segments.
+   s/darwin.h.  A value of 0x690, e.g., leaves room for 30 additional
+   load commands for the newly created __DATA segments (at 56 bytes
+   each).  Unexec fails if there is not enough room for these new
+   segments.
 
    The __TEXT segment contains the sections __text, __cstring,
    __picsymbol_stub, and __const and the __DATA segment contains the
@@ -137,9 +135,6 @@ Boston, MA 02110-1301, USA.  */
    mapped to dynamically loaded libraries and will not be dumped.  */
 #define VM_DATA_TOP (20 * 1024 * 1024)
 
-/* Used by malloc_freezedry and malloc_jumpstart.  */
-int malloc_cookie;
-
 /* Type of an element on the list of regions to be dumped.  */
 struct region_t {
   vm_address_t address;
@@ -151,32 +146,32 @@ struct region_t {
 };
 
 /* Head and tail of the list of regions to be dumped.  */
-struct region_t *region_list_head = 0;
-struct region_t *region_list_tail = 0;
+static struct region_t *region_list_head = 0;
+static struct region_t *region_list_tail = 0;
 
 /* Pointer to array of load commands.  */
-struct load_command **lca;
+static struct load_command **lca;
 
 /* Number of load commands.  */
-int nlc;
+static int nlc;
 
 /* The highest VM address of segments loaded by the input file.
    Regions with addresses beyond this are assumed to be allocated
    dynamically and thus require dumping.  */
-vm_address_t infile_lc_highest_addr = 0;
+static vm_address_t infile_lc_highest_addr = 0;
 
 /* The lowest file offset used by the all sections in the __TEXT
    segments.  This leaves room at the beginning of the file to store
    the Mach-O header.  Check this value against header size to ensure
    the added load commands for the new __DATA segments did not
    overwrite any of the sections in the __TEXT segment.  */
-unsigned long text_seg_lowest_offset = 0x10000000;
+static unsigned long text_seg_lowest_offset = 0x10000000;
 
 /* Mach header.  */
-struct mach_header mh;
+static struct mach_header mh;
 
 /* Offset at which the next load command should be written.  */
-unsigned long curr_header_offset = sizeof (struct mach_header);
+static unsigned long curr_header_offset = sizeof (struct mach_header);
 
 /* Offset at which the next segment should be written.  */
 static unsigned long curr_file_offset = 0;
@@ -184,16 +179,16 @@ static unsigned long curr_file_offset = 0;
 static unsigned long pagesize;
 #define ROUNDUP_TO_PAGE_BOUNDARY(x)    (((x) + pagesize - 1) & ~(pagesize - 1))
 
-int infd, outfd;
+static int infd, outfd;
 
-int in_dumped_exec = 0;
+static int in_dumped_exec = 0;
 
-malloc_zone_t *emacs_zone;
+static malloc_zone_t *emacs_zone;
 
 /* file offset of input file's data segment */
-off_t data_segment_old_fileoff = 0;
+static off_t data_segment_old_fileoff = 0;
 
-struct segment_command *data_segment_scp;
+static struct segment_command *data_segment_scp;
 
 /* Read N bytes from infd into memory starting at address DEST.
    Return true if successful, false otherwise.  */
@@ -320,7 +315,7 @@ print_region_list ()
     print_region (r->address, r->size, r->protection, r->max_protection);
 }
 
-void
+static void
 print_regions ()
 {
   task_t target_task = mach_task_self ();
@@ -430,18 +425,34 @@ build_region_list ()
 
 #define MAX_UNEXEC_REGIONS 400
 
-int num_unexec_regions;
-vm_range_t unexec_regions[MAX_UNEXEC_REGIONS];
+static int num_unexec_regions;
+typedef struct {
+  vm_range_t range;
+  vm_size_t filesize;
+} unexec_region_info;
+static unexec_region_info unexec_regions[MAX_UNEXEC_REGIONS];
 
 static void
 unexec_regions_recorder (task_t task, void *rr, unsigned type,
                         vm_range_t *ranges, unsigned num)
 {
+  vm_address_t p;
+  vm_size_t filesize;
+
   while (num && num_unexec_regions < MAX_UNEXEC_REGIONS)
     {
-      unexec_regions[num_unexec_regions++] = *ranges;
-      printf ("%#8lx (sz: %#8lx)\n",
-             (long) (ranges->address), (long) (ranges->size));
+      /* Subtract the size of trailing null bytes from filesize.  It
+        can be smaller than vmsize in segment commands.  In such a
+        case, trailing bytes are initialized with zeros.  */
+      for (p = ranges->address + ranges->size; p > ranges->address; p--)
+       if (*(((char *) p)-1))
+         break;
+      filesize = p - ranges->address;
+
+      unexec_regions[num_unexec_regions].filesize = filesize;
+      unexec_regions[num_unexec_regions++].range = *ranges;
+      printf ("%#10lx (sz: %#8lx/%#8lx)\n", (long) (ranges->address),
+             (long) filesize, (long) (ranges->size));
       ranges++; num--;
     }
 }
@@ -453,7 +464,7 @@ unexec_reader (task_t task, vm_address_t address, vm_size_t size, void **ptr)
   return KERN_SUCCESS;
 }
 
-void
+static void
 find_emacs_zone_regions ()
 {
   num_unexec_regions = 0;
@@ -472,8 +483,8 @@ find_emacs_zone_regions ()
 static int
 unexec_regions_sort_compare (const void *a, const void *b)
 {
-  vm_address_t aa = ((vm_range_t *) a)->address;
-  vm_address_t bb = ((vm_range_t *) b)->address;
+  vm_address_t aa = ((unexec_region_info *) a)->range.address;
+  vm_address_t bb = ((unexec_region_info *) b)->range.address;
 
   if (aa < bb)
     return -1;
@@ -487,22 +498,43 @@ static void
 unexec_regions_merge ()
 {
   int i, n;
-  vm_range_t r;
+  unexec_region_info r;
+  vm_size_t padsize;
 
   qsort (unexec_regions, num_unexec_regions, sizeof (unexec_regions[0]),
         &unexec_regions_sort_compare);
   n = 0;
   r = unexec_regions[0];
+  padsize = r.range.address & (pagesize - 1);
+  if (padsize)
+    {
+      r.range.address -= padsize;
+      r.range.size += padsize;
+      r.filesize += padsize;
+    }
   for (i = 1; i < num_unexec_regions; i++)
     {
-      if (r.address + r.size == unexec_regions[i].address)
+      if (r.range.address + r.range.size == unexec_regions[i].range.address
+         && r.range.size - r.filesize < 2 * pagesize)
        {
-         r.size += unexec_regions[i].size;
+         r.filesize = r.range.size + unexec_regions[i].filesize;
+         r.range.size += unexec_regions[i].range.size;
        }
       else
        {
          unexec_regions[n++] = r;
          r = unexec_regions[i];
+         padsize = r.range.address & (pagesize - 1);
+         if (padsize)
+           {
+             if ((unexec_regions[n-1].range.address
+                  + unexec_regions[n-1].range.size) == r.range.address)
+               unexec_regions[n-1].range.size -= padsize;
+
+             r.range.address -= padsize;
+             r.range.size += padsize;
+             r.filesize += padsize;
+           }
        }
     }
   unexec_regions[n++] = r;
@@ -545,6 +577,29 @@ print_load_command_name (int lc)
     case LC_TWOLEVEL_HINTS:
       printf ("LC_TWOLEVEL_HINTS");
       break;
+#ifdef LC_UUID
+    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          ");
     }
@@ -642,7 +697,7 @@ read_load_commands ()
   printf ("Highest address of load commands in input file: %#8x\n",
          infile_lc_highest_addr);
 
-  printf ("Lowest offset of all sections in __TEXT segment: %#8x\n",
+  printf ("Lowest offset of all sections in __TEXT segment: %#8lx\n",
          text_seg_lowest_offset);
 
   printf ("--- List of Load Commands in Input File ---\n");
@@ -675,9 +730,9 @@ copy_segment (struct load_command *lc)
       sectp++;
     }
 
-  printf ("Writing segment %-16.16s @ %#8lx (%#8lx @ %#8lx)\n",
-         scp->segname, (long) (scp->fileoff), (long) (scp->vmsize),
-         (long) (scp->vmaddr));
+  printf ("Writing segment %-16.16s @ %#8lx (%#8lx/%#8lx @ %#10lx)\n",
+         scp->segname, (long) (scp->fileoff), (long) (scp->filesize),
+         (long) (scp->vmsize), (long) (scp->vmaddr));
 
   if (!unexec_copy (scp->fileoff, old_fileoff, scp->filesize))
     unexec_error ("cannot copy segment from input to output file");
@@ -707,11 +762,18 @@ copy_data_segment (struct load_command *lc)
   struct segment_command *scp = (struct segment_command *) lc;
   struct section *sectp;
   int j;
-  unsigned long header_offset, file_offset, old_file_offset;
+  unsigned long header_offset, old_file_offset;
+
+  /* The new filesize of the segment is set to its vmsize because data
+     blocks for segments must start at region boundaries.  Note that
+     this may leave unused locations at the end of the segment data
+     block because the total of the sizes of all sections in the
+     segment is generally smaller than vmsize.  */
+  scp->filesize = scp->vmsize;
 
-  printf ("Writing segment %-16.16s @ %#8lx (%#8lx @ %#8lx)\n",
-         scp->segname, curr_file_offset, (long)(scp->vmsize),
-         (long) (scp->vmaddr));
+  printf ("Writing segment %-16.16s @ %#8lx (%#8lx/%#8lx @ %#10lx)\n",
+         scp->segname, curr_file_offset, (long)(scp->filesize),
+         (long)(scp->vmsize), (long) (scp->vmaddr));
 
   /* Offsets in the output file for writing the next section structure
      and segment data block, respectively.  */
@@ -770,10 +832,14 @@ 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, "__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))
            unexec_error ("cannot copy section %s", sectp->sectname);
@@ -791,16 +857,11 @@ copy_data_segment (struct load_command *lc)
       sectp++;
     }
 
-  /* The new filesize of the segment is set to its vmsize because data
-     blocks for segments must start at region boundaries.  Note that
-     this may leave unused locations at the end of the segment data
-     block because the total of the sizes of all sections in the
-     segment is generally smaller than vmsize.  */
-  scp->filesize = scp->vmsize;
+  curr_file_offset += ROUNDUP_TO_PAGE_BOUNDARY (scp->filesize);
+
   if (!unexec_write (curr_header_offset, scp, sizeof (struct segment_command)))
     unexec_error ("cannot write header of __DATA segment");
   curr_header_offset += lc->cmdsize;
-  curr_file_offset += ROUNDUP_TO_PAGE_BOUNDARY (scp->filesize);
 
   /* Create new __DATA segment load commands for regions on the region
      list that do not corresponding to any segment load commands in
@@ -813,20 +874,20 @@ copy_data_segment (struct load_command *lc)
       sc.cmd = LC_SEGMENT;
       sc.cmdsize = sizeof (struct segment_command);
       strncpy (sc.segname, SEG_DATA, 16);
-      sc.vmaddr = unexec_regions[j].address;
-      sc.vmsize = unexec_regions[j].size;
+      sc.vmaddr = unexec_regions[j].range.address;
+      sc.vmsize = unexec_regions[j].range.size;
       sc.fileoff = curr_file_offset;
-      sc.filesize = unexec_regions[j].size;
+      sc.filesize = unexec_regions[j].filesize;
       sc.maxprot = VM_PROT_READ | VM_PROT_WRITE;
       sc.initprot = VM_PROT_READ | VM_PROT_WRITE;
       sc.nsects = 0;
       sc.flags = 0;
 
-      printf ("Writing segment %-16.16s @ %#8lx (%#8lx @ %#8lx)\n",
-             sc.segname, (long) (sc.fileoff), (long) (sc.vmsize),
-             (long) (sc.vmaddr));
+      printf ("Writing segment %-16.16s @ %#8lx (%#8lx/%#8lx @ %#10lx)\n",
+             sc.segname, (long) (sc.fileoff), (long) (sc.filesize),
+             (long) (sc.vmsize), (long) (sc.vmaddr));
 
-      if (!unexec_write (sc.fileoff, (void *) sc.vmaddr, sc.vmsize))
+      if (!unexec_write (sc.fileoff, (void *) sc.vmaddr, sc.filesize))
        unexec_error ("cannot write new __DATA segment");
       curr_file_offset += ROUNDUP_TO_PAGE_BOUNDARY (sc.filesize);
 
@@ -857,12 +918,13 @@ copy_symtab (struct load_command *lc, long delta)
 
 /* Fix up relocation entries. */
 static void
-unrelocate (const char *name, off_t reloff, int nrel)
+unrelocate (const char *name, off_t reloff, int nrel, vm_address_t base)
 {
   int i, unreloc_count;
   struct relocation_info reloc_info;
   struct scattered_relocation_info *sc_reloc_info
     = (struct scattered_relocation_info *) &reloc_info;
+  vm_address_t location;
 
   for (unreloc_count = 0, i = 0; i < nrel; i++)
     {
@@ -876,14 +938,15 @@ unrelocate (const char *name, off_t reloff, int nrel)
        switch (reloc_info.r_type)
          {
          case GENERIC_RELOC_VANILLA:
-           if (reloc_info.r_address >= data_segment_scp->vmaddr
-               && reloc_info.r_address < (data_segment_scp->vmaddr
-                                          + data_segment_scp->vmsize))
+           location = base + reloc_info.r_address;
+           if (location >= data_segment_scp->vmaddr
+               && location < (data_segment_scp->vmaddr
+                              + data_segment_scp->vmsize))
              {
                off_t src_off = data_segment_old_fileoff
-                 + reloc_info.r_address - data_segment_scp->vmaddr;
+                 + (location - data_segment_scp->vmaddr);
                off_t dst_off = data_segment_scp->fileoff
-                 + reloc_info.r_address - data_segment_scp->vmaddr;
+                 + (location - data_segment_scp->vmaddr);
 
                if (!unexec_copy (dst_off, src_off, 1 << reloc_info.r_length))
                  unexec_error ("unrelocate: %s:%d cannot copy original value",
@@ -914,15 +977,73 @@ unrelocate (const char *name, off_t reloff, int nrel)
            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
 copy_dysymtab (struct load_command *lc, long delta)
 {
   struct dysymtab_command *dstp = (struct dysymtab_command *) lc;
+  vm_address_t base;
 
-  unrelocate ("local", dstp->locreloff, dstp->nlocrel);
-  unrelocate ("external", dstp->extreloff, dstp->nextrel);
+#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;
+#endif
+
+  unrelocate ("local", dstp->locreloff, dstp->nlocrel, base);
+  unrelocate ("external", dstp->extreloff, dstp->nextrel, base);
 
   if (dstp->nextrel > 0) {
     dstp->extreloff += delta;
@@ -941,6 +1062,29 @@ 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
@@ -962,6 +1106,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
@@ -1028,6 +1224,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;
@@ -1036,7 +1243,7 @@ dump_it ()
   if (curr_header_offset > text_seg_lowest_offset)
     unexec_error ("not enough room for load commands for new __DATA segments");
 
-  printf ("%d unused bytes follow Mach-O header\n",
+  printf ("%ld unused bytes follow Mach-O header\n",
          text_seg_lowest_offset - curr_header_offset);
 
   mh.sizeofcmds = curr_header_offset - sizeof (struct mach_header);
@@ -1113,8 +1320,8 @@ ptr_in_unexec_regions (void *ptr)
   int i;
 
   for (i = 0; i < num_unexec_regions; i++)
-    if ((vm_address_t) ptr - unexec_regions[i].address
-       < unexec_regions[i].size)
+    if ((vm_address_t) ptr - unexec_regions[i].range.address
+       < unexec_regions[i].range.size)
       return 1;
 
   return 0;
@@ -1194,6 +1401,8 @@ unexec_realloc (void *old_ptr, size_t new_size)
 void
 unexec_free (void *ptr)
 {
+  if (ptr == NULL)
+    return;
   if (in_dumped_exec)
     {
       if (!ptr_in_unexec_regions (ptr))