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