613
|
1 /* unexapollo.c -- COFF File UNEXEC for XEmacs on Apollo SR10.x
|
428
|
2 Copyright (C) 1988, 1994 Free Software Foundation, Inc.
|
|
3
|
613
|
4 This file is part of XEmacs.
|
428
|
5
|
613
|
6 XEmacs is free software; you can redistribute it and/or modify
|
428
|
7 it under the terms of the GNU General Public License as published by
|
|
8 the Free Software Foundation; either version 2, or (at your option)
|
|
9 any later version.
|
|
10
|
613
|
11 XEmacs is distributed in the hope that it will be useful,
|
428
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 GNU General Public License for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
613
|
17 along with XEmacs; see the file COPYING. If not, write to
|
428
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA. */
|
|
20
|
|
21 /* Synched up with: FSF 19.31. */
|
|
22
|
|
23 /* Written by Leonard N. Zubkoff. */
|
|
24
|
|
25 #include <config.h>
|
|
26 #include <fcntl.h>
|
|
27
|
|
28
|
|
29 #include <a.out.h>
|
|
30 #include <sys/file.h>
|
|
31 #include <apollo/base.h>
|
|
32 #include <apollo/ios.h>
|
|
33 #include <apollo/type_uids.h>
|
|
34 #include <apollo/dst.h>
|
|
35
|
|
36
|
|
37 #define DST_RECORD_HDR_SIZE 2
|
|
38 #define LONG_ALIGN(X) (((X)+3)&(~3))
|
|
39
|
|
40
|
|
41 void
|
|
42 unexec (target_file_name, source_file_name)
|
|
43 char *target_file_name, *source_file_name;
|
|
44 {
|
|
45 struct filehdr file_header;
|
|
46 struct aouthdr domain_header;
|
|
47 struct scnhdr *section, *sections, *sections_limit;
|
|
48 struct scnhdr *first_data_section, *last_data_section;
|
|
49 struct scnhdr *rwdi_section, *blocks_section;
|
|
50 struct reloc reloc_entry;
|
|
51 unsigned long data_size, old_data_section_size, source_file_offset_past_rwdi;
|
|
52 unsigned char buffer[4096];
|
|
53 long delta_before_rwdi, delta_after_rwdi, byte_count;
|
|
54 long first_changed_vaddr, old_rwdi_vaddr, i;
|
|
55 ios_$id_t target_file, source_file;
|
|
56 status_$t status;
|
|
57 /* Open the Source File. */
|
|
58 if ((source_file = open (source_file_name, O_RDONLY)) < 0)
|
563
|
59 signal_error (Qfile_error, "cannot open source file for input", Qunbound);
|
428
|
60 /* Read the File Header. */
|
|
61 if (read (source_file, &file_header, sizeof (file_header)) != sizeof (file_header))
|
563
|
62 signal_error (Qfile_error, "cannot read file header", Qunbound);
|
428
|
63
|
|
64 /* Read the Domain Header. */
|
|
65 if (read (source_file, &domain_header, sizeof (domain_header))
|
|
66 != sizeof (domain_header))
|
563
|
67 signal_error (Qfile_error, "cannot read domain header", Qunbound);
|
428
|
68 /* Read the Section Headers. */
|
|
69 sections =
|
|
70 (struct scnhdr *) malloc (file_header.f_nscns*sizeof (struct scnhdr));
|
|
71 if (sections == (struct scnhdr *) 0)
|
563
|
72 out_of_memory ("cannot allocate section header storage", Qunbound);
|
428
|
73 sections_limit = sections + file_header.f_nscns;
|
|
74 if (read (source_file, sections, file_header.f_nscns*sizeof (struct scnhdr))
|
|
75 != file_header.f_nscns*sizeof (struct scnhdr))
|
563
|
76 signal_error (Qfile_error, "cannot read section headers", Qunbound);
|
428
|
77 /* Compute the new Size of the Data Section. */
|
|
78 data_size = sbrk (0) - domain_header.data_start;
|
|
79 delta_before_rwdi = delta_after_rwdi = data_size - domain_header.dsize;
|
|
80 old_rwdi_vaddr = 0;
|
|
81 /* Find and Deallocate the .rwdi Section Information. */
|
|
82 for (rwdi_section = sections; rwdi_section != sections_limit; rwdi_section++)
|
|
83 if (strcmp (rwdi_section->s_name, ".rwdi") == 0)
|
|
84 {
|
|
85 /* If there are relocation entries, we cannot "unrelocate" them. */
|
|
86 if (rwdi_section->s_nreloc > 0)
|
563
|
87 signal_error (Qio_error, ".rwdi section needs relocation - cannot dump Emacs", Qunbound);
|
428
|
88 delta_after_rwdi = delta_before_rwdi - rwdi_section->s_size;
|
|
89 old_rwdi_vaddr = rwdi_section->s_vaddr;
|
|
90 rwdi_section->s_paddr = 0;
|
|
91 rwdi_section->s_vaddr = 0;
|
|
92 rwdi_section->s_scnptr = 0;
|
|
93 rwdi_section->s_size = 0;
|
|
94 source_file_offset_past_rwdi = (rwdi_section+1)->s_scnptr;
|
|
95 break;
|
|
96 }
|
|
97 /* Skip over the Text Section Headers. */
|
|
98 for (section = sections; (section->s_flags & STYP_TEXT) != 0; section++) ;
|
|
99 /*
|
|
100 Find the First and Last Data Sections and Fixup
|
|
101 Section Header Relocation Pointers.
|
|
102 */
|
|
103 first_data_section = last_data_section = (struct scnhdr *) 0;
|
|
104 for (; section != sections_limit; section++)
|
|
105 {
|
|
106 if ((section->s_flags & STYP_DATA) != 0)
|
|
107 {
|
|
108 if (first_data_section == (struct scnhdr *) 0)
|
|
109 first_data_section = section;
|
|
110 last_data_section = section;
|
|
111 }
|
|
112 if (section->s_relptr != 0)
|
|
113 section->s_relptr += delta_after_rwdi;
|
|
114 }
|
|
115 /* Increment the Size of the Last Data Section. */
|
|
116 old_data_section_size = last_data_section->s_size;
|
|
117 last_data_section->s_size += delta_before_rwdi;
|
|
118
|
|
119 /* Update the File Header and Domain Header. */
|
|
120 file_header.f_symptr += delta_after_rwdi;
|
|
121 domain_header.dsize = data_size;
|
|
122 domain_header.bsize = 0;
|
|
123 /* Skip over subsequent Bss Section Headers. */
|
|
124 for (section = last_data_section+1;
|
|
125 (section->s_flags & STYP_BSS) != 0; section++) ;
|
|
126 /* Update the remaining Section Headers. */
|
|
127 blocks_section = (struct scnhdr *) 0;
|
|
128 first_changed_vaddr = 0;
|
|
129 for (; section != sections_limit; section++)
|
|
130 {
|
|
131 long delta = (section < rwdi_section ? delta_before_rwdi : delta_after_rwdi);
|
|
132 if (section->s_paddr != 0)
|
|
133 section->s_paddr += delta;
|
|
134 if (section->s_vaddr != 0)
|
|
135 {
|
|
136 if (first_changed_vaddr == 0)
|
|
137 first_changed_vaddr = section->s_vaddr;
|
|
138 section->s_vaddr += delta;
|
|
139 }
|
|
140 if (section->s_scnptr != 0)
|
|
141 section->s_scnptr += delta;
|
|
142 if (strcmp (section->s_name, ".blocks") == 0)
|
|
143 blocks_section = section;
|
|
144 else if (strcmp (section->s_name, ".sri") == 0 &&
|
|
145 domain_header.o_sri != 0)
|
|
146 domain_header.o_sri += delta;
|
|
147 else if (strcmp (section->s_name, ".inlib") == 0 &&
|
|
148 domain_header.o_inlib != 0)
|
|
149 domain_header.o_inlib += delta;
|
|
150 }
|
|
151 /* Open the Target File. */
|
|
152 ios_$create (target_file_name, strlen (target_file_name), coff_$uid,
|
|
153 ios_$recreate_mode, ios_$write_opt, &target_file, &status);
|
|
154 if (status.all != status_$ok)
|
563
|
155 signal_error (Qfile_error, "cannot open target file for output", Qunbound);
|
428
|
156 /* Write the File Header. */
|
|
157 if (write (target_file, &file_header, sizeof (file_header)) != sizeof (file_header))
|
563
|
158 signal_error (Qfile_error, "cannot write file header", Qunbound);
|
428
|
159 /* Write the Domain Header. */
|
|
160 if (write (target_file, &domain_header, sizeof (domain_header))
|
|
161 != sizeof (domain_header))
|
563
|
162 signal_error (Qfile_error, "cannot write domain header", Qunbound);
|
428
|
163 /* Write the Section Headers. */
|
|
164 if (write (target_file, sections, file_header.f_nscns*sizeof (struct scnhdr))
|
|
165 != file_header.f_nscns*sizeof (struct scnhdr))
|
563
|
166 signal_error (Qfile_error, "cannot write section headers", Qunbound);
|
428
|
167 /* Copy the Allocated Sections. */
|
|
168 for (section = sections; section != first_data_section; section++)
|
|
169 if (section->s_scnptr != 0)
|
|
170 CopyData (target_file, source_file, LONG_ALIGN(section->s_size));
|
|
171 /* Write the Expanded Data Segment. */
|
|
172 if (write (target_file, first_data_section->s_vaddr, data_size) != data_size)
|
563
|
173 signal_error (Qfile_error, "cannot write new data section", Qunbound);
|
428
|
174
|
|
175 /* Skip over the Last Data Section and Copy until the .rwdi Section. */
|
|
176 if (lseek (source_file, last_data_section->s_scnptr
|
|
177 +old_data_section_size, L_SET) == -1)
|
563
|
178 signal_error (Qfile_error, "cannot seek past data section", Qunbound);
|
428
|
179 for (section = last_data_section+1; section != rwdi_section; section++)
|
|
180 if (section->s_scnptr != 0)
|
|
181 CopyData (target_file, source_file, LONG_ALIGN(section->s_size));
|
|
182 /* Skip over the .rwdi Section and Copy Remainder of Source File. */
|
|
183 if (lseek (source_file, source_file_offset_past_rwdi, L_SET) == -1)
|
563
|
184 signal_error (Qfile_error, "cannot seek past .rwdi section", Qunbound);
|
428
|
185 while ((byte_count = read (source_file, buffer, sizeof (buffer))) > 0)
|
|
186 if (write (target_file, buffer, byte_count) != byte_count)
|
563
|
187 signal_error (Qfile_error, "cannot write data", Qunbound);
|
428
|
188 /* Unrelocate .data references to Global Symbols. */
|
|
189 for (section = first_data_section; section <= last_data_section; section++)
|
|
190 for (i = 0; i < section->s_nreloc; i++)
|
|
191 {
|
|
192 if (lseek (source_file, section->s_relptr
|
|
193 +i*sizeof (struct reloc)-delta_after_rwdi, L_SET) == -1)
|
563
|
194 signal_error (Qfile_error, "cannot seek to relocation info", Qunbound);
|
428
|
195 if (read (source_file, &reloc_entry, sizeof (reloc_entry))
|
|
196 != sizeof (reloc_entry))
|
563
|
197 signal_error (Qfile_error, "cannot read reloc entry", Qunbound);
|
428
|
198 if (lseek (source_file, reloc_entry.r_vaddr-section->s_vaddr
|
|
199 +section->s_scnptr, L_SET) == -1)
|
563
|
200 signal_error (Qfile_error, "cannot seek to data element", Qunbound);
|
428
|
201 if (lseek (target_file, reloc_entry.r_vaddr-section->s_vaddr
|
|
202 +section->s_scnptr, L_SET) == -1)
|
563
|
203 signal_error (Qfile_error, "cannot seek to data element", Qunbound);
|
428
|
204 if (read (source_file, buffer, 4) != 4)
|
563
|
205 signal_error (Qfile_error, "cannot read data element", Qunbound);
|
428
|
206 if (write (target_file, buffer, 4) != 4)
|
563
|
207 signal_error (Qfile_error, "cannot write data element", Qunbound);
|
428
|
208 }
|
|
209
|
|
210 /* Correct virtual addresses in .blocks section. */
|
|
211 if (blocks_section != (struct scnhdr *) 0)
|
|
212 {
|
|
213 dst_rec_t dst_record;
|
|
214 dst_rec_comp_unit_t *comp_unit;
|
|
215 unsigned short number_of_sections;
|
|
216 unsigned long section_base;
|
|
217 unsigned long section_offset = 0;
|
|
218 /* Find section tables and update section base addresses. */
|
|
219 while (section_offset < blocks_section->s_size)
|
|
220 {
|
|
221 if (lseek (target_file,
|
|
222 blocks_section->s_scnptr+section_offset, L_SET) == -1)
|
563
|
223 signal_error (Qfile_error, "cannot seek to comp unit record", Qunbound);
|
428
|
224 /* Handle pad records before the comp unit record. */
|
|
225 if (read (target_file, &dst_record, DST_RECORD_HDR_SIZE)
|
|
226 != DST_RECORD_HDR_SIZE)
|
563
|
227 signal_error (Qfile_error, "cannot read dst record tag", Qunbound);
|
428
|
228 if (dst_record.rec_type == dst_typ_pad)
|
|
229 section_offset += DST_RECORD_HDR_SIZE;
|
|
230 else if (dst_record.rec_type == dst_typ_comp_unit)
|
|
231 {
|
|
232 comp_unit = &dst_record.rec_data.comp_unit_;
|
|
233 if (read (target_file, comp_unit, sizeof (*comp_unit))
|
|
234 != sizeof (*comp_unit))
|
563
|
235 signal_error (Qfile_error, "cannot read comp unit record", Qunbound);
|
428
|
236 if (lseek (target_file, blocks_section->s_scnptr
|
|
237 +section_offset
|
|
238 #if dst_version_major == 1 && dst_version_minor < 4
|
|
239 +comp_unit->section_table
|
|
240 #else
|
|
241 +comp_unit->section_table.rel_offset
|
|
242 #endif
|
|
243 +DST_RECORD_HDR_SIZE,
|
|
244 L_SET) == -1)
|
563
|
245 signal_error (Qfile_error, "cannot seek to section table", Qunbound);
|
428
|
246 if (read (target_file, &number_of_sections, sizeof (number_of_sections))
|
|
247 != sizeof (number_of_sections))
|
563
|
248 signal_error (Qfile_error, "cannot read section table size", Qunbound);
|
428
|
249 for (i = 0; i < number_of_sections; i++)
|
|
250 {
|
|
251 if (read (target_file, §ion_base, sizeof (section_base))
|
|
252 != sizeof (section_base))
|
563
|
253 signal_error (Qfile_error, "cannot read section base value", Qunbound);
|
428
|
254 if (section_base < first_changed_vaddr)
|
|
255 continue;
|
|
256 else if (section_base < old_rwdi_vaddr)
|
|
257 section_base += delta_before_rwdi;
|
|
258 else section_base += delta_after_rwdi;
|
|
259 if (lseek (target_file, -sizeof (section_base), L_INCR) == -1)
|
563
|
260 signal_error (Qfile_error, "cannot seek to section base value", Qunbound);
|
428
|
261 if (write (target_file, §ion_base, sizeof (section_base))
|
|
262 != sizeof (section_base))
|
563
|
263 signal_error (Qfile_error, "cannot write section base", Qunbound);
|
428
|
264 }
|
|
265 section_offset += comp_unit->data_size;
|
|
266 }
|
563
|
267 else signal_error (Qfile_error, "unexpected dst record type", Qunbound);
|
428
|
268 }
|
|
269 }
|
|
270
|
|
271 if (close (source_file) == -1)
|
563
|
272 signal_error (Qfile_error, "cannot close source file", Qunbound);
|
428
|
273 if (close (target_file) == -1)
|
563
|
274 signal_error (Qfile_error, "cannot close target file", Qunbound);
|
428
|
275 }
|
|
276
|
|
277
|
|
278 static
|
|
279 CopyData (target_file, source_file, total_byte_count)
|
|
280 int target_file, source_file;
|
|
281 long total_byte_count;
|
|
282 {
|
|
283 unsigned char buffer[4096];
|
|
284 long byte_count;
|
|
285 while (total_byte_count > 0)
|
|
286 {
|
|
287 if (total_byte_count > sizeof (buffer))
|
|
288 byte_count = sizeof (buffer);
|
|
289 else byte_count = total_byte_count;
|
|
290 if (read (source_file, buffer, byte_count) != byte_count)
|
563
|
291 signal_error (Qfile_error, "cannot read data", Qunbound);
|
428
|
292 if (write (target_file, buffer, byte_count) != byte_count)
|
563
|
293 signal_error (Qfile_error, "cannot write data", Qunbound);
|
428
|
294 total_byte_count -= byte_count;
|
|
295 }
|
|
296 }
|