annotate src/unexapollo.c @ 22860:349fa4ee1f27

(assoc-default): New function.
author Richard M. Stallman <rms@gnu.org>
date Fri, 31 Jul 1998 10:53:30 +0000
parents 763c253911c3
children 64341c22fb4c
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
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
23 #include "config.h"
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
24 #include <fcntl.h>
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
25
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
26
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
27 #include <a.out.h>
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
28 #include <sys/file.h>
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
29 #include <apollo/base.h>
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
30 #include <apollo/ios.h>
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
31 #include <apollo/type_uids.h>
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
32 #include <apollo/dst.h>
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
33
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
34
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
35 #define DST_RECORD_HDR_SIZE 2
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
36 #define LONG_ALIGN(X) (((X)+3)&(~3))
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
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
39 void
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
40 unexec (target_file_name, source_file_name)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
41 char *target_file_name, *source_file_name;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
42 {
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
43 struct filehdr file_header;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
44 struct aouthdr domain_header;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
45 struct scnhdr *section, *sections, *sections_limit;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
46 struct scnhdr *first_data_section, *last_data_section;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
47 struct scnhdr *rwdi_section, *blocks_section;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
48 struct reloc reloc_entry;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
49 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
50 unsigned char buffer[4096];
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
51 long delta_before_rwdi, delta_after_rwdi, byte_count;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
52 long first_changed_vaddr, old_rwdi_vaddr, i;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
53 ios_$id_t target_file, source_file;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
54 status_$t status;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
55 /* Open the Source File. */
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
56 if ((source_file = open (source_file_name, O_RDONLY)) < 0)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
57 error ("cannot open source file for input");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
58 /* Read the File Header. */
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
59 if (read (source_file, &file_header, sizeof (file_header)) != sizeof (file_header))
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
60 error ("cannot read file header");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
61
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
62 /* Read the Domain Header. */
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
63 if (read (source_file, &domain_header, sizeof (domain_header))
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
64 != sizeof (domain_header))
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
65 error ("cannot read domain header");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
66 /* Read the Section Headers. */
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
67 sections =
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
68 (struct scnhdr *) malloc (file_header.f_nscns*sizeof (struct scnhdr));
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
69 if (sections == (struct scnhdr *) 0)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
70 error ("cannot allocate section header storage");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
71 sections_limit = sections + file_header.f_nscns;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
72 if (read (source_file, sections, file_header.f_nscns*sizeof (struct scnhdr))
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
73 != file_header.f_nscns*sizeof (struct scnhdr))
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
74 error ("cannot read section headers");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
75 /* Compute the new Size of the Data Section. */
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
76 data_size = sbrk (0) - domain_header.data_start;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
77 delta_before_rwdi = delta_after_rwdi = data_size - domain_header.dsize;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
78 old_rwdi_vaddr = 0;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
79 /* Find and Deallocate the .rwdi Section Information. */
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
80 for (rwdi_section = sections; rwdi_section != sections_limit; rwdi_section++)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
81 if (strcmp (rwdi_section->s_name, ".rwdi") == 0)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
82 {
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
83 /* If there are relocation entries, we cannot "unrelocate" them. */
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
84 if (rwdi_section->s_nreloc > 0)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
85 error (".rwdi section needs relocation - cannot dump Emacs");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
86 delta_after_rwdi = delta_before_rwdi - rwdi_section->s_size;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
87 old_rwdi_vaddr = rwdi_section->s_vaddr;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
88 rwdi_section->s_paddr = 0;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
89 rwdi_section->s_vaddr = 0;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
90 rwdi_section->s_scnptr = 0;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
91 rwdi_section->s_size = 0;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
92 source_file_offset_past_rwdi = (rwdi_section+1)->s_scnptr;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
93 break;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
94 }
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
95 /* Skip over the Text Section Headers. */
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
96 for (section = sections; (section->s_flags & STYP_TEXT) != 0; section++) ;
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 Find the First and Last Data Sections and Fixup
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
99 Section Header Relocation Pointers.
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 first_data_section = last_data_section = (struct scnhdr *) 0;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
102 for (; section != sections_limit; section++)
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 if ((section->s_flags & STYP_DATA) != 0)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
105 {
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
106 if (first_data_section == (struct scnhdr *) 0)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
107 first_data_section = section;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
108 last_data_section = section;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
109 }
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
110 if (section->s_relptr != 0)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
111 section->s_relptr += delta_after_rwdi;
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 /* Increment the Size of the Last Data Section. */
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
114 old_data_section_size = last_data_section->s_size;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
115 last_data_section->s_size += delta_before_rwdi;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
116
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
117 /* Update the File Header and Domain Header. */
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
118 file_header.f_symptr += delta_after_rwdi;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
119 domain_header.dsize = data_size;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
120 domain_header.bsize = 0;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
121 /* Skip over subsequent Bss Section Headers. */
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
122 for (section = last_data_section+1;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
123 (section->s_flags & STYP_BSS) != 0; section++) ;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
124 /* Update the remaining Section Headers. */
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
125 blocks_section = (struct scnhdr *) 0;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
126 first_changed_vaddr = 0;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
127 for (; section != sections_limit; section++)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
128 {
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
129 long delta = (section < rwdi_section ? delta_before_rwdi : delta_after_rwdi);
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
130 if (section->s_paddr != 0)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
131 section->s_paddr += delta;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
132 if (section->s_vaddr != 0)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
133 {
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
134 if (first_changed_vaddr == 0)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
135 first_changed_vaddr = section->s_vaddr;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
136 section->s_vaddr += delta;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
137 }
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
138 if (section->s_scnptr != 0)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
139 section->s_scnptr += delta;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
140 if (strcmp (section->s_name, ".blocks") == 0)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
141 blocks_section = section;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
142 else if (strcmp (section->s_name, ".sri") == 0 &&
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
143 domain_header.o_sri != 0)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
144 domain_header.o_sri += delta;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
145 else if (strcmp (section->s_name, ".inlib") == 0 &&
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
146 domain_header.o_inlib != 0)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
147 domain_header.o_inlib += delta;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
148 }
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
149 /* Open the Target File. */
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
150 ios_$create (target_file_name, strlen (target_file_name), coff_$uid,
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
151 ios_$recreate_mode, ios_$write_opt, &target_file, &status);
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
152 if (status.all != status_$ok)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
153 error ("cannot open target file for output");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
154 /* Write the File Header. */
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
155 if (write (target_file, &file_header, sizeof (file_header)) != sizeof (file_header))
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
156 error ("cannot write file header");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
157 /* Write the Domain Header. */
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
158 if (write (target_file, &domain_header, sizeof (domain_header))
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
159 != sizeof (domain_header))
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
160 error ("cannot write domain header");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
161 /* Write the Section Headers. */
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
162 if (write (target_file, sections, file_header.f_nscns*sizeof (struct scnhdr))
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
163 != file_header.f_nscns*sizeof (struct scnhdr))
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
164 error ("cannot write section headers");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
165 /* Copy the Allocated Sections. */
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
166 for (section = sections; section != first_data_section; section++)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
167 if (section->s_scnptr != 0)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
168 CopyData (target_file, source_file, LONG_ALIGN(section->s_size));
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
169 /* Write the Expanded Data Segment. */
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
170 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
171 error ("cannot write new data section");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
172
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
173 /* Skip over the Last Data Section and Copy until the .rwdi Section. */
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
174 if (lseek (source_file, last_data_section->s_scnptr
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
175 +old_data_section_size, L_SET) == -1)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
176 error ("cannot seek past data section");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
177 for (section = last_data_section+1; section != rwdi_section; section++)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
178 if (section->s_scnptr != 0)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
179 CopyData (target_file, source_file, LONG_ALIGN(section->s_size));
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
180 /* Skip over the .rwdi Section and Copy Remainder of Source File. */
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
181 if (lseek (source_file, source_file_offset_past_rwdi, L_SET) == -1)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
182 error ("cannot seek past .rwdi section");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
183 while ((byte_count = read (source_file, buffer, sizeof (buffer))) > 0)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
184 if (write (target_file, buffer, byte_count) != byte_count)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
185 error ("cannot write data");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
186 /* Unrelocate .data references to Global Symbols. */
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
187 for (section = first_data_section; section <= last_data_section; section++)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
188 for (i = 0; i < section->s_nreloc; i++)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
189 {
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
190 if (lseek (source_file, section->s_relptr
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
191 +i*sizeof (struct reloc)-delta_after_rwdi, L_SET) == -1)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
192 error ("cannot seek to relocation info");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
193 if (read (source_file, &reloc_entry, sizeof (reloc_entry))
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
194 != sizeof (reloc_entry))
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
195 error ("cannot read reloc entry");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
196 if (lseek (source_file, reloc_entry.r_vaddr-section->s_vaddr
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
197 +section->s_scnptr, L_SET) == -1)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
198 error ("cannot seek to data element");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
199 if (lseek (target_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 (read (source_file, buffer, 4) != 4)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
203 error ("cannot read data element");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
204 if (write (target_file, buffer, 4) != 4)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
205 error ("cannot write data element");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
206 }
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
207
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
208 /* Correct virtual addresses in .blocks section. */
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
209 if (blocks_section != (struct scnhdr *) 0)
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 dst_rec_t dst_record;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
212 dst_rec_comp_unit_t *comp_unit;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
213 unsigned short number_of_sections;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
214 unsigned long section_base;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
215 unsigned long section_offset = 0;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
216 /* Find section tables and update section base addresses. */
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
217 while (section_offset < blocks_section->s_size)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
218 {
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
219 if (lseek (target_file,
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
220 blocks_section->s_scnptr+section_offset, L_SET) == -1)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
221 error ("cannot seek to comp unit record");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
222 /* Handle pad records before the comp unit record. */
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
223 if (read (target_file, &dst_record, DST_RECORD_HDR_SIZE)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
224 != DST_RECORD_HDR_SIZE)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
225 error ("cannot read dst record tag");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
226 if (dst_record.rec_type == dst_typ_pad)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
227 section_offset += DST_RECORD_HDR_SIZE;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
228 else if (dst_record.rec_type == dst_typ_comp_unit)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
229 {
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
230 comp_unit = &dst_record.rec_data.comp_unit_;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
231 if (read (target_file, comp_unit, sizeof (*comp_unit))
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
232 != sizeof (*comp_unit))
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
233 error ("cannot read comp unit record");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
234 if (lseek (target_file, blocks_section->s_scnptr
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
235 +section_offset
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
236 #if dst_version_major == 1 && dst_version_minor < 4
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
237 +comp_unit->section_table
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
238 #else
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
239 +comp_unit->section_table.rel_offset
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
240 #endif
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
241 +DST_RECORD_HDR_SIZE,
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
242 L_SET) == -1)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
243 error ("cannot seek to section table");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
244 if (read (target_file, &number_of_sections, sizeof (number_of_sections))
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
245 != sizeof (number_of_sections))
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
246 error ("cannot read section table size");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
247 for (i = 0; i < number_of_sections; i++)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
248 {
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
249 if (read (target_file, &section_base, sizeof (section_base))
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
250 != sizeof (section_base))
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
251 error ("cannot read section base value");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
252 if (section_base < first_changed_vaddr)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
253 continue;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
254 else if (section_base < old_rwdi_vaddr)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
255 section_base += delta_before_rwdi;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
256 else section_base += delta_after_rwdi;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
257 if (lseek (target_file, -sizeof (section_base), L_INCR) == -1)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
258 error ("cannot seek to section base value");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
259 if (write (target_file, &section_base, sizeof (section_base))
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
260 != sizeof (section_base))
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
261 error ("cannot write section base");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
262 }
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
263 section_offset += comp_unit->data_size;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
264 }
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
265 else error ("unexpected dst record type");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
266 }
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
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
269 if (close (source_file) == -1)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
270 error("cannot close source file");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
271 if (close (target_file) == -1)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
272 error ("cannot close target file");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
273 }
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
274
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
275
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
276 static
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
277 CopyData (target_file, source_file, total_byte_count)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
278 int target_file, source_file;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
279 long total_byte_count;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
280 {
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
281 unsigned char buffer[4096];
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
282 long byte_count;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
283 while (total_byte_count > 0)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
284 {
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
285 if (total_byte_count > sizeof (buffer))
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
286 byte_count = sizeof (buffer);
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
287 else byte_count = total_byte_count;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
288 if (read (source_file, buffer, byte_count) != byte_count)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
289 error ("cannot read data");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
290 if (write (target_file, buffer, byte_count) != byte_count)
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
291 error ("cannot write data");
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
292 total_byte_count -= byte_count;
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
293 }
763c253911c3 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
294 }