annotate src/unexnext.c @ 3094:ad2f4ae9895b

[xemacs-hg @ 2005-11-26 11:45:47 by stephent] Xft merge. <87k6ev4p8q.fsf@tleepslib.sk.tsukuba.ac.jp>
author stephent
date Sat, 26 Nov 2005 11:46:25 +0000
parents ab71ad6ff3dd
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 /* Dump Emacs in macho format.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 Copyright (C) 1990-1993 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 Written by Bradley Taylor (btaylor@next.com).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 Free Software Foundation; either version 2, or (at your option) any
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 along with XEmacs; see the file COPYING. If not, write to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 Boston, MA 02111-1307, USA. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 /* Synched up with: NeXT port */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 #include <stdio.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 #include <libc.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 #include <nlist.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 #include <mach/mach.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 #include <mach-o/ldsyms.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 #include <mach-o/loader.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 int malloc_cookie;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 static void fatal_unexec(char *format, ...)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 va_list ap;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 va_start(ap, format);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 fprintf(stderr, "unexec: ");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 vfprintf(stderr, format, ap);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 fprintf(stderr, "\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 va_end(ap);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 exit(1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 static void mcopy(int ffd,int tfd,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 unsigned long fpos,unsigned long tpos,unsigned long len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 if ((ffd==-1)&&(tfd==-1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 char *f,*t,*e;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 if (fpos>tpos)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 f=(char *)fpos;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 t=(char *)tpos;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 e=(char *)(fpos+len);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 while(f<e) *t++=*f++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 else if (tpos>fpos)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 f=(char *)(fpos+len);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 t=(char *)(tpos+len);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 e=(char *)fpos;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 while(f>e) *--t=*--f;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 else if (ffd==-1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 if (lseek(tfd,tpos,L_SET)<0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 fatal_unexec("cannot seek target");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 if (write(tfd,(void *)fpos,len)!=len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 fatal_unexec("cannot write target");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 else if (tfd==-1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 if (lseek(ffd,fpos,L_SET)<0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 fatal_unexec("cannot seek source");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 if (read(ffd,(void *)tpos,len)!=len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 fatal_unexec("cannot read source");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 int bread;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 char *buf=alloca(1<<16);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 if (lseek(ffd,fpos,L_SET)<0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 fatal_unexec("cannot seek source");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 if (lseek(tfd,tpos,L_SET)<0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 fatal_unexec("cannot seek target");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 while((len>0) && (bread=read(ffd,buf,MIN(1<<16,len)))>0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 if (bread<0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 fatal_unexec("cannot read source");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 if (write(tfd,buf,bread)!=bread)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 fatal_unexec("cannot write target");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 len-=bread;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 static void unexec_doit(int infd,int outfd)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 int i,j,hpos,opos;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 extern int malloc_freezedry(void);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 struct region
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 struct region *next;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 unsigned long addr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 unsigned long size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 vm_prot_t prot;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 vm_prot_t mprot;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 } *regions=0,*cregion,**pregions;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 struct mach_header mh;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 struct segment_command *lc,*sp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 struct symtab_command *st;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 struct section *sect;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 malloc_cookie=malloc_freezedry();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 vm_task_t task=task_self();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 vm_address_t addr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 vm_size_t size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 vm_prot_t prot,mprot;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 vm_inherit_t inhe;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 boolean_t shrd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 port_t name;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 vm_offset_t offset;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 for(addr=VM_MIN_ADDRESS,pregions=&regions;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 vm_region(task,&addr,&size,&prot,&mprot,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 &inhe,&shrd,&name,&offset)==KERN_SUCCESS;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 addr += size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 (*pregions)=alloca(sizeof(struct region));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 (*pregions)->addr=addr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 (*pregions)->size=size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 (*pregions)->prot=prot;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 (*pregions)->mprot=mprot;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 (*pregions)->next=0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 pregions=&((*pregions)->next);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 for(cregion=regions;cregion;cregion=cregion->next)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 while ((cregion->next) &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 (cregion->next->addr==cregion->addr+cregion->size) &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 (cregion->next->prot==cregion->prot) &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 (cregion->next->mprot==cregion->mprot))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 cregion->size += cregion->next->size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 cregion->next = cregion->next->next;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 mcopy(infd,-1,0,(unsigned long) &mh,sizeof(mh));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 lc=alloca(mh.sizeofcmds);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 mcopy(infd,-1,sizeof(mh),(unsigned long) lc,mh.sizeofcmds);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 for(pregions=&regions;*pregions;)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 if (!((*pregions)->prot&VM_PROT_WRITE)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 || ((*pregions)->addr>=0x3000000))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 goto kill_region;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 for(sp=lc,i=0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 i<mh.ncmds;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 i++,sp=(struct segment_command *)(((char *)sp)+sp->cmdsize))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 unsigned long ob,oe;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 if (sp->cmd!=LC_SEGMENT||(strcmp(sp->segname,SEG_DATA)==0)) continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 ob=MAX((*pregions)->addr,sp->vmaddr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 oe=MIN((*pregions)->addr+(*pregions)->size,sp->vmaddr+sp->vmsize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 if (ob >= oe) continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 if (ob==(*pregions)->addr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 if (oe==(*pregions)->addr+(*pregions)->size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 goto kill_region;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 (*pregions)->addr=oe;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 (*pregions)->size-=(oe-ob);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 if (oe==(*pregions)->addr+(*pregions)->size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 (*pregions)->size-=(oe-ob);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 cregion=alloca(sizeof(*cregion));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 cregion->addr=oe;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 cregion->size=((*pregions)->addr+(*pregions)->size)-oe;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 cregion->prot=(*pregions)->prot;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 cregion->mprot=(*pregions)->mprot;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 cregion->next=(*pregions)->next;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 (*pregions)->size=ob-(*pregions)->addr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 (*pregions)->next=cregion;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 pregions=&((*pregions)->next);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 kill_region:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 *pregions=(*pregions)->next;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 for(sp=lc,i=mh.ncmds,hpos=sizeof(mh),opos=0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 i>0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 i--,sp=(struct segment_command *)(((char *)sp)+sp->cmdsize))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 switch (sp->cmd)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 case LC_SEGMENT:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 if (strcmp(sp->segname,SEG_DATA)==0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 mh.ncmds--;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 j=sp->cmdsize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 while (regions)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 mcopy(-1,outfd,regions->addr,opos,regions->size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 sp->cmd=LC_SEGMENT;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 sp->cmdsize=sizeof(*sp);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 strncpy(sp->segname,SEG_DATA,sizeof(sp->segname));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 sp->vmaddr=regions->addr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 sp->vmsize=regions->size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 sp->filesize=regions->size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 sp->maxprot=regions->prot;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 sp->initprot=regions->mprot;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 sp->nsects=0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 sp->flags=0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 sp->fileoff=opos;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 opos+=sp->filesize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 mcopy(-1,outfd,(unsigned long)sp,hpos,sp->cmdsize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 hpos+=sp->cmdsize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 mh.ncmds++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 regions=regions->next;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 sp->cmdsize=j;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 regions=0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 else if (strcmp(sp->segname,SEG_LINKEDIT)==0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 mh.ncmds--;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 mcopy(infd,outfd,sp->fileoff,opos,sp->filesize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 sect=(struct section *) (((char *)sp)+sizeof(*sp));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 for(j=0;j<sp->nsects;j++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 if (sect[j].offset!=0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 sect[j].offset=(sect[j].offset-sp->fileoff)+opos;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 if (sect[j].reloff!=0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 sect[j].reloff=(sect[j].reloff-sp->fileoff)+opos;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 sp->fileoff=opos;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 opos+=sp->filesize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 mcopy(-1,outfd,(unsigned long)sp,hpos,sp->cmdsize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 hpos+=sp->cmdsize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 case LC_SYMTAB:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 st=(struct symtab_command *)sp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 mcopy(infd,outfd,st->symoff,opos,st->nsyms*sizeof(struct nlist));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 st->symoff=opos;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 opos+=sizeof(struct nlist)*st->nsyms;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 mcopy(infd,outfd,st->stroff,opos,st->strsize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 ((struct symtab_command *)sp)->stroff=opos;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 opos+=((struct symtab_command *)sp)->strsize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 default:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 mcopy(-1,outfd,(unsigned long)sp,hpos,sp->cmdsize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 hpos+=sp->cmdsize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 mh.sizeofcmds=hpos-sizeof(mh);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 mcopy(-1,outfd,(unsigned long) &mh,0,sizeof(mh));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 void unexec(char *outfile,char *infile)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 {
2421
ab71ad6ff3dd [xemacs-hg @ 2004-12-06 03:50:53 by ben]
ben
parents: 771
diff changeset
282 char tmpfile[PATH_MAX_EXTERNAL];
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 int infd,outfd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 if ((infd=open(infile, O_RDONLY, 0))<0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 fatal_unexec("cannot open input file `%s'", infile);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 strcpy(tmpfile,outfile);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 strcat(tmpfile,"-temp");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 if ((outfd=open(tmpfile, O_RDWR|O_TRUNC|O_CREAT, 0755))<0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 fatal_unexec("cannot open temporary output file `%s'",tmpfile);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 unexec_doit(infd,outfd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 close(infd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 close(outfd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 if (rename(tmpfile, outfile)<0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 unlink(tmpfile);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 fatal_unexec("cannot rename `%s' to `%s'", tmpfile, outfile);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 }