annotate src/unexapollo.c @ 52311:c7af0671f3ba

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