update.
[chise/xemacs-chise.git.1] / src / unexapollo.c
1 /* unexapollo.c -- COFF File UNEXEC for GNU Emacs on Apollo SR10.x
2    Copyright (C) 1988, 1994 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
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.
10
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.
15
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.  */
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)
59     error ("cannot open source file for input");
60   /* Read the File Header. */
61   if (read (source_file, &file_header, sizeof (file_header)) != sizeof (file_header))
62     error ("cannot read file header");
63   \f
64   /* Read the Domain Header. */
65   if (read (source_file, &domain_header, sizeof (domain_header))
66       != sizeof (domain_header))
67     error ("cannot read domain header");
68   /* Read the Section Headers. */
69   sections =
70     (struct scnhdr *) malloc (file_header.f_nscns*sizeof (struct scnhdr));
71   if (sections == (struct scnhdr *) 0)
72     error ("cannot allocate section header storage");
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))
76     error ("cannot read section headers");
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)
87           error (".rwdi section needs relocation - cannot dump Emacs");
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   \f
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)
155     error ("cannot open target file for output");
156   /* Write the File Header. */
157   if (write (target_file, &file_header, sizeof (file_header)) != sizeof (file_header))
158     error ("cannot write file header");
159   /* Write the Domain Header. */
160   if (write (target_file, &domain_header, sizeof (domain_header))
161       != sizeof (domain_header))
162     error ("cannot write domain header");
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))
166     error ("cannot write section headers");
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)
173     error ("cannot write new data section");
174   \f
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)
178     error ("cannot seek past data section");
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)
184     error ("cannot seek past .rwdi section");
185   while ((byte_count = read (source_file, buffer, sizeof (buffer))) > 0)
186     if (write (target_file, buffer, byte_count) != byte_count)
187       error ("cannot write data");
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)
194           error ("cannot seek to relocation info");
195         if (read (source_file, &reloc_entry, sizeof (reloc_entry))
196             != sizeof (reloc_entry))
197           error ("cannot read reloc entry");
198         if (lseek (source_file, reloc_entry.r_vaddr-section->s_vaddr
199                    +section->s_scnptr, L_SET) == -1)
200           error ("cannot seek to data element");
201         if (lseek (target_file, reloc_entry.r_vaddr-section->s_vaddr
202                    +section->s_scnptr, L_SET) == -1)
203           error ("cannot seek to data element");
204         if (read (source_file, buffer, 4) != 4)
205           error ("cannot read data element");
206         if (write (target_file, buffer, 4) != 4)
207           error ("cannot write data element");
208       }
209   \f
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)
223             error ("cannot seek to comp unit record");
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)
227             error ("cannot read dst record tag");
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))
235                 error ("cannot read comp unit record");
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)
245                 error ("cannot seek to section table");
246               if (read (target_file, &number_of_sections, sizeof (number_of_sections))
247                   != sizeof (number_of_sections))
248                 error ("cannot read section table size");
249               for (i = 0; i < number_of_sections; i++)
250                 {
251                   if (read (target_file, &section_base, sizeof (section_base))
252                       != sizeof (section_base))
253                     error ("cannot read section base value");
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)
260                     error ("cannot seek to section base value");
261                   if (write (target_file, &section_base, sizeof (section_base))
262                       != sizeof (section_base))
263                     error ("cannot write section base");
264                 }
265               section_offset += comp_unit->data_size;
266             }
267           else error ("unexpected dst record type");
268         }
269     }
270   \f
271   if (close (source_file) == -1)
272     error("cannot close source file");
273   if (close (target_file) == -1)
274     error ("cannot close target file");
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)
291         error ("cannot read data");
292       if (write (target_file, buffer, byte_count) != byte_count)
293         error ("cannot write data");
294       total_byte_count -= byte_count;
295     }
296 }