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