Mercurial > hg > xemacs-beta
comparison src/unexalpha.c @ 0:376386a54a3c r19-14
Import from CVS: tag r19-14
author | cvs |
---|---|
date | Mon, 13 Aug 2007 08:45:50 +0200 |
parents | |
children | 8eaf7971accc |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:376386a54a3c |
---|---|
1 /* Unexec for DEC alpha. schoepf@sc.ZIB-Berlin.DE (Rainer Schoepf). | |
2 | |
3 Copyright (C) 1994 Free Software Foundation, Inc. | |
4 | |
5 This file is part of GNU Emacs. | |
6 | |
7 GNU Emacs is free software; you can redistribute it and/or modify | |
8 it under the terms of the GNU General Public License as published by | |
9 the Free Software Foundation; either version 2, or (at your option) | |
10 any later version. | |
11 | |
12 GNU Emacs is distributed in the hope that it will be useful, | |
13 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 GNU General Public License for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
18 along with GNU Emacs; 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. */ | |
21 | |
22 /* Synched up with: FSF 19.31. */ | |
23 | |
24 | |
25 #include <config.h> | |
26 #include <sys/types.h> | |
27 #include <sys/file.h> | |
28 #include <sys/stat.h> | |
29 #include <sys/mman.h> | |
30 #include <stdio.h> | |
31 #include <varargs.h> | |
32 #include <filehdr.h> | |
33 #include <aouthdr.h> | |
34 #include <scnhdr.h> | |
35 #include <syms.h> | |
36 | |
37 static void fatal_unexec (); | |
38 static void mark_x (); | |
39 | |
40 #define READ(_fd, _buffer, _size, _error_message, _error_arg) \ | |
41 errno = EEOF; \ | |
42 if (read (_fd, _buffer, _size) != _size) \ | |
43 fatal_unexec (_error_message, _error_arg); | |
44 | |
45 #define WRITE(_fd, _buffer, _size, _error_message, _error_arg) \ | |
46 if (write (_fd, _buffer, _size) != _size) \ | |
47 fatal_unexec (_error_message, _error_arg); | |
48 | |
49 #define SEEK(_fd, _position, _error_message, _error_arg) \ | |
50 errno = EEOF; \ | |
51 if (lseek (_fd, _position, L_SET) != _position) \ | |
52 fatal_unexec (_error_message, _error_arg); | |
53 | |
54 extern int errno; | |
55 extern char *strerror (); | |
56 | |
57 void *sbrk(); | |
58 | |
59 #define EEOF -1 | |
60 | |
61 static struct scnhdr *text_section; | |
62 static struct scnhdr *init_section; | |
63 static struct scnhdr *finit_section; | |
64 static struct scnhdr *rdata_section; | |
65 static struct scnhdr *rconst_section; | |
66 static struct scnhdr *data_section; | |
67 static struct scnhdr *pdata_section; | |
68 static struct scnhdr *xdata_section; | |
69 static struct scnhdr *got_section; | |
70 static struct scnhdr *lit8_section; | |
71 static struct scnhdr *lit4_section; | |
72 static struct scnhdr *sdata_section; | |
73 static struct scnhdr *sbss_section; | |
74 static struct scnhdr *bss_section; | |
75 | |
76 static unsigned long Brk; | |
77 | |
78 struct headers { | |
79 struct filehdr fhdr; | |
80 struct aouthdr aout; | |
81 struct scnhdr section[_MIPS_NSCNS_MAX]; | |
82 }; | |
83 | |
84 | |
85 | |
86 /* Define name of label for entry point for the dumped executable. */ | |
87 | |
88 #ifndef DEFAULT_ENTRY_ADDRESS | |
89 #define DEFAULT_ENTRY_ADDRESS __start | |
90 #endif | |
91 | |
92 unexec (new_name, a_name, data_start, bss_start, entry_address) | |
93 char *new_name, *a_name; | |
94 unsigned long data_start, bss_start, entry_address; | |
95 { | |
96 int new, old; | |
97 char * oldptr; | |
98 struct headers ohdr, nhdr; | |
99 struct stat stat; | |
100 long pagesize, brk; | |
101 long newsyms, symrel; | |
102 int nread; | |
103 int i; | |
104 long vaddr, scnptr; | |
105 #define BUFSIZE 8192 | |
106 char buffer[BUFSIZE]; | |
107 | |
108 if ((old = open (a_name, O_RDONLY)) < 0) | |
109 fatal_unexec ("opening %s", a_name); | |
110 | |
111 new = creat (new_name, 0666); | |
112 if (new < 0) fatal_unexec ("creating %s", new_name); | |
113 | |
114 if ((fstat (old, &stat) == -1)) | |
115 fatal_unexec ("fstat %s", a_name); | |
116 | |
117 oldptr = (char *)mmap (0, stat.st_size, PROT_READ, MAP_FILE|MAP_SHARED, old, 0); | |
118 | |
119 if (oldptr == (char *)-1) | |
120 fatal_unexec ("mmap %s", a_name); | |
121 | |
122 close (old); | |
123 | |
124 /* This is a copy of the a.out header of the original executable */ | |
125 | |
126 ohdr = (*(struct headers *)oldptr); | |
127 | |
128 /* This is where we build the new header from the in-memory copy */ | |
129 | |
130 nhdr = *((struct headers *)TEXT_START); | |
131 | |
132 /* First do some consistency checks */ | |
133 | |
134 if (nhdr.fhdr.f_magic != ALPHAMAGIC | |
135 && nhdr.fhdr.f_magic != ALPHAUMAGIC) | |
136 { | |
137 fprintf (stderr, "unexec: input file magic number is %x, not %x or %x.\n", | |
138 nhdr.fhdr.f_magic, ALPHAMAGIC, ALPHAUMAGIC); | |
139 exit (1); | |
140 } | |
141 | |
142 if (nhdr.fhdr.f_opthdr != sizeof (nhdr.aout)) | |
143 { | |
144 fprintf (stderr, "unexec: input a.out header is %d bytes, not %d.\n", | |
145 nhdr.fhdr.f_opthdr, sizeof (nhdr.aout)); | |
146 exit (1); | |
147 } | |
148 if (nhdr.aout.magic != ZMAGIC) | |
149 { | |
150 fprintf (stderr, "unexec: input file a.out magic number is %o, not %o.\n", | |
151 nhdr.aout.magic, ZMAGIC); | |
152 exit (1); | |
153 } | |
154 | |
155 | |
156 /* Now check the existence of certain header section and grab | |
157 their addresses. */ | |
158 | |
159 #define CHECK_SCNHDR(ptr, name, flags) \ | |
160 ptr = NULL; \ | |
161 for (i = 0; i < nhdr.fhdr.f_nscns && !ptr; i++) \ | |
162 if (strcmp (nhdr.section[i].s_name, name) == 0) \ | |
163 { \ | |
164 if (nhdr.section[i].s_flags != flags) \ | |
165 fprintf (stderr, "unexec: %x flags (%x expected) in %s section.\n", \ | |
166 nhdr.section[i].s_flags, flags, name); \ | |
167 ptr = nhdr.section + i; \ | |
168 } \ | |
169 | |
170 CHECK_SCNHDR (text_section, _TEXT, STYP_TEXT); | |
171 CHECK_SCNHDR (init_section, _INIT, STYP_INIT); | |
172 #ifdef _FINI | |
173 CHECK_SCNHDR (finit_section, _FINI, STYP_FINI); | |
174 #endif /* _FINI */ | |
175 CHECK_SCNHDR (rdata_section, _RDATA, STYP_RDATA); | |
176 #ifdef _RCONST | |
177 CHECK_SCNHDR (rconst_section, _RCONST, STYP_RCONST); | |
178 #endif | |
179 #ifdef _PDATA | |
180 CHECK_SCNHDR (pdata_section, _PDATA, STYP_PDATA); | |
181 #endif /* _PDATA */ | |
182 #ifdef _GOT | |
183 CHECK_SCNHDR (got_section, _GOT, STYP_GOT); | |
184 #endif /* _GOT */ | |
185 CHECK_SCNHDR (data_section, _DATA, STYP_DATA); | |
186 #ifdef _XDATA | |
187 CHECK_SCNHDR (xdata_section, _XDATA, STYP_XDATA); | |
188 #endif /* _XDATA */ | |
189 #ifdef _LIT8 | |
190 CHECK_SCNHDR (lit8_section, _LIT8, STYP_LIT8); | |
191 CHECK_SCNHDR (lit4_section, _LIT4, STYP_LIT4); | |
192 #endif /* _LIT8 */ | |
193 CHECK_SCNHDR (sdata_section, _SDATA, STYP_SDATA); | |
194 CHECK_SCNHDR (sbss_section, _SBSS, STYP_SBSS); | |
195 CHECK_SCNHDR (bss_section, _BSS, STYP_BSS); | |
196 | |
197 | |
198 pagesize = getpagesize (); | |
199 brk = (((long) (sbrk (0))) + pagesize - 1) & (-pagesize); | |
200 | |
201 /* Remember the current break */ | |
202 | |
203 Brk = brk; | |
204 | |
205 nhdr.aout.dsize = brk - DATA_START; | |
206 nhdr.aout.bsize = 0; | |
207 if (entry_address == 0) | |
208 { | |
209 extern DEFAULT_ENTRY_ADDRESS (); | |
210 nhdr.aout.entry = (unsigned long)DEFAULT_ENTRY_ADDRESS; | |
211 } | |
212 else | |
213 nhdr.aout.entry = entry_address; | |
214 | |
215 nhdr.aout.bss_start = nhdr.aout.data_start + nhdr.aout.dsize; | |
216 | |
217 if (rdata_section != NULL) | |
218 { | |
219 rdata_section->s_size = data_start - DATA_START; | |
220 | |
221 /* Adjust start and virtual addresses of rdata_section, too. */ | |
222 rdata_section->s_vaddr = DATA_START; | |
223 rdata_section->s_paddr = DATA_START; | |
224 rdata_section->s_scnptr = text_section->s_scnptr + nhdr.aout.tsize; | |
225 } | |
226 | |
227 data_section->s_vaddr = data_start; | |
228 data_section->s_paddr = data_start; | |
229 data_section->s_size = brk - data_start; | |
230 | |
231 if (rdata_section != NULL) | |
232 { | |
233 data_section->s_scnptr = rdata_section->s_scnptr + rdata_section->s_size; | |
234 } | |
235 | |
236 vaddr = data_section->s_vaddr + data_section->s_size; | |
237 scnptr = data_section->s_scnptr + data_section->s_size; | |
238 if (lit8_section != NULL) | |
239 { | |
240 lit8_section->s_vaddr = vaddr; | |
241 lit8_section->s_paddr = vaddr; | |
242 lit8_section->s_size = 0; | |
243 lit8_section->s_scnptr = scnptr; | |
244 } | |
245 if (lit4_section != NULL) | |
246 { | |
247 lit4_section->s_vaddr = vaddr; | |
248 lit4_section->s_paddr = vaddr; | |
249 lit4_section->s_size = 0; | |
250 lit4_section->s_scnptr = scnptr; | |
251 } | |
252 if (sdata_section != NULL) | |
253 { | |
254 sdata_section->s_vaddr = vaddr; | |
255 sdata_section->s_paddr = vaddr; | |
256 sdata_section->s_size = 0; | |
257 sdata_section->s_scnptr = scnptr; | |
258 } | |
259 #ifdef _XDATA | |
260 if (xdata_section != NULL) | |
261 { | |
262 xdata_section->s_vaddr = vaddr; | |
263 xdata_section->s_paddr = vaddr; | |
264 xdata_section->s_size = 0; | |
265 xdata_section->s_scnptr = scnptr; | |
266 } | |
267 #endif | |
268 #ifdef _GOT | |
269 if (got_section != NULL) | |
270 { | |
271 got_section->s_vaddr = vaddr; | |
272 got_section->s_paddr = vaddr; | |
273 got_section->s_size = 0; | |
274 got_section->s_scnptr = scnptr; | |
275 } | |
276 #endif /*_GOT */ | |
277 if (sbss_section != NULL) | |
278 { | |
279 sbss_section->s_vaddr = vaddr; | |
280 sbss_section->s_paddr = vaddr; | |
281 sbss_section->s_size = 0; | |
282 sbss_section->s_scnptr = scnptr; | |
283 } | |
284 if (bss_section != NULL) | |
285 { | |
286 bss_section->s_vaddr = vaddr; | |
287 bss_section->s_paddr = vaddr; | |
288 bss_section->s_size = 0; | |
289 bss_section->s_scnptr = scnptr; | |
290 } | |
291 | |
292 WRITE (new, (char *)TEXT_START, nhdr.aout.tsize, | |
293 "writing text section to %s", new_name); | |
294 WRITE (new, (char *)DATA_START, nhdr.aout.dsize, | |
295 "writing data section to %s", new_name); | |
296 | |
297 | |
298 /* | |
299 * Construct new symbol table header | |
300 */ | |
301 | |
302 bcopy (oldptr + nhdr.fhdr.f_symptr, buffer, cbHDRR); | |
303 | |
304 #define symhdr ((pHDRR)buffer) | |
305 newsyms = nhdr.aout.tsize + nhdr.aout.dsize; | |
306 symrel = newsyms - nhdr.fhdr.f_symptr; | |
307 nhdr.fhdr.f_symptr = newsyms; | |
308 symhdr->cbLineOffset += symrel; | |
309 symhdr->cbDnOffset += symrel; | |
310 symhdr->cbPdOffset += symrel; | |
311 symhdr->cbSymOffset += symrel; | |
312 symhdr->cbOptOffset += symrel; | |
313 symhdr->cbAuxOffset += symrel; | |
314 symhdr->cbSsOffset += symrel; | |
315 symhdr->cbSsExtOffset += symrel; | |
316 symhdr->cbFdOffset += symrel; | |
317 symhdr->cbRfdOffset += symrel; | |
318 symhdr->cbExtOffset += symrel; | |
319 | |
320 WRITE (new, buffer, cbHDRR, "writing symbol table header of %s", new_name); | |
321 | |
322 /* | |
323 * Copy the symbol table and line numbers | |
324 */ | |
325 WRITE (new, oldptr + ohdr.fhdr.f_symptr + cbHDRR, | |
326 stat.st_size - ohdr.fhdr.f_symptr - cbHDRR, | |
327 "writing symbol table of %s", new_name); | |
328 | |
329 #if 0 | |
330 | |
331 /* Not needed for now */ | |
332 | |
333 update_dynamic_symbols (oldptr, new_name, new, newsyms, | |
334 ((pHDRR) (oldptr + ohdr.fhdr.f_symptr))->issExtMax, | |
335 ((pHDRR) (oldptr + ohdr.fhdr.f_symptr))->cbExtOffset, | |
336 ((pHDRR) (oldptr + ohdr.fhdr.f_symptr))->cbSsExtOffset); | |
337 | |
338 #endif | |
339 | |
340 #undef symhdr | |
341 | |
342 SEEK (new, 0, "seeking to start of header in %s", new_name); | |
343 WRITE (new, &nhdr, sizeof (nhdr), | |
344 "writing header of %s", new_name); | |
345 | |
346 close (old); | |
347 close (new); | |
348 mark_x (new_name); | |
349 } | |
350 | |
351 | |
352 #if 0 | |
353 | |
354 /* Not needed for now */ | |
355 | |
356 /* The following function updates the values of some symbols | |
357 that are used by the dynamic loader: | |
358 | |
359 _edata | |
360 _end | |
361 | |
362 */ | |
363 | |
364 | |
365 update_dynamic_symbols (old, new_name, new, newsyms, nsyms, symoff, stroff) | |
366 char *old; /* Pointer to old executable */ | |
367 char *new_name; /* Name of new executable */ | |
368 int new; /* File descriptor for new executable */ | |
369 long newsyms; /* Offset of Symbol table in new executable */ | |
370 int nsyms; /* Number of symbol table entries */ | |
371 long symoff; /* Offset of External Symbols in old file */ | |
372 long stroff; /* Offset of string table in old file */ | |
373 { | |
374 long i; | |
375 int found = 0; | |
376 EXTR n_end, n_edata; | |
377 | |
378 /* We go through the symbol table entries until we have found the two | |
379 symbols. */ | |
380 | |
381 /* cbEXTR is the size of an external symbol table entry */ | |
382 | |
383 for (i = 0; i < nsyms && found < 2; i += cbEXTR) | |
384 { | |
385 register pEXTR x = (pEXTR) (old + symoff + i); | |
386 char *s; | |
387 | |
388 s = old + stroff + x->asym.iss; /* name of the symbol */ | |
389 | |
390 if (!strcmp(s,"_edata")) | |
391 { | |
392 found++; | |
393 bcopy (x, &n_edata, cbEXTR); | |
394 n_edata.asym.value = Brk; | |
395 SEEK (new, newsyms + cbHDRR + i, | |
396 "seeking to symbol _edata in %s", new_name); | |
397 WRITE (new, &n_edata, cbEXTR, | |
398 "writing symbol table entry for _edata into %s", new_name); | |
399 } | |
400 else if (!strcmp(s,"_end")) | |
401 { | |
402 found++; | |
403 bcopy (x, &n_end, cbEXTR); | |
404 n_end.asym.value = Brk; | |
405 SEEK (new, newsyms + cbHDRR + i, | |
406 "seeking to symbol _end in %s", new_name); | |
407 WRITE (new, &n_end, cbEXTR, | |
408 "writing symbol table entry for _end into %s", new_name); | |
409 } | |
410 } | |
411 | |
412 } | |
413 | |
414 #endif | |
415 | |
416 | |
417 /* | |
418 * mark_x | |
419 * | |
420 * After successfully building the new a.out, mark it executable | |
421 */ | |
422 | |
423 static void | |
424 mark_x (name) | |
425 char *name; | |
426 { | |
427 struct stat sbuf; | |
428 int um = umask (777); | |
429 umask (um); | |
430 if (stat (name, &sbuf) < 0) | |
431 fatal_unexec ("getting protection on %s", name); | |
432 sbuf.st_mode |= 0111 & ~um; | |
433 if (chmod (name, sbuf.st_mode) < 0) | |
434 fatal_unexec ("setting protection on %s", name); | |
435 } | |
436 | |
437 static void | |
438 fatal_unexec (s, arg) | |
439 char *s; | |
440 char *arg; | |
441 { | |
442 if (errno == EEOF) | |
443 fputs ("unexec: unexpected end of file, ", stderr); | |
444 else | |
445 fprintf (stderr, "unexec: %s, ", strerror (errno)); | |
446 fprintf (stderr, s, arg); | |
447 fputs (".\n", stderr); | |
448 exit (1); | |
449 } |