/* Dump Emacs in Mach-O format for use on Mac OS X.
- Copyright (C) 2001, 2002 Free Software Foundation, Inc.
+ Copyright (C) 2001, 2002, 2003, 2004, 2005,
+ 2006 Free Software Foundation, Inc.
This file is part of GNU Emacs.
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., 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA. */
+the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+Boston, MA 02110-1301, USA. */
/* Contributed by Andrew Choi (akochoi@mac.com). */
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
#include <unistd.h>
#include <mach/mach.h>
#include <mach-o/loader.h>
+#include <mach-o/reloc.h>
+#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
#include <objc/malloc.h>
+#endif
+
+#include <assert.h>
+
+#ifdef _LP64
+#define mach_header mach_header_64
+#define segment_command segment_command_64
+#undef VM_REGION_BASIC_INFO_COUNT
+#define VM_REGION_BASIC_INFO_COUNT VM_REGION_BASIC_INFO_COUNT_64
+#undef VM_REGION_BASIC_INFO
+#define VM_REGION_BASIC_INFO VM_REGION_BASIC_INFO_64
+#undef LC_SEGMENT
+#define LC_SEGMENT LC_SEGMENT_64
+#define vm_region vm_region_64
+#define section section_64
+#undef MH_MAGIC
+#define MH_MAGIC MH_MAGIC_64
+#endif
#define VERBOSE 1
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;
};
/* 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;
+
+static unsigned long pagesize;
+#define ROUNDUP_TO_PAGE_BOUNDARY(x) (((x) + pagesize - 1) & ~(pagesize - 1))
-/* Current adjustment that needs to be made to offset values because
- of additional data segments. */
-unsigned long delta = 0;
+static int infd, outfd;
-int infd, outfd;
+static int in_dumped_exec = 0;
-int in_dumped_exec = 0;
+static malloc_zone_t *emacs_zone;
-malloc_zone_t *emacs_zone;
+/* file offset of input file's data segment */
+static off_t data_segment_old_fileoff = 0;
-/* Read n bytes from infd into memory starting at address dest.
+static struct segment_command *data_segment_scp;
+
+/* Read N bytes from infd into memory starting at address DEST.
Return true if successful, false otherwise. */
static int
unexec_read (void *dest, size_t n)
return n == read (infd, dest, n);
}
-/* Write n bytes from memory starting at address src to outfd starting
- at offset dest. Return true if successful, false otherwise. */
+/* Write COUNT bytes from memory starting at address SRC to outfd
+ starting at offset DEST. Return true if successful, false
+ otherwise. */
static int
unexec_write (off_t dest, const void *src, size_t count)
{
return write (outfd, src, count) == count;
}
-/* Copy n bytes from starting offset src in infd to starting offset
- dest in outfd. Return true if successful, false otherwise. */
+/* Write COUNT bytes of zeros to outfd starting at offset DEST.
+ Return true if successful, false otherwise. */
+static int
+unexec_write_zero (off_t dest, size_t count)
+{
+ char buf[UNEXEC_COPY_BUFSZ];
+ ssize_t bytes;
+
+ bzero (buf, UNEXEC_COPY_BUFSZ);
+ if (lseek (outfd, dest, SEEK_SET) != dest)
+ return 0;
+
+ while (count > 0)
+ {
+ bytes = count > UNEXEC_COPY_BUFSZ ? UNEXEC_COPY_BUFSZ : count;
+ if (write (outfd, buf, bytes) != bytes)
+ return 0;
+ count -= bytes;
+ }
+
+ return 1;
+}
+
+/* Copy COUNT bytes from starting offset SRC in infd to starting
+ offset DEST in outfd. Return true if successful, false
+ otherwise. */
static int
unexec_copy (off_t dest, off_t src, ssize_t count)
{
ssize_t bytes_read;
+ ssize_t bytes_to_read;
char buf[UNEXEC_COPY_BUFSZ];
while (count > 0)
{
- bytes_read = read (infd, buf, UNEXEC_COPY_BUFSZ);
+ bytes_to_read = count > UNEXEC_COPY_BUFSZ ? UNEXEC_COPY_BUFSZ : count;
+ bytes_read = read (infd, buf, bytes_to_read);
if (bytes_read <= 0)
return 0;
if (write (outfd, buf, bytes_read) != bytes_read)
print_region (vm_address_t address, vm_size_t size, vm_prot_t prot,
vm_prot_t max_prot)
{
- printf ("%#10x %#8x ", address, size);
+ printf ("%#10lx %#8lx ", (long) address, (long) size);
print_prot (prot);
putchar (' ');
print_prot (max_prot);
print_region (r->address, r->size, r->protection, r->max_protection);
}
-void
+static void
print_regions ()
{
task_t target_task = mach_task_self ();
}
-#define MAX_UNEXEC_REGIONS 200
+#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 ("%#8x (sz: %#8x)\n", ranges->address, ranges->size);
+ /* Subtract the size of trailing null pages from filesize. It
+ can be smaller than vmsize in segment commands. In such a
+ case, trailing pages are initialized with zeros. */
+ for (p = ranges->address + ranges->size; p > ranges->address;
+ p -= sizeof (int))
+ if (*(((int *) p)-1))
+ break;
+ filesize = ROUNDUP_TO_PAGE_BOUNDARY (p - ranges->address);
+ assert (filesize <= ranges->size);
+
+ 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--;
}
- if (num_unexec_regions == MAX_UNEXEC_REGIONS)
- fprintf (stderr, "malloc_freezedry_recorder: too many regions\n");
}
static kern_return_t
return KERN_SUCCESS;
}
-void
+static void
find_emacs_zone_regions ()
{
num_unexec_regions = 0;
(vm_address_t) emacs_zone,
unexec_reader,
unexec_regions_recorder);
+
+ if (num_unexec_regions == MAX_UNEXEC_REGIONS)
+ unexec_error ("find_emacs_zone_regions: too many 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;
unexec_regions_merge ()
{
int i, n;
- vm_range_t r;
+ unexec_region_info r;
qsort (unexec_regions, num_unexec_regions, sizeof (unexec_regions[0]),
&unexec_regions_sort_compare);
r = unexec_regions[0];
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
{
switch (lc)
{
case LC_SEGMENT:
+#ifndef _LP64
printf ("LC_SEGMENT ");
+#else
+ printf ("LC_SEGMENT_64 ");
+#endif
break;
case LC_LOAD_DYLINKER:
printf ("LC_LOAD_DYLINKER ");
int j;
scp = (struct segment_command *) lc;
- printf (" %-16.16s %#10x %#8x\n",
- scp->segname, scp->vmaddr, scp->vmsize);
+ printf (" %-16.16s %#10lx %#8lx\n",
+ scp->segname, (long) (scp->vmaddr), (long) (scp->vmsize));
sectp = (struct section *) (scp + 1);
for (j = 0; j < scp->nsects; j++)
{
- printf (" %-16.16s %#10x %#8x\n",
- sectp->sectname, sectp->addr, sectp->size);
+ printf (" %-16.16s %#10lx %#8lx\n",
+ sectp->sectname, (long) (sectp->addr), (long) (sectp->size));
sectp++;
}
}
static void
read_load_commands ()
{
- int n, i, j;
+ int i;
if (!unexec_read (&mh, sizeof (struct mach_header)))
unexec_error ("cannot read mach-o header");
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");
struct section *sectp;
int j;
- scp->fileoff += delta;
+ scp->fileoff = curr_file_offset;
sectp = (struct section *) (scp + 1);
for (j = 0; j < scp->nsects; j++)
{
- sectp->offset += delta;
+ sectp->offset += curr_file_offset - old_fileoff;
sectp++;
}
- printf ("Writing segment %-16.16s at %#8x - %#8x (sz: %#8x)\n",
- scp->segname, scp->fileoff, scp->fileoff + scp->filesize,
- scp->filesize);
+ 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");
+ curr_file_offset += ROUNDUP_TO_PAGE_BOUNDARY (scp->filesize);
+
if (!unexec_write (curr_header_offset, lc, lc->cmdsize))
unexec_error ("cannot write load command to header");
struct segment_command *scp = (struct segment_command *) lc;
struct section *sectp;
int j;
- unsigned long header_offset, file_offset, old_file_offset;
- struct region_t *r;
+ unsigned long header_offset, old_file_offset;
- printf ("Writing segment %-16.16s at %#8x - %#8x (sz: %#8x)\n",
- scp->segname, scp->fileoff, scp->fileoff + scp->filesize,
- scp->filesize);
+ /* 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;
- if (delta != 0)
- unexec_error ("cannot handle multiple DATA segments in input file");
+ 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. */
for (j = 0; j < scp->nsects; j++)
{
old_file_offset = sectp->offset;
- sectp->offset = sectp->addr - scp->vmaddr + scp->fileoff;
+ sectp->offset = sectp->addr - scp->vmaddr + curr_file_offset;
/* The __data section is dumped from memory. The __bss and
__common sections are also dumped from memory but their flag
fields require changing (from S_ZEROFILL to S_REGULAR). The
if (!unexec_write (header_offset, sectp, sizeof (struct section)))
unexec_error ("cannot write section %s's header", SECT_DATA);
}
- else if (strncmp (sectp->sectname, SECT_BSS, 16) == 0
- || strncmp (sectp->sectname, SECT_COMMON, 16) == 0)
+ else if (strncmp (sectp->sectname, SECT_COMMON, 16) == 0)
{
sectp->flags = S_REGULAR;
if (!unexec_write (sectp->offset, (void *) sectp->addr, sectp->size))
- unexec_error ("cannot write section %s", SECT_DATA);
+ unexec_error ("cannot write section %s", sectp->sectname);
if (!unexec_write (header_offset, sectp, sizeof (struct section)))
- unexec_error ("cannot write section %s's header", SECT_DATA);
+ unexec_error ("cannot write section %s'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;
+
+ /* Clear uninitialized local variables in statically linked
+ libraries. In particular, function pointers stored by
+ libSystemStub.a, which is introduced in Mac OS X 10.4 for
+ binary compatibility with respect to long double, are
+ cleared so that they will be reinitialized when the
+ dumped binary is executed on other versions of OS. */
+ 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",
+ sectp->sectname);
+ if (!unexec_write (sectp->offset, (void *) sectp->addr, my_size))
+ unexec_error ("cannot write section %s", sectp->sectname);
+ if (!unexec_write_zero (sectp->offset + my_size,
+ sectp->size - my_size))
+ unexec_error ("cannot write section %s", sectp->sectname);
+ if (!unexec_write (header_offset, sectp, sizeof (struct section)))
+ unexec_error ("cannot write section %s's header", sectp->sectname);
}
else if (strncmp (sectp->sectname, "__la_symbol_ptr", 16) == 0
|| strncmp (sectp->sectname, "__nl_symbol_ptr", 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)
else
unexec_error ("unrecognized section name in __DATA segment");
- printf (" section %-16.16s at %#8x - %#8x (sz: %#8x)\n",
- sectp->sectname, sectp->offset, sectp->offset + sectp->size,
- sectp->size);
+ printf (" section %-16.16s at %#8lx - %#8lx (sz: %#8lx)\n",
+ sectp->sectname, (long) (sectp->offset),
+ (long) (sectp->offset + sectp->size), (long) (sectp->size));
header_offset += sizeof (struct section);
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. */
- delta = scp->vmsize - scp->filesize;
- 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;
/* Create new __DATA segment load commands for regions on the region
list that do not corresponding to any segment load commands in
the input file.
- */
- file_offset = scp->fileoff + scp->filesize;
+ */
for (j = 0; j < num_unexec_regions; j++)
{
struct segment_command sc;
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.fileoff = file_offset;
- sc.filesize = 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].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 at %#8x - %#8x (sz: %#8x)\n",
- sc.segname, sc.fileoff, sc.fileoff + sc.filesize,
- sc.filesize);
+ 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");
- delta += sc.filesize;
- file_offset += sc.filesize;
+ curr_file_offset += ROUNDUP_TO_PAGE_BOUNDARY (sc.filesize);
if (!unexec_write (curr_header_offset, &sc, sc.cmdsize))
unexec_error ("cannot write new __DATA segment's header");
/* Copy a LC_SYMTAB load command from the input file to the output
file, adjusting the file offset fields. */
static void
-copy_symtab (struct load_command *lc)
+copy_symtab (struct load_command *lc, long delta)
{
struct symtab_command *stp = (struct symtab_command *) lc;
curr_header_offset += lc->cmdsize;
}
+/* Fix up relocation entries. */
+static void
+unrelocate (const char *name, off_t reloff, int nrel)
+{
+ int i, unreloc_count;
+ struct relocation_info reloc_info;
+ struct scattered_relocation_info *sc_reloc_info
+ = (struct scattered_relocation_info *) &reloc_info;
+
+ for (unreloc_count = 0, i = 0; i < nrel; i++)
+ {
+ if (lseek (infd, reloff, L_SET) != reloff)
+ unexec_error ("unrelocate: %s:%d cannot seek to reloc_info", name, i);
+ if (!unexec_read (&reloc_info, sizeof (reloc_info)))
+ unexec_error ("unrelocate: %s:%d cannot read reloc_info", name, i);
+ reloff += sizeof (reloc_info);
+
+ if (sc_reloc_info->r_scattered == 0)
+ 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))
+ {
+ off_t src_off = data_segment_old_fileoff
+ + reloc_info.r_address - data_segment_scp->vmaddr;
+ off_t dst_off = data_segment_scp->fileoff
+ + reloc_info.r_address - 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",
+ name, i);
+ unreloc_count++;
+ }
+ break;
+ default:
+ unexec_error ("unrelocate: %s:%d cannot handle type = %d",
+ 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);
+ }
+ }
+
+ if (nrel > 0)
+ printf ("Fixed up %d/%d %s relocation entries in data segment.\n",
+ unreloc_count, nrel, name);
+}
+
/* 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)
+copy_dysymtab (struct load_command *lc, long delta)
{
struct dysymtab_command *dstp = (struct dysymtab_command *) lc;
- /* If Mach-O executable is not prebound, relocation entries need
- fixing up. This is not supported currently. */
- if (!(mh.flags & MH_PREBOUND) && (dstp->nextrel != 0 || dstp->nlocrel != 0))
- unexec_error ("cannot handle LC_DYSYMTAB with relocation entries");
+ unrelocate ("local", dstp->locreloff, dstp->nlocrel);
+ unrelocate ("external", dstp->extreloff, dstp->nextrel);
if (dstp->nextrel > 0) {
dstp->extreloff += delta;
/* Copy a LC_TWOLEVEL_HINTS load command from the input file to the output
file, adjusting the file offset fields. */
static void
-copy_twolevelhints (struct load_command *lc)
+copy_twolevelhints (struct load_command *lc, long delta)
{
struct twolevel_hints_command *tlhp = (struct twolevel_hints_command *) lc;
dump_it ()
{
int i;
+ long linkedit_delta = 0;
printf ("--- Load Commands written to Output File ---\n");
struct segment_command *scp = (struct segment_command *) lca[i];
if (strncmp (scp->segname, SEG_DATA, 16) == 0)
{
+ /* save data segment file offset and segment_command for
+ unrelocate */
+ if (data_segment_old_fileoff)
+ unexec_error ("cannot handle multiple DATA segments"
+ " in input file");
+ data_segment_old_fileoff = scp->fileoff;
+ data_segment_scp = scp;
+
copy_data_segment (lca[i]);
}
else
{
+ if (strncmp (scp->segname, SEG_LINKEDIT, 16) == 0)
+ {
+ if (linkedit_delta)
+ unexec_error ("cannot handle multiple LINKEDIT segments"
+ " in input file");
+ linkedit_delta = curr_file_offset - scp->fileoff;
+ }
+
copy_segment (lca[i]);
}
}
break;
case LC_SYMTAB:
- copy_symtab (lca[i]);
+ copy_symtab (lca[i], linkedit_delta);
break;
case LC_DYSYMTAB:
- copy_dysymtab (lca[i]);
+ copy_dysymtab (lca[i], linkedit_delta);
break;
case LC_TWOLEVEL_HINTS:
- copy_twolevelhints (lca[i]);
+ copy_twolevelhints (lca[i], linkedit_delta);
break;
default:
copy_other (lca[i]);
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);
unexec (char *outfile, char *infile, void *start_data, void *start_bss,
void *entry_address)
{
+ if (in_dumped_exec)
+ unexec_error ("Unexec from a dumped executable is not supported.");
+
+ pagesize = getpagesize ();
infd = open (infile, O_RDONLY, 0);
if (infd < 0)
{
malloc_set_zone_name (emacs_zone, "EmacsZone");
}
+#ifndef MACOSX_MALLOC_MULT16
+#define MACOSX_MALLOC_MULT16 1
+#endif
+
+typedef struct unexec_malloc_header {
+ union {
+ char c[8];
+ size_t size;
+ } u;
+} unexec_malloc_header_t;
+
+#if MACOSX_MALLOC_MULT16
+
+#define ptr_in_unexec_regions(p) ((((vm_address_t) (p)) & 8) != 0)
+
+#else
+
int
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;
}
+#endif
+
void *
unexec_malloc (size_t size)
{
if (in_dumped_exec)
- return malloc (size);
+ {
+ void *p;
+
+ p = malloc (size);
+#if MACOSX_MALLOC_MULT16
+ assert (((vm_address_t) p % 16) == 0);
+#endif
+ return p;
+ }
else
- return malloc_zone_malloc (emacs_zone, size);
+ {
+ unexec_malloc_header_t *ptr;
+
+ ptr = (unexec_malloc_header_t *)
+ malloc_zone_malloc (emacs_zone, size + sizeof (unexec_malloc_header_t));
+ ptr->u.size = size;
+ ptr++;
+#if MACOSX_MALLOC_MULT16
+ assert (((vm_address_t) ptr % 16) == 8);
+#endif
+ return (void *) ptr;
+ }
}
void *
unexec_realloc (void *old_ptr, size_t new_size)
{
if (in_dumped_exec)
- if (ptr_in_unexec_regions (old_ptr))
- {
- char *p = malloc (new_size);
- /* 2002-04-15 T. Ikegami <ikegami@adam.uprr.pr>. The original
- code to get size failed to reallocate read_buffer
- (lread.c). */
- int old_size = malloc_default_zone()->size (emacs_zone, old_ptr);
- int size = new_size > old_size ? old_size : new_size;
-
- if (size)
- memcpy (p, old_ptr, size);
- return p;
- }
- else
- return realloc (old_ptr, new_size);
+ {
+ void *p;
+
+ if (ptr_in_unexec_regions (old_ptr))
+ {
+ 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);
+ if (size)
+ memcpy (p, old_ptr, size);
+ }
+ else
+ {
+ p = realloc (old_ptr, new_size);
+ }
+#if MACOSX_MALLOC_MULT16
+ assert (((vm_address_t) p % 16) == 0);
+#endif
+ return p;
+ }
else
- return malloc_zone_realloc (emacs_zone, old_ptr, new_size);
+ {
+ unexec_malloc_header_t *ptr;
+
+ ptr = (unexec_malloc_header_t *)
+ malloc_zone_realloc (emacs_zone, (unexec_malloc_header_t *) old_ptr - 1,
+ new_size + sizeof (unexec_malloc_header_t));
+ ptr->u.size = new_size;
+ ptr++;
+#if MACOSX_MALLOC_MULT16
+ assert (((vm_address_t) ptr % 16) == 8);
+#endif
+ return (void *) ptr;
+ }
}
void
free (ptr);
}
else
- malloc_zone_free (emacs_zone, ptr);
+ malloc_zone_free (emacs_zone, (unexec_malloc_header_t *) ptr - 1);
}
+
+/* arch-tag: 1a784f7b-a184-4c4f-9544-da8619593d72
+ (do not change this comment) */