1 /* Waiting for papers! */
4 * Do an unexec() for coff encapsulation. Uses the approach I took
5 * for AKCL, so don't be surprised if it doesn't look too much like
6 * the other unexec() routines. Assumes NO_REMAP. Should be easy to
7 * adapt to the emacs style unexec() if that is desired, but this works
8 * just fine for me with GCC/GAS/GLD under System V. - Jordan
11 #include <sys/types.h>
12 #include <sys/fcntl.h>
15 #include "/usr/gnu/lib/gcc/gcc-include/a.out.h"
25 fread(buffer
, BUFSIZ
, 1, from
);
26 fwrite(buffer
, BUFSIZ
, 1, to
);
29 fread(buffer
, 1, n
, from
);
30 fwrite(buffer
, 1, n
, to
);
35 /* ****************************************************************
39 * ****************************************************************/
40 unexec (new_name
, a_name
, data_start
, bss_start
, entry_address
)
41 char *new_name
, *a_name
;
42 unsigned data_start
, bss_start
, entry_address
;
44 struct coffheader header1
;
45 struct coffscn
*tp
, *dp
, *bp
;
48 char *original_file
= a_name
;
49 char *save_file
= new_name
;
51 char *data_begin
, *data_end
;
53 FILE *original
, *save
;
57 char stdin_buf
[BUFSIZ
], stdout_buf
[BUFSIZ
];
61 original
= fopen(original_file
, "r");
62 if (stdin
!= original
|| original
->_file
!= 0) {
63 fprintf(stderr
, "unexec: Can't open the original file.\n");
66 setbuf(original
, stdin_buf
);
69 n
= open(save_file
, O_CREAT
|O_WRONLY
, 0777);
70 if (n
!= 1 || (save
= fdopen(n
, "w")) != stdout
) {
71 fprintf(stderr
, "unexec: Can't open the save file.\n");
74 setbuf(save
, stdout_buf
);
76 fread(&header1
, sizeof(header1
), 1, original
);
77 tp
= &header1
.scns
[0];
78 dp
= &header1
.scns
[1];
79 bp
= &header1
.scns
[2];
80 fread(&header
, sizeof(header
), 1, original
);
81 data_begin
=(char *)N_DATADDR(header
);
83 original_data
= header
.a_data
;
84 header
.a_data
= data_end
- data_begin
;
86 dp
->s_size
= header
.a_data
;
87 bp
->s_paddr
= dp
->s_vaddr
+ dp
->s_size
;
88 bp
->s_vaddr
= bp
->s_paddr
;
90 header1
.tsize
= tp
->s_size
;
91 header1
.dsize
= dp
->s_size
;
92 header1
.bsize
= bp
->s_size
;
93 fwrite(&header1
, sizeof(header1
), 1, save
);
94 fwrite(&header
, sizeof(header
), 1, save
);
96 filecpy(save
, original
, header
.a_text
);
98 for (n
= header
.a_data
, p
= data_begin
; ; n
-= BUFSIZ
, p
+= BUFSIZ
)
100 fwrite(p
, BUFSIZ
, 1, save
);
102 fwrite(p
, 1, n
, save
);
107 fseek(original
, original_data
, 1);
109 filecpy(save
, original
, header
.a_syms
+header
.a_trsize
+header
.a_drsize
);
110 fread(&stsize
, sizeof(stsize
), 1, original
);
111 fwrite(&stsize
, sizeof(stsize
), 1, save
);
112 filecpy(save
, original
, stsize
- sizeof(stsize
));