1 /* Waiting for papers! */
3 /* Synched up with: FSF 19.31. */
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
13 #include <sys/types.h>
14 #include <sys/fcntl.h>
17 #include "/usr/gnu/lib/gcc/gcc-include/a.out.h"
27 fread(buffer, BUFSIZ, 1, from);
28 fwrite(buffer, BUFSIZ, 1, to);
31 fread(buffer, 1, n, from);
32 fwrite(buffer, 1, n, to);
37 /* ****************************************************************
41 * ****************************************************************/
42 unexec (new_name, a_name, data_start, bss_start, entry_address)
43 char *new_name, *a_name;
44 unsigned data_start, bss_start, entry_address;
46 struct coffheader header1;
47 struct coffscn *tp, *dp, *bp;
50 char *original_file = a_name;
51 char *save_file = new_name;
53 char *data_begin, *data_end;
55 FILE *original, *save;
59 char stdin_buf[BUFSIZ], stdout_buf[BUFSIZ];
63 original = fopen(original_file, "r");
64 if (stdin != original || original->_file != 0) {
65 fprintf(stderr, "unexec: Can't open the original file.\n");
68 setbuf(original, stdin_buf);
71 n = open (save_file, O_CREAT|O_WRONLY, 0777);
72 if (n != 1 || (save = fdopen(n, "w")) != stdout) {
73 fprintf(stderr, "unexec: Can't open the save file.\n");
76 setbuf(save, stdout_buf);
78 fread(&header1, sizeof(header1), 1, original);
79 tp = &header1.scns[0];
80 dp = &header1.scns[1];
81 bp = &header1.scns[2];
82 fread(&header, sizeof(header), 1, original);
83 data_begin=(char *)N_DATADDR(header);
85 original_data = header.a_data;
86 header.a_data = data_end - data_begin;
88 dp->s_size = header.a_data;
89 bp->s_paddr = dp->s_vaddr + dp->s_size;
90 bp->s_vaddr = bp->s_paddr;
92 header1.tsize = tp->s_size;
93 header1.dsize = dp->s_size;
94 header1.bsize = bp->s_size;
95 fwrite(&header1, sizeof(header1), 1, save);
96 fwrite(&header, sizeof(header), 1, save);
98 filecpy(save, original, header.a_text);
100 for (n = header.a_data, p = data_begin; ; n -= BUFSIZ, p += BUFSIZ)
102 fwrite(p, BUFSIZ, 1, save);
104 fwrite(p, 1, n, save);
109 fseek(original, original_data, 1);
111 filecpy(save, original, header.a_syms+header.a_trsize+header.a_drsize);
112 fread(&stsize, sizeof(stsize), 1, original);
113 fwrite(&stsize, sizeof(stsize), 1, save);
114 filecpy(save, original, stsize - sizeof(stsize));