]> code.delx.au - gnu-emacs/blob - src/unexaix.c
* pre-crt0.c (data_start): Initialize to 1.
[gnu-emacs] / src / unexaix.c
1 /* Dump an executable image.
2 Copyright (C) 1985-1988, 1999, 2001-2013 Free Software Foundation,
3 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 3 of the License, or
10 (at your option) 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. If not, see <http://www.gnu.org/licenses/>. */
19
20 /*
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding! */
24
25
26 /* Originally based on the COFF unexec.c by Spencer W. Thomas.
27 *
28 * Subsequently hacked on by
29 * Bill Mann <Bill_Man@praxisint.com>
30 * Andrew Vignaux <Andrew.Vignaux@comp.vuw.ac.nz>
31 * Mike Sperber <sperber@informatik.uni-tuebingen.de>
32 *
33 * Synopsis:
34 * unexec (const char *new_name, const *old_name);
35 *
36 * Takes a snapshot of the program and makes an a.out format file in the
37 * file named by the string argument new_name.
38 * If a_name is non-NULL, the symbol table will be taken from the given file.
39 * On some machines, an existing a_name file is required.
40 *
41 */
42
43 #include <config.h>
44 #include "unexec.h"
45
46 #define PERROR(file) report_error (file, new)
47 #include <a.out.h>
48 /* Define getpagesize () if the system does not.
49 Note that this may depend on symbols defined in a.out.h
50 */
51 #include "getpagesize.h"
52
53 #include <sys/types.h>
54 #include <inttypes.h>
55 #include <stdarg.h>
56 #include <stdio.h>
57 #include <sys/stat.h>
58 #include <errno.h>
59 #include <unistd.h>
60 #include <fcntl.h>
61
62 #include "mem-limits.h"
63
64 char *start_of_text (void); /* Start of text */
65
66 extern int _data;
67 extern int _text;
68
69 #include <filehdr.h>
70 #include <aouthdr.h>
71 #include <scnhdr.h>
72 #include <syms.h>
73
74 static struct filehdr f_hdr; /* File header */
75 static struct aouthdr f_ohdr; /* Optional file header (a.out) */
76 static long bias; /* Bias to add for growth */
77 static long lnnoptr; /* Pointer to line-number info within file */
78
79 static long text_scnptr;
80 static long data_scnptr;
81 #define ALIGN(val, pwr) (((val) + ((1L<<(pwr))-1)) & ~((1L<<(pwr))-1))
82 static long load_scnptr;
83 static long orig_load_scnptr;
84 static long orig_data_scnptr;
85 static int unrelocate_symbols (int, int, const char *, const char *);
86
87 #ifndef MAX_SECTIONS
88 #define MAX_SECTIONS 10
89 #endif
90
91 static int adjust_lnnoptrs (int, int, const char *);
92
93 static int pagemask;
94
95 #include "lisp.h"
96
97 static _Noreturn void
98 report_error (const char *file, int fd)
99 {
100 if (fd)
101 {
102 int failed_errno = errno;
103 close (fd);
104 errno = failed_errno;
105 }
106 report_file_error ("Cannot unexec", Fcons (build_string (file), Qnil));
107 }
108
109 #define ERROR0(msg) report_error_1 (new, msg)
110 #define ERROR1(msg,x) report_error_1 (new, msg, x)
111 #define ERROR2(msg,x,y) report_error_1 (new, msg, x, y)
112
113 static _Noreturn void ATTRIBUTE_FORMAT_PRINTF (2, 3)
114 report_error_1 (int fd, const char *msg, ...)
115 {
116 va_list ap;
117 close (fd);
118 va_start (ap, msg);
119 verror (msg, ap);
120 va_end (ap);
121 }
122
123 static int make_hdr (int, int, const char *, const char *);
124 static void mark_x (const char *);
125 static int copy_text_and_data (int);
126 static int copy_sym (int, int, const char *, const char *);
127 static void write_segment (int, char *, char *);
128 \f
129 /* ****************************************************************
130 * unexec
131 *
132 * driving logic.
133 */
134 void
135 unexec (const char *new_name, const char *a_name)
136 {
137 int new = -1, a_out = -1;
138
139 if (a_name && (a_out = open (a_name, O_RDONLY)) < 0)
140 {
141 PERROR (a_name);
142 }
143 if ((new = creat (new_name, 0666)) < 0)
144 {
145 PERROR (new_name);
146 }
147 if (make_hdr (new, a_out,
148 a_name, new_name) < 0
149 || copy_text_and_data (new) < 0
150 || copy_sym (new, a_out, a_name, new_name) < 0
151 || adjust_lnnoptrs (new, a_out, new_name) < 0
152 || unrelocate_symbols (new, a_out, a_name, new_name) < 0)
153 {
154 close (new);
155 return;
156 }
157
158 close (new);
159 if (a_out >= 0)
160 close (a_out);
161 mark_x (new_name);
162 }
163
164 /* ****************************************************************
165 * make_hdr
166 *
167 * Make the header in the new a.out from the header in core.
168 * Modify the text and data sizes.
169 */
170 static int
171 make_hdr (int new, int a_out,
172 const char *a_name, const char *new_name)
173 {
174 int scns;
175 uintptr_t bss_start;
176 uintptr_t data_start;
177
178 struct scnhdr section[MAX_SECTIONS];
179 struct scnhdr * f_thdr; /* Text section header */
180 struct scnhdr * f_dhdr; /* Data section header */
181 struct scnhdr * f_bhdr; /* Bss section header */
182 struct scnhdr * f_lhdr; /* Loader section header */
183 struct scnhdr * f_tchdr; /* Typechk section header */
184 struct scnhdr * f_dbhdr; /* Debug section header */
185 struct scnhdr * f_xhdr; /* Except section header */
186
187 load_scnptr = orig_load_scnptr = lnnoptr = 0;
188 pagemask = getpagesize () - 1;
189
190 /* Adjust text/data boundary. */
191 data_start = (uintptr_t) start_of_data ();
192
193 data_start = data_start & ~pagemask; /* (Down) to page boundary. */
194
195 bss_start = (uintptr_t) sbrk (0) + pagemask;
196 bss_start &= ~ pagemask;
197
198 if (data_start > bss_start) /* Can't have negative data size. */
199 {
200 ERROR2 (("unexec: data_start (0x%"PRIxPTR
201 ") can't be greater than bss_start (0x%"PRIxPTR")"),
202 data_start, bss_start);
203 }
204
205 /* Salvage as much info from the existing file as possible */
206 f_thdr = NULL; f_dhdr = NULL; f_bhdr = NULL;
207 f_lhdr = NULL; f_tchdr = NULL; f_dbhdr = NULL; f_xhdr = NULL;
208 if (a_out >= 0)
209 {
210 if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
211 {
212 PERROR (a_name);
213 }
214 if (f_hdr.f_opthdr > 0)
215 {
216 if (read (a_out, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
217 {
218 PERROR (a_name);
219 }
220 }
221 if (f_hdr.f_nscns > MAX_SECTIONS)
222 {
223 ERROR0 ("unexec: too many section headers -- increase MAX_SECTIONS");
224 }
225 /* Loop through section headers */
226 for (scns = 0; scns < f_hdr.f_nscns; scns++) {
227 struct scnhdr *s = &section[scns];
228 if (read (a_out, s, sizeof (*s)) != sizeof (*s))
229 {
230 PERROR (a_name);
231 }
232
233 #define CHECK_SCNHDR(ptr, name, flags) \
234 if (strcmp (s->s_name, name) == 0) { \
235 if (s->s_flags != flags) { \
236 fprintf (stderr, "unexec: %lx flags where %x expected in %s section.\n", \
237 (unsigned long)s->s_flags, flags, name); \
238 } \
239 if (ptr) { \
240 fprintf (stderr, "unexec: duplicate section header for section %s.\n", \
241 name); \
242 } \
243 ptr = s; \
244 }
245 CHECK_SCNHDR (f_thdr, _TEXT, STYP_TEXT);
246 CHECK_SCNHDR (f_dhdr, _DATA, STYP_DATA);
247 CHECK_SCNHDR (f_bhdr, _BSS, STYP_BSS);
248 CHECK_SCNHDR (f_lhdr, _LOADER, STYP_LOADER);
249 CHECK_SCNHDR (f_dbhdr, _DEBUG, STYP_DEBUG);
250 CHECK_SCNHDR (f_tchdr, _TYPCHK, STYP_TYPCHK);
251 CHECK_SCNHDR (f_xhdr, _EXCEPT, STYP_EXCEPT);
252 }
253
254 if (f_thdr == 0)
255 {
256 ERROR1 ("unexec: couldn't find \"%s\" section", (int) _TEXT);
257 }
258 if (f_dhdr == 0)
259 {
260 ERROR1 ("unexec: couldn't find \"%s\" section", (int) _DATA);
261 }
262 if (f_bhdr == 0)
263 {
264 ERROR1 ("unexec: couldn't find \"%s\" section", (int) _BSS);
265 }
266 }
267 else
268 {
269 ERROR0 ("can't build a COFF file from scratch yet");
270 }
271 orig_data_scnptr = f_dhdr->s_scnptr;
272 orig_load_scnptr = f_lhdr ? f_lhdr->s_scnptr : 0;
273
274 /* Now we alter the contents of all the f_*hdr variables
275 to correspond to what we want to dump. */
276
277 /* Indicate that the reloc information is no longer valid for ld (bind);
278 we only update it enough to fake out the exec-time loader. */
279 f_hdr.f_flags |= (F_RELFLG | F_EXEC);
280
281 f_ohdr.dsize = bss_start - f_ohdr.data_start;
282 f_ohdr.bsize = 0;
283
284 f_dhdr->s_size = f_ohdr.dsize;
285 f_bhdr->s_size = f_ohdr.bsize;
286 f_bhdr->s_paddr = f_ohdr.data_start + f_ohdr.dsize;
287 f_bhdr->s_vaddr = f_ohdr.data_start + f_ohdr.dsize;
288
289 /* fix scnptr's */
290 {
291 ulong ptr = section[0].s_scnptr;
292
293 bias = -1;
294 for (scns = 0; scns < f_hdr.f_nscns; scns++)
295 {
296 struct scnhdr *s = &section[scns];
297
298 if (s->s_flags & STYP_PAD) /* .pad sections omitted in AIX 4.1 */
299 {
300 /*
301 * the text_start should probably be o_algntext but that doesn't
302 * seem to change
303 */
304 if (f_ohdr.text_start != 0) /* && scns != 0 */
305 {
306 s->s_size = 512 - (ptr % 512);
307 if (s->s_size == 512)
308 s->s_size = 0;
309 }
310 s->s_scnptr = ptr;
311 }
312 else if (s->s_flags & STYP_DATA)
313 s->s_scnptr = ptr;
314 else if (!(s->s_flags & (STYP_TEXT | STYP_BSS)))
315 {
316 if (bias == -1) /* if first section after bss */
317 bias = ptr - s->s_scnptr;
318
319 s->s_scnptr += bias;
320 ptr = s->s_scnptr;
321 }
322
323 ptr = ptr + s->s_size;
324 }
325 }
326
327 /* fix other pointers */
328 for (scns = 0; scns < f_hdr.f_nscns; scns++)
329 {
330 struct scnhdr *s = &section[scns];
331
332 if (s->s_relptr != 0)
333 {
334 s->s_relptr += bias;
335 }
336 if (s->s_lnnoptr != 0)
337 {
338 if (lnnoptr == 0) lnnoptr = s->s_lnnoptr;
339 s->s_lnnoptr += bias;
340 }
341 }
342
343 if (f_hdr.f_symptr > 0L)
344 {
345 f_hdr.f_symptr += bias;
346 }
347
348 text_scnptr = f_thdr->s_scnptr;
349 data_scnptr = f_dhdr->s_scnptr;
350 load_scnptr = f_lhdr ? f_lhdr->s_scnptr : 0;
351
352 if (write (new, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
353 {
354 PERROR (new_name);
355 }
356
357 if (f_hdr.f_opthdr > 0)
358 {
359 if (write (new, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
360 {
361 PERROR (new_name);
362 }
363 }
364
365 for (scns = 0; scns < f_hdr.f_nscns; scns++) {
366 struct scnhdr *s = &section[scns];
367 if (write (new, s, sizeof (*s)) != sizeof (*s))
368 {
369 PERROR (new_name);
370 }
371 }
372
373 return (0);
374 }
375 \f
376 /* ****************************************************************
377
378 *
379 * Copy the text and data segments from memory to the new a.out
380 */
381 static int
382 copy_text_and_data (int new)
383 {
384 char *end;
385 char *ptr;
386
387 lseek (new, (long) text_scnptr, SEEK_SET);
388 ptr = start_of_text () + text_scnptr;
389 end = ptr + f_ohdr.tsize;
390 write_segment (new, ptr, end);
391
392 lseek (new, (long) data_scnptr, SEEK_SET);
393 ptr = (char *) f_ohdr.data_start;
394 end = ptr + f_ohdr.dsize;
395 write_segment (new, ptr, end);
396
397 return 0;
398 }
399
400 #define UnexBlockSz (1<<12) /* read/write block size */
401 static void
402 write_segment (int new, char *ptr, char *end)
403 {
404 int i, nwrite, ret;
405 char zeros[UnexBlockSz];
406
407 for (i = 0; ptr < end;)
408 {
409 /* distance to next block. */
410 nwrite = (((int) ptr + UnexBlockSz) & -UnexBlockSz) - (int) ptr;
411 /* But not beyond specified end. */
412 if (nwrite > end - ptr) nwrite = end - ptr;
413 ret = write (new, ptr, nwrite);
414 /* If write gets a page fault, it means we reached
415 a gap between the old text segment and the old data segment.
416 This gap has probably been remapped into part of the text segment.
417 So write zeros for it. */
418 if (ret == -1 && errno == EFAULT)
419 {
420 memset (zeros, 0, nwrite);
421 write (new, zeros, nwrite);
422 }
423 else if (nwrite != ret)
424 {
425 int write_errno = errno;
426 char buf[1000];
427 void *addr = ptr;
428 sprintf (buf,
429 "unexec write failure: addr %p, fileno %d, size 0x%x, wrote 0x%x, errno %d",
430 addr, new, nwrite, ret, errno);
431 errno = write_errno;
432 PERROR (buf);
433 }
434 i += nwrite;
435 ptr += nwrite;
436 }
437 }
438 \f
439 /* ****************************************************************
440 * copy_sym
441 *
442 * Copy the relocation information and symbol table from the a.out to the new
443 */
444 static int
445 copy_sym (int new, int a_out, const char *a_name, const char *new_name)
446 {
447 char page[UnexBlockSz];
448 int n;
449
450 if (a_out < 0)
451 return 0;
452
453 if (orig_load_scnptr == 0L)
454 return 0;
455
456 if (lnnoptr && lnnoptr < orig_load_scnptr) /* if there is line number info */
457 lseek (a_out, lnnoptr, SEEK_SET); /* start copying from there */
458 else
459 lseek (a_out, orig_load_scnptr, SEEK_SET); /* Position a.out to symtab. */
460
461 while ((n = read (a_out, page, sizeof page)) > 0)
462 {
463 if (write (new, page, n) != n)
464 {
465 PERROR (new_name);
466 }
467 }
468 if (n < 0)
469 {
470 PERROR (a_name);
471 }
472 return 0;
473 }
474 \f
475 /* ****************************************************************
476 * mark_x
477 *
478 * After successfully building the new a.out, mark it executable
479 */
480 static void
481 mark_x (const char *name)
482 {
483 struct stat sbuf;
484 int um;
485 int new = 0; /* for PERROR */
486
487 um = umask (777);
488 umask (um);
489 if (stat (name, &sbuf) == -1)
490 {
491 PERROR (name);
492 }
493 sbuf.st_mode |= 0111 & ~um;
494 if (chmod (name, sbuf.st_mode) == -1)
495 PERROR (name);
496 }
497 \f
498 static int
499 adjust_lnnoptrs (int writedesc, int readdesc, const char *new_name)
500 {
501 int nsyms;
502 int naux;
503 int new;
504 struct syment symentry;
505 union auxent auxentry;
506
507 if (!lnnoptr || !f_hdr.f_symptr)
508 return 0;
509
510 if ((new = open (new_name, O_RDWR)) < 0)
511 {
512 PERROR (new_name);
513 return -1;
514 }
515
516 lseek (new, f_hdr.f_symptr, SEEK_SET);
517 for (nsyms = 0; nsyms < f_hdr.f_nsyms; nsyms++)
518 {
519 read (new, &symentry, SYMESZ);
520 if (symentry.n_sclass == C_BINCL || symentry.n_sclass == C_EINCL)
521 {
522 symentry.n_value += bias;
523 lseek (new, -SYMESZ, SEEK_CUR);
524 write (new, &symentry, SYMESZ);
525 }
526
527 for (naux = symentry.n_numaux; naux-- != 0; )
528 {
529 read (new, &auxentry, AUXESZ);
530 nsyms++;
531 if (naux != 0 /* skip csect auxentry (last entry) */
532 && (symentry.n_sclass == C_EXT || symentry.n_sclass == C_HIDEXT))
533 {
534 auxentry.x_sym.x_fcnary.x_fcn.x_lnnoptr += bias;
535 lseek (new, -AUXESZ, SEEK_CUR);
536 write (new, &auxentry, AUXESZ);
537 }
538 }
539 }
540 close (new);
541
542 return 0;
543 }
544
545 static int
546 unrelocate_symbols (int new, int a_out,
547 const char *a_name, const char *new_name)
548 {
549 int i;
550 LDHDR ldhdr;
551 LDREL ldrel;
552 ulong t_reloc = (ulong) &_text - f_ohdr.text_start;
553 #ifndef ALIGN_DATA_RELOC
554 ulong d_reloc = (ulong) &_data - f_ohdr.data_start;
555 #else
556 /* This worked (and was needed) before AIX 4.2.
557 I have no idea why. -- Mike */
558 ulong d_reloc = (ulong) &_data - ALIGN (f_ohdr.data_start, 2);
559 #endif
560 int * p;
561
562 if (load_scnptr == 0)
563 return 0;
564
565 lseek (a_out, orig_load_scnptr, SEEK_SET);
566 if (read (a_out, &ldhdr, sizeof (ldhdr)) != sizeof (ldhdr))
567 {
568 PERROR (new_name);
569 }
570
571 #define SYMNDX_TEXT 0
572 #define SYMNDX_DATA 1
573 #define SYMNDX_BSS 2
574
575 for (i = 0; i < ldhdr.l_nreloc; i++)
576 {
577 lseek (a_out,
578 orig_load_scnptr + LDHDRSZ + LDSYMSZ*ldhdr.l_nsyms + LDRELSZ*i,
579 SEEK_SET);
580
581 if (read (a_out, &ldrel, LDRELSZ) != LDRELSZ)
582 {
583 PERROR (a_name);
584 }
585
586 /* move the BSS loader symbols to the DATA segment */
587 if (ldrel.l_symndx == SYMNDX_BSS)
588 {
589 ldrel.l_symndx = SYMNDX_DATA;
590
591 lseek (new,
592 load_scnptr + LDHDRSZ + LDSYMSZ*ldhdr.l_nsyms + LDRELSZ*i,
593 SEEK_SET);
594
595 if (write (new, &ldrel, LDRELSZ) != LDRELSZ)
596 {
597 PERROR (new_name);
598 }
599 }
600
601 if (ldrel.l_rsecnm == f_ohdr.o_sndata)
602 {
603 int orig_int;
604
605 lseek (a_out,
606 orig_data_scnptr + (ldrel.l_vaddr - f_ohdr.data_start),
607 SEEK_SET);
608
609 if (read (a_out, (void *) &orig_int, sizeof (orig_int))
610 != sizeof (orig_int))
611 {
612 PERROR (a_name);
613 }
614
615 p = (int *) (ldrel.l_vaddr + d_reloc);
616
617 switch (ldrel.l_symndx) {
618 case SYMNDX_TEXT:
619 orig_int = * p - t_reloc;
620 break;
621
622 case SYMNDX_DATA:
623 case SYMNDX_BSS:
624 orig_int = * p - d_reloc;
625 break;
626 }
627
628 if (orig_int != * p)
629 {
630 lseek (new,
631 data_scnptr + (ldrel.l_vaddr - f_ohdr.data_start),
632 SEEK_SET);
633 if (write (new, (void *) &orig_int, sizeof (orig_int))
634 != sizeof (orig_int))
635 {
636 PERROR (new_name);
637 }
638 }
639 }
640 }
641 return 0;
642 }
643
644 /*
645 * Return the address of the start of the text segment prior to
646 * doing an unexec. After unexec the return value is undefined.
647 * See crt0.c for further explanation and _start.
648 *
649 */
650
651 char *
652 start_of_text (void)
653 {
654 return ((char *) 0x10000000);
655 }