annotate src/unexapollo.c @ 86589:a8357c53ed02

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