#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdarg.h>
#include <sys/types.h>
#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>
#include "version.h"
#if _LP64
#define mach_header mach_header_64
#define segment_command segment_command_64
#define vm_region vm_region_64
#define section section_64
#define target_VM_REGION_BASIC_INFO_COUNT VM_REGION_BASIC_INFO_COUNT_64
#define target_VM_REGION_BASIC_INFO VM_REGION_BASIC_INFO_64
#define target_LC_SEGMENT LC_SEGMENT_64
#define target_MH_MAGIC MH_MAGIC_64
#else
#define target_VM_REGION_BASIC_INFO_COUNT VM_REGION_BASIC_INFO_COUNT
#define target_VM_REGION_BASIC_INFO VM_REGION_BASIC_INFO
#define target_LC_SEGMENT LC_SEGMENT
#define target_MH_MAGIC MH_MAGIC
#endif
#define VERBOSE 1
#define UNEXEC_COPY_BUFSZ 1024
#define VM_DATA_TOP (20 * 1024 * 1024)
struct region_t {
vm_address_t address;
vm_size_t size;
vm_prot_t protection;
vm_prot_t max_protection;
struct region_t *next;
};
static struct region_t *region_list_head = 0;
static struct region_t *region_list_tail = 0;
static struct load_command **lca;
static int nlc;
static vm_address_t infile_lc_highest_addr = 0;
static unsigned long text_seg_lowest_offset = 0x10000000;
static struct mach_header mh;
static unsigned long curr_header_offset = sizeof (struct mach_header);
static unsigned long curr_file_offset = 0;
static unsigned long pagesize;
#define ROUNDUP_TO_PAGE_BOUNDARY(x) (((x) + pagesize - 1) & ~(pagesize - 1))
static int infd, outfd;
static int in_dumped_exec = 0;
static malloc_zone_t *emacs_zone;
static off_t data_segment_old_fileoff = 0;
static struct segment_command *data_segment_scp;
static int
unexec_read (void *dest, size_t n)
{
return n == read (infd, dest, n);
}
static int
unexec_write (off_t dest, const void *src, size_t count)
{
if (lseek (outfd, dest, SEEK_SET) != dest)
return 0;
return write (outfd, src, count) == count;
}
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;
}
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];
if (lseek (infd, src, SEEK_SET) != src)
return 0;
if (lseek (outfd, dest, SEEK_SET) != dest)
return 0;
while (count > 0)
{
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)
return 0;
count -= bytes_read;
}
return 1;
}
static void
unexec_error (char *format, ...)
{
va_list ap;
va_start (ap, format);
fprintf (stderr, "unexec: ");
vfprintf (stderr, format, ap);
fprintf (stderr, "\n");
va_end (ap);
exit (1);
}
static void
print_prot (vm_prot_t prot)
{
if (prot == VM_PROT_NONE)
printf ("none");
else
{
putchar (prot & VM_PROT_READ ? 'r' : ' ');
putchar (prot & VM_PROT_WRITE ? 'w' : ' ');
putchar (prot & VM_PROT_EXECUTE ? 'x' : ' ');
putchar (' ');
}
}
static void
print_region (vm_address_t address, vm_size_t size, vm_prot_t prot,
vm_prot_t max_prot)
{
printf ("%#10lx %#8lx ", (long) address, (long) size);
print_prot (prot);
putchar (' ');
print_prot (max_prot);
putchar ('\n');
}
static void
print_region_list ()
{
struct region_t *r;
printf (" address size prot maxp\n");
for (r = region_list_head; r; r = r->next)
print_region (r->address, r->size, r->protection, r->max_protection);
}
static void
print_regions ()
{
task_t target_task = mach_task_self ();
vm_address_t address = (vm_address_t) 0;
vm_size_t size;
struct vm_region_basic_info info;
mach_msg_type_number_t info_count = target_VM_REGION_BASIC_INFO_COUNT;
mach_port_t object_name;
printf (" address size prot maxp\n");
while (vm_region (target_task, &address, &size, VM_REGION_BASIC_INFO,
(vm_region_info_t) &info, &info_count, &object_name)
== KERN_SUCCESS && info_count == target_VM_REGION_BASIC_INFO_COUNT)
{
print_region (address, size, info.protection, info.max_protection);
if (object_name != MACH_PORT_NULL)
mach_port_deallocate (target_task, object_name);
address += size;
}
}
static void
build_region_list ()
{
task_t target_task = mach_task_self ();
vm_address_t address = (vm_address_t) 0;
vm_size_t size;
struct vm_region_basic_info info;
mach_msg_type_number_t info_count = VM_REGION_BASIC_INFO_COUNT;
mach_port_t object_name;
struct region_t *r;
#if VERBOSE
printf ("--- List of All Regions ---\n");
printf (" address size prot maxp\n");
#endif
while (vm_region (target_task, &address, &size, VM_REGION_BASIC_INFO,
(vm_region_info_t) &info, &info_count, &object_name)
== KERN_SUCCESS && info_count == VM_REGION_BASIC_INFO_COUNT)
{
if (address >= VM_DATA_TOP)
break;
#if VERBOSE
print_region (address, size, info.protection, info.max_protection);
#endif
if (region_list_tail
&& info.protection == region_list_tail->protection
&& info.max_protection == region_list_tail->max_protection
&& region_list_tail->address + region_list_tail->size == address)
{
region_list_tail->size += size;
}
else
{
r = (struct region_t *) malloc (sizeof (struct region_t));
if (!r)
unexec_error ("cannot allocate region structure");
r->address = address;
r->size = size;
r->protection = info.protection;
r->max_protection = info.max_protection;
r->next = 0;
if (region_list_head == 0)
{
region_list_head = r;
region_list_tail = r;
}
else
{
region_list_tail->next = r;
region_list_tail = r;
}
if (object_name != MACH_PORT_NULL)
mach_port_deallocate (target_task, object_name);
}
address += size;
}
printf ("--- List of Regions to be Dumped ---\n");
print_region_list ();
}
#define MAX_UNEXEC_REGIONS 400
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)
{
ranges->address &= ~(pagesize-1L);
ranges->size += (ranges->address & (pagesize-1L));
unexec_regions[num_unexec_regions].filesize = ranges->size;
unexec_regions[num_unexec_regions++].range = *ranges;
printf ("%#10lx (sz: %#8lx/%#8lx)\n", (long) (ranges->address),
(long) filesize, (long) (ranges->size));
ranges++; num--;
}
}
static kern_return_t
unexec_reader (task_t task, vm_address_t address, vm_size_t size, void **ptr)
{
*ptr = (void *) address;
return KERN_SUCCESS;
}
static void
find_emacs_zone_regions ()
{
num_unexec_regions = 0;
emacs_zone->introspect->enumerator (mach_task_self(), 0,
MALLOC_PTR_REGION_RANGE_TYPE
| MALLOC_ADMIN_REGION_RANGE_TYPE,
(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 = ((unexec_region_info *) a)->range.address;
vm_address_t bb = ((unexec_region_info *) b)->range.address;
if (aa < bb)
return -1;
else if (aa > bb)
return 1;
else
return 0;
}
static void
unexec_regions_merge ()
{
int i, n;
vm_address_t end;
unexec_region_info r;
qsort (unexec_regions, num_unexec_regions, sizeof (unexec_regions[0]),
&unexec_regions_sort_compare);
n = 0;
r = unexec_regions[0];
for (i = 1; i < num_unexec_regions; i++)
{
if (r.range.address + r.range.size >= unexec_regions[i].range.address
&& r.range.size - r.filesize < 2 * pagesize)
{
r.filesize = r.range.size + unexec_regions[i].filesize;
r.range.size += unexec_regions[i].range.size;
}
else
{
end = r.range.address + r.range.size;
if (end & (pagesize-1L)) {
end = ROUNDUP_TO_PAGE_BOUNDARY(end);
printf("Page (%#8lx) aligning region @%#8lx size from %#8lx to %#8lx\n",
(long)pagesize, (long)r.range.address, (long)r.range.size, (long)(end - r.range.address));
r.range.size = end - r.range.address;
r.filesize = r.range.size;
}
unexec_regions[n++] = r;
r = unexec_regions[i];
}
}
end = r.range.address + r.range.size;
if (end & (pagesize-1L)) {
end = ROUNDUP_TO_PAGE_BOUNDARY(end);
printf("Page (%#8lx) aligning region @%#8lx size from %#8lx to %#8lx\n",
pagesize, (long)r.range.address, (long)r.range.size, (long)(end - r.range.address));
r.range.size = end - r.range.address;
r.filesize = r.range.size;
}
unexec_regions[n++] = r;
num_unexec_regions = n;
}
static void
print_load_command_name (int lc)
{
switch (lc)
{
case LC_SEGMENT:
printf("LC_SEGMENT ");
break;
case LC_SYMTAB:
printf("LC_SYMTAB ");
break;
case LC_SYMSEG:
printf("LC_SYMSEG ");
break;
case LC_UNIXTHREAD:
printf("LC_UNIXTHREAD ");
break;
case LC_DYSYMTAB:
printf("LC_DYSYMTAB ");
break;
case LC_LOAD_DYLIB:
printf("LC_LOAD_DYLIB ");
break;
case LC_ID_DYLIB:
printf("LC_ID_DYLIB ");
break;
case LC_LOAD_DYLINKER:
printf("LC_LOAD_DYLINKER ");
break;
case LC_PREBOUND_DYLIB:
printf("LC_PREBOUND_DYLIB ");
break;
case LC_ROUTINES:
printf("LC_ROUTINES ");
break;
case LC_SUB_FRAMEWORK:
printf("LC_SUBFRAMEWORK ");
break;
case LC_SUB_UMBRELLA:
printf("LC_SUB_UMBRELLA ");
break;
case LC_SUB_CLIENT:
printf("LC_SUB_CLIENT ");
break;
case LC_SUB_LIBRARY:
printf("LC_SUB_LIBRARY ");
break;
case LC_TWOLEVEL_HINTS:
printf("LC_TWOLEVEL_HINTS ");
break;
case LC_PREBIND_CKSUM:
printf("LC_PREBIND_CKSUM ");
break;
case LC_LOAD_WEAK_DYLIB:
printf("LC_LOAD_WEAK_DYLIB ");
break;
case LC_SEGMENT_64:
printf("LC_SEGMENT_64 ");
break;
case LC_ROUTINES_64:
printf("LC_ROUTINES_64 ");
break;
case LC_UUID:
printf("LC_UUID ");
break;
case LC_RPATH:
printf("LC_RPATH ");
break;
case LC_CODE_SIGNATURE:
printf("LC_CODE_SIGNATURE ");
break;
case LC_SEGMENT_SPLIT_INFO:
printf("LC_SEGMENT_SPLIT_INFO ");
break;
case LC_REEXPORT_DYLIB:
printf("LC_REEXPORT_DYLIB ");
break;
case LC_LAZY_LOAD_DYLIB:
printf("LC_LAZY_LOAD_DYLIB ");
break;
case LC_ENCRYPTION_INFO:
printf("LC_ENCRYPTION_INFO ");
break;
case LC_DYLD_INFO:
printf("LC_DYLD_INFO ");
break;
case LC_DYLD_INFO_ONLY:
printf("LC_DYLD_INFO_ONLY ");
break;
case LC_LOAD_UPWARD_DYLIB:
printf("LC_LOAD_UPWARD_DYLIB ");
break;
case LC_VERSION_MIN_MACOSX:
printf("LC_VERSION_MIN_MACOSX ");
break;
case LC_VERSION_MIN_IPHONEOS:
printf("LC_VERSION_MIN_IPHONEOS");
break;
case LC_FUNCTION_STARTS:
printf("LC_FUNCTION_STARTS ");
break;
case LC_DYLD_ENVIRONMENT:
printf("LC_DYLD_ENVIRONMENT ");
break;
case LC_SOURCE_VERSION:
printf("LC_SOURCE_VERSION ");
break;
case LC_DYLIB_CODE_SIGN_DRS:
printf("LC_DYLIB_CODE_SIGN_DRS ");
break;
default:
printf("unknown(%2d) ", lc);
break;
}
}
static void
print_load_command (struct load_command *lc)
{
print_load_command_name (lc->cmd);
printf ("%8d", lc->cmdsize);
if (lc->cmd == LC_SEGMENT || lc->cmd == LC_SEGMENT_64)
{
struct segment_command *scp;
struct section *sectp;
int j;
scp = (struct segment_command *) lc;
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 %#10lx %#8lx (flags: %#8lx)\n",
sectp->sectname, (long) (sectp->addr), (long) (sectp->size), (long) (sectp->flags));
sectp++;
}
}
else
printf ("\n");
}
static void
read_load_commands ()
{
int i;
if (!unexec_read (&mh, sizeof (struct mach_header)))
unexec_error ("cannot read mach-o header");
if (mh.magic != target_MH_MAGIC)
unexec_error ("input file not in correct Mach-O format");
if (mh.filetype != MH_EXECUTE)
unexec_error ("input Mach-O file is not an executable object file");
#if VERBOSE
printf ("--- Header Information ---\n");
printf ("Magic = 0x%08x\n", mh.magic);
printf ("CPUType = %d\n", mh.cputype);
printf ("CPUSubType = %d\n", mh.cpusubtype);
printf ("FileType = 0x%x\n", mh.filetype);
printf ("NCmds = %d\n", mh.ncmds);
printf ("SizeOfCmds = %d\n", mh.sizeofcmds);
printf ("Flags = 0x%08x\n", mh.flags);
#endif
nlc = mh.ncmds;
lca = (struct load_command **) malloc (nlc * sizeof (struct load_command *));
for (i = 0; i < nlc; i++)
{
struct load_command lc;
if (!unexec_read (&lc, sizeof (struct load_command)))
unexec_error ("cannot read load command");
lca[i] = (struct load_command *) 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");
if (lc.cmd == LC_SEGMENT || lc.cmd == LC_SEGMENT_64)
{
struct segment_command *scp = (struct segment_command *) lca[i];
if (scp->vmaddr + scp->vmsize > infile_lc_highest_addr)
infile_lc_highest_addr = scp->vmaddr + scp->vmsize;
if (strncmp (scp->segname, SEG_TEXT, 16) == 0)
{
struct section *sectp = (struct section *) (scp + 1);
int j;
for (j = 0; j < scp->nsects; j++)
if (sectp->offset < text_seg_lowest_offset)
text_seg_lowest_offset = sectp->offset;
}
}
}
printf ("Highest address of load commands in input file: %#8lx\n",
(long)infile_lc_highest_addr);
printf ("Lowest offset of all sections in __TEXT segment: %#8lx\n",
text_seg_lowest_offset);
printf ("--- List of Load Commands in Input File ---\n");
printf ("# cmd cmdsize name address size\n");
for (i = 0; i < nlc; i++)
{
printf ("%1d ", i);
print_load_command (lca[i]);
}
}
static void
copy_segment (struct load_command *lc)
{
struct segment_command *scp = (struct segment_command *) lc;
unsigned long old_fileoff = scp->fileoff;
struct section *sectp;
int j;
scp->fileoff = curr_file_offset;
sectp = (struct section *) (scp + 1);
for (j = 0; j < scp->nsects; j++)
{
sectp->offset += curr_file_offset - old_fileoff;
sectp++;
}
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");
curr_header_offset += lc->cmdsize;
}
static void
copy_data_segment (struct load_command *lc)
{
struct segment_command *scp = (struct segment_command *) lc;
struct section *sectp;
int j;
unsigned long header_offset, old_file_offset;
scp->filesize = scp->vmsize;
printf ("Writing segment %-16.16s @ %#8lx (%#8lx/%#8lx @ %#10lx)\n",
scp->segname, curr_file_offset, (long)(scp->filesize),
(long)(scp->vmsize), (long) (scp->vmaddr));
header_offset = curr_header_offset + sizeof (struct segment_command);
sectp = (struct section *) (scp + 1);
for (j = 0; j < scp->nsects; j++)
{
old_file_offset = sectp->offset;
sectp->offset = sectp->addr - scp->vmaddr + curr_file_offset;
unsigned char sect_type = sectp->flags & SECTION_TYPE;
switch (sect_type) {
case S_LAZY_SYMBOL_POINTERS:
case S_NON_LAZY_SYMBOL_POINTERS:
case S_CSTRING_LITERALS:
if (!unexec_copy (sectp->offset, old_file_offset, sectp->size))
unexec_error ("cannot copy section %s", sectp->sectname);
if (!unexec_write (header_offset, sectp, sizeof (struct section)))
unexec_error ("cannot write section %s's header", sectp->sectname);
break;
case S_REGULAR:
if (strncmp (sectp->sectname, "__const", 16) == 0
|| strncmp(sectp->sectname, "__program_vars", 16) == 0) {
if (!unexec_copy (sectp->offset, old_file_offset, sectp->size))
unexec_error ("cannot copy section %s", sectp->sectname);
} else {
if (!unexec_write (sectp->offset, (void *)sectp->addr, sectp->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);
break;
case S_ZEROFILL:
sectp->flags = S_REGULAR;
if (strncmp (sectp->sectname, SECT_BSS, 16) == 0) {
extern char *my_endbss_static;
unsigned long my_size;
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);
printf("copy SECT_BSS\n");
} else {
if (!unexec_write (sectp->offset, (void *) sectp->addr, sectp->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);
printf("copy %s\n", sectp->sectname);
}
break;
default:
unexec_error("unrecognized section type '0x%x' '%s' in __DATA segment", sect_type, sectp->sectname);
}
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++;
}
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;
for (j = 0; j < num_unexec_regions; j++)
{
struct segment_command sc;
sc.cmd = target_LC_SEGMENT;
sc.cmdsize = sizeof (struct segment_command);
strncpy (sc.segname, SEG_DATA, 16);
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 @ %#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.filesize))
unexec_error ("cannot write new __DATA segment %#8lx (sz: %#8lx)", sc.vmaddr, 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");
curr_header_offset += sc.cmdsize;
mh.ncmds++;
}
}
static void
copy_symtab (struct load_command *lc, long delta)
{
struct symtab_command *stp = (struct symtab_command *) lc;
stp->symoff += delta;
stp->stroff += 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 LC_SYMTAB command to header");
curr_header_offset += lc->cmdsize;
}
static void
copy_dyld_info_only (struct load_command *lc, long delta)
{
struct dyld_info_command *dyld = (struct dyld_info_command *) lc;
if (dyld->rebase_size)
dyld->rebase_off += delta;
if (dyld->bind_size)
dyld->bind_off += delta;
if (dyld->weak_bind_size)
dyld->weak_bind_off += delta;
if (dyld->lazy_bind_size)
dyld->lazy_bind_off += delta;
if (dyld->export_size)
dyld->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 LC_DYLD_INFO_ONLY command to header");
curr_header_offset += lc->cmdsize;
}
static void
copy_linkedit_data (struct load_command *lc, long delta)
{
struct linkedit_data_command *data = (struct linkedit_data_command *) lc;
if (data->dataoff)
data->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 %d command to header", lc->cmd);
curr_header_offset += lc->cmdsize;
}
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:
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);
}
static void
copy_dysymtab (struct load_command *lc, long delta)
{
struct dysymtab_command *dstp = (struct dysymtab_command *) lc;
unrelocate ("local", dstp->locreloff, dstp->nlocrel);
unrelocate ("external", dstp->extreloff, dstp->nextrel);
if (dstp->nextrel > 0) {
dstp->extreloff += delta;
}
if (dstp->nlocrel > 0) {
dstp->locreloff += delta;
}
if (dstp->nindirectsyms > 0)
dstp->indirectsymoff += 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 symtab command to header");
curr_header_offset += lc->cmdsize;
}
static void
copy_twolevelhints (struct load_command *lc, long delta)
{
struct twolevel_hints_command *tlhp = (struct twolevel_hints_command *) lc;
if (tlhp->nhints > 0) {
tlhp->offset += 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 two level hint command to header");
curr_header_offset += lc->cmdsize;
}
static void
copy_other (struct load_command *lc)
{
printf ("Writing ");
print_load_command_name (lc->cmd);
printf (" command\n");
if (lc->cmd == LC_CODE_SIGNATURE)
lc->cmd = 0x0;
if (!unexec_write (curr_header_offset, lc, lc->cmdsize))
unexec_error ("cannot write load command to header");
curr_header_offset += lc->cmdsize;
}
static void
dump_it ()
{
int i;
long linkedit_delta = 0;
printf ("--- Load Commands written to Output File ---\n");
for (i = 0; i < nlc; i++)
switch (lca[i]->cmd)
{
case target_LC_SEGMENT:
{
struct segment_command *scp = (struct segment_command *) lca[i];
if (strncmp (scp->segname, SEG_DATA, 16) == 0)
{
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], linkedit_delta);
break;
case LC_DYSYMTAB:
copy_dysymtab (lca[i], linkedit_delta);
break;
case LC_TWOLEVEL_HINTS:
copy_twolevelhints (lca[i], linkedit_delta);
break;
case LC_DYLD_INFO_ONLY:
copy_dyld_info_only(lca[i], linkedit_delta);
break;
case LC_CODE_SIGNATURE:
case LC_SEGMENT_SPLIT_INFO:
case LC_FUNCTION_STARTS:
case LC_DATA_IN_CODE:
case LC_DYLIB_CODE_SIGN_DRS:
copy_linkedit_data(lca[i], linkedit_delta);
break;
default:
copy_other (lca[i]);
break;
}
if (curr_header_offset > text_seg_lowest_offset)
unexec_error ("not enough room for load commands for new __DATA segments");
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);
if (!unexec_write (0, &mh, sizeof (struct mach_header)))
unexec_error ("cannot write final header contents");
}
void
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)
{
unexec_error ("cannot open input file `%s'", infile);
}
outfd = open (outfile, O_WRONLY | O_TRUNC | O_CREAT, 0755);
if (outfd < 0)
{
close (infd);
unexec_error ("cannot open output file `%s'", outfile);
}
build_region_list ();
read_load_commands ();
find_emacs_zone_regions ();
unexec_regions_merge ();
in_dumped_exec = 1;
dump_it ();
close (outfd);
}
void
unexec_init_emacs_zone ()
{
emacs_zone = malloc_create_zone (0, 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].range.address
< unexec_regions[i].range.size)
return 1;
return 0;
}
#endif
void *
unexec_malloc (size_t size)
{
if (in_dumped_exec)
{
void *p;
p = malloc (size);
#if MACOSX_MALLOC_MULT16
assert (((vm_address_t) p % 16) == 0);
#endif
return p;
}
else
{
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)
{
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
{
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
unexec_free (void *ptr)
{
if (in_dumped_exec)
{
if (!ptr_in_unexec_regions (ptr))
free (ptr);
}
else
malloc_zone_free (emacs_zone, (unexec_malloc_header_t *) ptr - 1);
}