0
|
1 /* Waiting for papers! */
|
|
2
|
|
3 /* Synched up with: FSF 19.31. */
|
|
4
|
|
5 /*
|
|
6 * Do an unexec() for coff encapsulation. Uses the approach I took
|
|
7 * for AKCL, so don't be surprised if it doesn't look too much like
|
|
8 * the other unexec() routines. Assumes NO_REMAP. Should be easy to
|
|
9 * adapt to the emacs style unexec() if that is desired, but this works
|
|
10 * just fine for me with GCC/GAS/GLD under System V. - Jordan
|
|
11 */
|
|
12
|
|
13 #include <sys/types.h>
|
|
14 #include <sys/fcntl.h>
|
|
15 #include <sys/file.h>
|
|
16 #include <stdio.h>
|
|
17 #include "/usr/gnu/lib/gcc/gcc-include/a.out.h"
|
2286
|
18 #include "compiler.h"
|
0
|
19
|
|
20 filecpy(to, from, n)
|
|
21 FILE *to, *from;
|
|
22 int n;
|
|
23 {
|
|
24 char buffer[BUFSIZ];
|
|
25
|
|
26 for (;;)
|
|
27 if (n > BUFSIZ) {
|
|
28 fread(buffer, BUFSIZ, 1, from);
|
|
29 fwrite(buffer, BUFSIZ, 1, to);
|
|
30 n -= BUFSIZ;
|
|
31 } else if (n > 0) {
|
|
32 fread(buffer, 1, n, from);
|
|
33 fwrite(buffer, 1, n, to);
|
|
34 break;
|
|
35 } else
|
|
36 break;
|
|
37 }
|
|
38 /* ****************************************************************
|
|
39 * unexec
|
|
40 *
|
|
41 * driving logic.
|
|
42 * ****************************************************************/
|
|
43 unexec (new_name, a_name, data_start, bss_start, entry_address)
|
|
44 char *new_name, *a_name;
|
2286
|
45 unsigned UNUSED (data_start);
|
|
46 unsigned UNUSED (bss_start);
|
|
47 unsigned UNUSED (entry_address);
|
0
|
48 {
|
|
49 struct coffheader header1;
|
|
50 struct coffscn *tp, *dp, *bp;
|
|
51 struct exec header;
|
|
52 int stsize;
|
|
53 char *original_file = a_name;
|
|
54 char *save_file = new_name;
|
|
55
|
|
56 char *data_begin, *data_end;
|
|
57 int original_data;
|
|
58 FILE *original, *save;
|
|
59 int n;
|
|
60 char *p;
|
|
61 extern char *sbrk();
|
|
62 char stdin_buf[BUFSIZ], stdout_buf[BUFSIZ];
|
|
63
|
|
64
|
|
65 fclose(stdin);
|
|
66 original = fopen(original_file, "r");
|
|
67 if (stdin != original || original->_file != 0) {
|
|
68 fprintf(stderr, "unexec: Can't open the original file.\n");
|
|
69 exit(1);
|
|
70 }
|
|
71 setbuf(original, stdin_buf);
|
|
72 fclose(stdout);
|
|
73 unlink(save_file);
|
|
74 n = open (save_file, O_CREAT|O_WRONLY, 0777);
|
|
75 if (n != 1 || (save = fdopen(n, "w")) != stdout) {
|
|
76 fprintf(stderr, "unexec: Can't open the save file.\n");
|
|
77 exit(1);
|
|
78 }
|
|
79 setbuf(save, stdout_buf);
|
|
80
|
|
81 fread(&header1, sizeof(header1), 1, original);
|
|
82 tp = &header1.scns[0];
|
|
83 dp = &header1.scns[1];
|
|
84 bp = &header1.scns[2];
|
|
85 fread(&header, sizeof(header), 1, original);
|
|
86 data_begin=(char *)N_DATADDR(header);
|
|
87 data_end = sbrk(0);
|
|
88 original_data = header.a_data;
|
|
89 header.a_data = data_end - data_begin;
|
|
90 header.a_bss = 0;
|
|
91 dp->s_size = header.a_data;
|
|
92 bp->s_paddr = dp->s_vaddr + dp->s_size;
|
|
93 bp->s_vaddr = bp->s_paddr;
|
|
94 bp->s_size = 0;
|
|
95 header1.tsize = tp->s_size;
|
|
96 header1.dsize = dp->s_size;
|
|
97 header1.bsize = bp->s_size;
|
|
98 fwrite(&header1, sizeof(header1), 1, save);
|
|
99 fwrite(&header, sizeof(header), 1, save);
|
|
100
|
|
101 filecpy(save, original, header.a_text);
|
|
102
|
|
103 for (n = header.a_data, p = data_begin; ; n -= BUFSIZ, p += BUFSIZ)
|
|
104 if (n > BUFSIZ)
|
|
105 fwrite(p, BUFSIZ, 1, save);
|
|
106 else if (n > 0) {
|
|
107 fwrite(p, 1, n, save);
|
|
108 break;
|
|
109 } else
|
|
110 break;
|
|
111
|
|
112 fseek(original, original_data, 1);
|
|
113
|
|
114 filecpy(save, original, header.a_syms+header.a_trsize+header.a_drsize);
|
|
115 fread(&stsize, sizeof(stsize), 1, original);
|
|
116 fwrite(&stsize, sizeof(stsize), 1, save);
|
|
117 filecpy(save, original, stsize - sizeof(stsize));
|
|
118
|
|
119 fclose(original);
|
|
120 fclose(save);
|
|
121 }
|