annotate src/unexapollo.c @ 2586:196ee3cd1ac5

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