1 /* Dump Emacs in macho format.
2 Copyright (C) 1990-1993 Free Software Foundation, Inc.
3 Written by Bradley Taylor (btaylor@next.com).
5 This file is part of XEmacs.
7 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with XEmacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Synched up with: NeXT port */
27 #include <mach/mach.h>
28 #include <mach-o/ldsyms.h>
29 #include <mach-o/loader.h>
33 static void fatal_unexec(char *format, ...)
38 fprintf(stderr, "unexec: ");
39 vfprintf(stderr, format, ap);
40 fprintf(stderr, "\n");
45 static void mcopy(int ffd,int tfd,
46 unsigned long fpos,unsigned long tpos,unsigned long len)
48 if ((ffd==-1)&&(tfd==-1))
68 if (lseek(tfd,tpos,L_SET)<0)
69 fatal_unexec("cannot seek target");
70 if (write(tfd,(void *)fpos,len)!=len)
71 fatal_unexec("cannot write target");
75 if (lseek(ffd,fpos,L_SET)<0)
76 fatal_unexec("cannot seek source");
77 if (read(ffd,(void *)tpos,len)!=len)
78 fatal_unexec("cannot read source");
83 char *buf=alloca(1<<16);
85 if (lseek(ffd,fpos,L_SET)<0)
86 fatal_unexec("cannot seek source");
88 if (lseek(tfd,tpos,L_SET)<0)
89 fatal_unexec("cannot seek target");
91 while((len>0) && (bread=read(ffd,buf,MIN(1<<16,len)))>0)
94 fatal_unexec("cannot read source");
95 if (write(tfd,buf,bread)!=bread)
96 fatal_unexec("cannot write target");
102 static void unexec_doit(int infd,int outfd)
105 extern int malloc_freezedry(void);
113 } *regions=0,*cregion,**pregions;
114 struct mach_header mh;
115 struct segment_command *lc,*sp;
116 struct symtab_command *st;
117 struct section *sect;
119 malloc_cookie=malloc_freezedry();
122 vm_task_t task=task_self();
125 vm_prot_t prot,mprot;
131 for(addr=VM_MIN_ADDRESS,pregions=®ions;
132 vm_region(task,&addr,&size,&prot,&mprot,
133 &inhe,&shrd,&name,&offset)==KERN_SUCCESS;
136 (*pregions)=alloca(sizeof(struct region));
137 (*pregions)->addr=addr;
138 (*pregions)->size=size;
139 (*pregions)->prot=prot;
140 (*pregions)->mprot=mprot;
142 pregions=&((*pregions)->next);
146 for(cregion=regions;cregion;cregion=cregion->next)
147 while ((cregion->next) &&
148 (cregion->next->addr==cregion->addr+cregion->size) &&
149 (cregion->next->prot==cregion->prot) &&
150 (cregion->next->mprot==cregion->mprot))
152 cregion->size += cregion->next->size;
153 cregion->next = cregion->next->next;
156 mcopy(infd,-1,0,(unsigned long) &mh,sizeof(mh));
157 lc=alloca(mh.sizeofcmds);
158 mcopy(infd,-1,sizeof(mh),(unsigned long) lc,mh.sizeofcmds);
160 for(pregions=®ions;*pregions;)
162 if (!((*pregions)->prot&VM_PROT_WRITE)
163 || ((*pregions)->addr>=0x3000000))
168 i++,sp=(struct segment_command *)(((char *)sp)+sp->cmdsize))
171 if (sp->cmd!=LC_SEGMENT||(strcmp(sp->segname,SEG_DATA)==0)) continue;
172 ob=MAX((*pregions)->addr,sp->vmaddr);
173 oe=MIN((*pregions)->addr+(*pregions)->size,sp->vmaddr+sp->vmsize);
174 if (ob >= oe) continue;
175 if (ob==(*pregions)->addr)
176 if (oe==(*pregions)->addr+(*pregions)->size)
182 (*pregions)->addr=oe;
183 (*pregions)->size-=(oe-ob);
186 if (oe==(*pregions)->addr+(*pregions)->size)
188 (*pregions)->size-=(oe-ob);
192 cregion=alloca(sizeof(*cregion));
194 cregion->size=((*pregions)->addr+(*pregions)->size)-oe;
195 cregion->prot=(*pregions)->prot;
196 cregion->mprot=(*pregions)->mprot;
197 cregion->next=(*pregions)->next;
198 (*pregions)->size=ob-(*pregions)->addr;
199 (*pregions)->next=cregion;
202 pregions=&((*pregions)->next);
205 *pregions=(*pregions)->next;
208 for(sp=lc,i=mh.ncmds,hpos=sizeof(mh),opos=0;
210 i--,sp=(struct segment_command *)(((char *)sp)+sp->cmdsize))
214 if (strcmp(sp->segname,SEG_DATA)==0)
220 mcopy(-1,outfd,regions->addr,opos,regions->size);
222 sp->cmdsize=sizeof(*sp);
223 strncpy(sp->segname,SEG_DATA,sizeof(sp->segname));
224 sp->vmaddr=regions->addr;
225 sp->vmsize=regions->size;
226 sp->filesize=regions->size;
227 sp->maxprot=regions->prot;
228 sp->initprot=regions->mprot;
233 mcopy(-1,outfd,(unsigned long)sp,hpos,sp->cmdsize);
236 regions=regions->next;
241 else if (strcmp(sp->segname,SEG_LINKEDIT)==0)
247 mcopy(infd,outfd,sp->fileoff,opos,sp->filesize);
248 sect=(struct section *) (((char *)sp)+sizeof(*sp));
249 for(j=0;j<sp->nsects;j++)
251 if (sect[j].offset!=0)
252 sect[j].offset=(sect[j].offset-sp->fileoff)+opos;
253 if (sect[j].reloff!=0)
254 sect[j].reloff=(sect[j].reloff-sp->fileoff)+opos;
258 mcopy(-1,outfd,(unsigned long)sp,hpos,sp->cmdsize);
263 st=(struct symtab_command *)sp;
265 mcopy(infd,outfd,st->symoff,opos,st->nsyms*sizeof(struct nlist));
267 opos+=sizeof(struct nlist)*st->nsyms;
269 mcopy(infd,outfd,st->stroff,opos,st->strsize);
270 ((struct symtab_command *)sp)->stroff=opos;
271 opos+=((struct symtab_command *)sp)->strsize;
273 mcopy(-1,outfd,(unsigned long)sp,hpos,sp->cmdsize);
276 mh.sizeofcmds=hpos-sizeof(mh);
277 mcopy(-1,outfd,(unsigned long) &mh,0,sizeof(mh));
280 void unexec(char *outfile,char *infile)
282 char tmpfile[MAXPATHLEN];
285 if ((infd=open(infile, O_RDONLY, 0))<0)
286 fatal_unexec("cannot open input file `%s'", infile);
288 strcpy(tmpfile,outfile);
289 strcat(tmpfile,"-temp");
291 if ((outfd=open(tmpfile, O_RDWR|O_TRUNC|O_CREAT, 0755))<0)
292 fatal_unexec("cannot open temporary output file `%s'",tmpfile);
294 unexec_doit(infd,outfd);
298 if (rename(tmpfile, outfile)<0)
301 fatal_unexec("cannot rename `%s' to `%s'", tmpfile, outfile);