annotate src/unexconvex.c @ 298:70ad99077275 r21-0b47

Import from CVS: tag r21-0b47
author cvs
date Mon, 13 Aug 2007 10:39:40 +0200
parents c42ec1d1cded
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 /* Modified version of unexec for convex machines.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 Copyright (C) 1985, 1986, 1988 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 under the terms of the GNU General Public License as published by the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 Free Software Foundation; either version 2, or (at your option) any
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 along with XEmacs; see the file COPYING. If not, write to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 Boston, MA 02111-1307, USA. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 /* Synched up with: FSF 19.31. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 /* modified for C-1 arch by jthomp@convex 871103 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 /* Corrected to support convex SOFF object file formats and thread specific
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 * regions. streepy@convex 890302
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 * unexec.c - Convert a running program into an a.out file.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 * Author: Spencer W. Thomas
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 * Computer Science Dept.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 * University of Utah
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 * Date: Tue Mar 2 1982
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 * Modified heavily since then.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 * Synopsis:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 * unexec (new_name, a_name, data_start, bss_start, entry_address)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 * char *new_name, *a_name;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 * unsigned data_start, bss_start, entry_address;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 * Takes a snapshot of the program and makes an a.out format file in the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 * file named by the string argument new_name.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 * If a_name is non-NULL, the symbol table will be taken from the given file.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 * On some machines, an existing a_name file is required.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 * The boundaries within the a.out file may be adjusted with the data_start
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 * and bss_start arguments. Either or both may be given as 0 for defaults.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 * Data_start gives the boundary between the text segment and the data
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 * segment of the program. The text segment can contain shared, read-only
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 * program code and literal data, while the data segment is always unshared
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 * and unprotected. Data_start gives the lowest unprotected address.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 * The value you specify may be rounded down to a suitable boundary
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 * as required by the machine you are using.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 * Specifying zero for data_start means the boundary between text and data
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 * should not be the same as when the program was loaded.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 * If NO_REMAP is defined, the argument data_start is ignored and the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 * segment boundaries are never changed.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 * Bss_start indicates how much of the data segment is to be saved in the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 * a.out file and restored when the program is executed. It gives the lowest
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 * unsaved address, and is rounded up to a page boundary. The default when 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 * is given assumes that the entire data segment is to be stored, including
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 * the previous data and bss as well as any additional storage allocated with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 * break (2).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 * The new file is set up to start at entry_address.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 * If you make improvements I'd like to get them too.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 * harpo!utah-cs!thomas, thomas@Utah-20
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 /* There are several compilation parameters affecting unexec:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 * COFF
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 Define this if your system uses COFF for executables.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 Otherwise we assume you use Berkeley format.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 * NO_REMAP
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 Define this if you do not want to try to save Emacs's pure data areas
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 as part of the text segment.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 Saving them as text is good because it allows users to share more.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 However, on machines that locate the text area far from the data area,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 the boundary cannot feasibly be moved. Such machines require
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 NO_REMAP.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 Also, remapping can cause trouble with the built-in startup routine
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 /lib/crt0.o, which defines `environ' as an initialized variable.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 Dumping `environ' as pure does not work! So, to use remapping,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 you must write a startup routine for your machine in Emacs's crt0.c.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 If NO_REMAP is defined, Emacs uses the system's crt0.o.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 * SECTION_ALIGNMENT
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 Some machines that use COFF executables require that each section
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 start on a certain boundary *in the COFF file*. Such machines should
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 define SECTION_ALIGNMENT to a mask of the low-order bits that must be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 zero on such a boundary. This mask is used to control padding between
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 segments in the COFF file.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 If SECTION_ALIGNMENT is not defined, the segments are written
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 consecutively with no attempt at alignment. This is right for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 unmodified system V.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 * SEGMENT_MASK
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 Some machines require that the beginnings and ends of segments
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 *in core* be on certain boundaries. For most machines, a page
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 boundary is sufficient. That is the default. When a larger
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 boundary is needed, define SEGMENT_MASK to a mask of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 the bits that must be zero on such a boundary.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 * A_TEXT_OFFSET(HDR)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 Some machines count the a.out header as part of the size of the text
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 segment (a_text); they may actually load the header into core as the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 first data in the text segment. Some have additional padding between
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 the header and the real text of the program that is counted in a_text.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 For these machines, define A_TEXT_OFFSET(HDR) to examine the header
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 structure HDR and return the number of bytes to add to `a_text'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 before writing it (above and beyond the number of bytes of actual
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 program text). HDR's standard fields are already correct, except that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 this adjustment to the `a_text' field has not yet been made;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 thus, the amount of offset can depend on the data in the file.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 * A_TEXT_SEEK(HDR)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 If defined, this macro specifies the number of bytes to seek into the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 a.out file before starting to write the text segment.a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 * EXEC_MAGIC
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 For machines using COFF, this macro, if defined, is a value stored
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 into the magic number field of the output file.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 * ADJUST_EXEC_HEADER
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 This macro can be used to generate statements to adjust or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 initialize nonstandard fields in the file header
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 * ADDR_CORRECT(ADDR)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 Macro to correct an int which is the bit pattern of a pointer to a byte
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 into an int which is the number of a byte.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 This macro has a default definition which is usually right.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 This default definition is a no-op on most machines (where a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 pointer looks like an int) but not on all machines.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 #include <config.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 #define PERROR(file) report_error (file, new)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 #include <a.out.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 /* Define getpagesize () if the system does not.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 Note that this may depend on symbols defined in a.out.h
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 #include "getpagesize.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 #include <sys/types.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 #include <stdio.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 #include <sys/stat.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 #include <errno.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 extern char *start_of_text (); /* Start of text */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 extern char *start_of_data (); /* Start of initialized data */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 #include <machine/filehdr.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 #include <machine/opthdr.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 #include <machine/scnhdr.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 #include <machine/pte.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 static long block_copy_start; /* Old executable start point */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 static struct filehdr f_hdr; /* File header */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 static struct opthdr f_ohdr; /* Optional file header (a.out) */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 long bias; /* Bias to add for growth */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 #define SYMS_START block_copy_start
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 static long text_scnptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 static long data_scnptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 static int pagemask;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 static int pagesz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 static
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 report_error (file, fd)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 char *file;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 int fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 if (fd)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 close (fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 error ("Failure operating on %s", file);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 #define ERROR0(msg) report_error_1 (new, msg, 0, 0); return -1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 #define ERROR1(msg,x) report_error_1 (new, msg, x, 0); return -1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 #define ERROR2(msg,x,y) report_error_1 (new, msg, x, y); return -1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 static
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 report_error_1 (fd, msg, a1, a2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 int fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 char *msg;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 int a1, a2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 close (fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 error (msg, a1, a2);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 /* ****************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 * unexec
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 * driving logic.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 unexec (new_name, a_name, data_start, bss_start, entry_address)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 char *new_name, *a_name;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 unsigned data_start, bss_start, entry_address;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 int new, a_out = -1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 if (a_name && (a_out = open (a_name, 0)) < 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 PERROR (a_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 if ((new = creat (new_name, 0666)) < 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 PERROR (new_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 if (make_hdr (new, a_out, data_start, bss_start, entry_address, a_name, new_name) < 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 || copy_text_and_data (new) < 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 || copy_sym (new, a_out, a_name, new_name) < 0 ) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 close (new);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 return -1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 close (new);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 if (a_out >= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 close (a_out);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 mark_x (new_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 /* ****************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 * make_hdr
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 * Make the header in the new a.out from the header in core.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 * Modify the text and data sizes.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 struct scnhdr *stbl; /* Table of all scnhdr's */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 struct scnhdr *f_thdr; /* Text section header */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 struct scnhdr *f_dhdr; /* Data section header */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 struct scnhdr *f_tdhdr; /* Thread Data section header */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 struct scnhdr *f_bhdr; /* Bss section header */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 struct scnhdr *f_tbhdr; /* Thread Bss section header */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 make_hdr (new, a_out, data_start, bss_start, entry_address, a_name, new_name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 int new, a_out;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 unsigned data_start, bss_start, entry_address;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 char *a_name;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 char *new_name;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 int scns;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 unsigned int bss_end;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 unsigned int eo_data; /* End of initialized data in new exec file */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 int scntype; /* Section type */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 int i; /* Var for sorting by vaddr */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 struct scnhdr scntemp; /* For swapping entries in sort */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 extern char *start_of_data();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 pagemask = (pagesz = getpagesize()) - 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 /* Adjust text/data boundary. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 if (!data_start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 data_start = (unsigned) start_of_data ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 data_start = data_start & ~pagemask; /* (Down) to page boundary. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 bss_end = (sbrk(0) + pagemask) & ~pagemask;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 /* Adjust data/bss boundary. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 if (bss_start != 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 bss_start = (bss_start + pagemask) & ~pagemask;/* (Up) to page bdry. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 if (bss_start > bss_end) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 ERROR1 ("unexec: Specified bss_start (%x) is past end of program",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 bss_start);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 } else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 bss_start = bss_end;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 if (data_start > bss_start) { /* Can't have negative data size. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 ERROR2 ("unexec: data_start (%x) can't be greater than bss_start (%x)",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 data_start, bss_start);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 /* Salvage as much info from the existing file as possible */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 if (a_out < 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 ERROR0 ("can't build a COFF file from scratch yet");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 /*NOTREACHED*/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr)) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 PERROR (a_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 block_copy_start += sizeof (f_hdr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 if (f_hdr.h_opthdr > 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 if (read (a_out, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr)) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 PERROR (a_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 block_copy_start += sizeof (f_ohdr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 /* Allocate room for scn headers */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 stbl = (struct scnhdr *)malloc( sizeof(struct scnhdr) * f_hdr.h_nscns );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 if( stbl == NULL ) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 ERROR0( "unexec: malloc of stbl failed" );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 f_tdhdr = f_tbhdr = NULL;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 /* Loop through section headers, copying them in */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 for (scns = 0; scns < f_hdr.h_nscns; scns++) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 if( read( a_out, &stbl[scns], sizeof(*stbl)) != sizeof(*stbl)) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 PERROR (a_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 scntype = stbl[scns].s_flags & S_TYPMASK; /* What type of section */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 if( stbl[scns].s_scnptr > 0L) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 if( block_copy_start < stbl[scns].s_scnptr + stbl[scns].s_size )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 block_copy_start = stbl[scns].s_scnptr + stbl[scns].s_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 if( scntype == S_TEXT) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 f_thdr = &stbl[scns];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 } else if( scntype == S_DATA) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 f_dhdr = &stbl[scns];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 #ifdef S_TDATA
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 } else if( scntype == S_TDATA ) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 f_tdhdr = &stbl[scns];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 } else if( scntype == S_TBSS ) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 f_tbhdr = &stbl[scns];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 #endif /* S_TDATA (thread stuff) */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 } else if( scntype == S_BSS) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 f_bhdr = &stbl[scns];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 /* We will now convert TEXT and DATA into TEXT, BSS into DATA, and leave
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 * all thread stuff alone.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 /* Now we alter the contents of all the f_*hdr variables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 to correspond to what we want to dump. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 f_thdr->s_vaddr = (long) start_of_text ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 f_thdr->s_size = data_start - f_thdr->s_vaddr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 f_thdr->s_scnptr = pagesz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 f_thdr->s_relptr = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 f_thdr->s_nrel = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 eo_data = f_thdr->s_scnptr + f_thdr->s_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 if( f_tdhdr ) { /* Process thread data */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 f_tdhdr->s_vaddr = data_start;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 f_tdhdr->s_size += f_dhdr->s_size - (data_start - f_dhdr->s_vaddr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 f_tdhdr->s_scnptr = eo_data;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 f_tdhdr->s_relptr = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 f_tdhdr->s_nrel = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 eo_data += f_tdhdr->s_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 /* And now for DATA */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 f_dhdr->s_vaddr = f_bhdr->s_vaddr; /* Take BSS start address */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 f_dhdr->s_size = bss_end - f_bhdr->s_vaddr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 f_dhdr->s_scnptr = eo_data;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391 f_dhdr->s_relptr = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 f_dhdr->s_nrel = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 eo_data += f_dhdr->s_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 } else {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 f_dhdr->s_vaddr = data_start;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 f_dhdr->s_size = bss_start - data_start;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 f_dhdr->s_scnptr = eo_data;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 f_dhdr->s_relptr = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 f_dhdr->s_nrel = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 eo_data += f_dhdr->s_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408 f_bhdr->s_vaddr = bss_start;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 f_bhdr->s_size = bss_end - bss_start + pagesz /* fudge */;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 f_bhdr->s_scnptr = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 f_bhdr->s_relptr = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 f_bhdr->s_nrel = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414 text_scnptr = f_thdr->s_scnptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 data_scnptr = f_dhdr->s_scnptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 bias = eo_data - block_copy_start;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 if (f_ohdr.o_symptr > 0L) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 f_ohdr.o_symptr += bias;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 if (f_hdr.h_strptr > 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423 f_hdr.h_strptr += bias;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 if (write (new, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr)) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427 PERROR (new_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430 if (write (new, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr)) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
431 PERROR (new_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434 for( scns = 0; scns < f_hdr.h_nscns; scns++ ) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436 /* This is a cheesy little loop to write out the section headers
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
437 * in order of increasing virtual address. Dull but effective.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440 for( i = scns+1; i < f_hdr.h_nscns; i++ ) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441 if( stbl[i].s_vaddr < stbl[scns].s_vaddr ) { /* Swap */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442 scntemp = stbl[i];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443 stbl[i] = stbl[scns];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 stbl[scns] = scntemp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450 for( scns = 0; scns < f_hdr.h_nscns; scns++ ) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 if( write( new, &stbl[scns], sizeof(*stbl)) != sizeof(*stbl)) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453 PERROR (new_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458 return (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462 /* ****************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463 * copy_text_and_data
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465 * Copy the text and data segments from memory to the new a.out
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
467 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
468 copy_text_and_data (new)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469 int new;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471 int scns;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
472
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473 for( scns = 0; scns < f_hdr.h_nscns; scns++ )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474 write_segment( new, &stbl[scns] );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
478
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479 write_segment( new, sptr )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480 int new;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481 struct scnhdr *sptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
483 char *ptr, *end;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
484 int nwrite, ret;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485 char buf[80];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486 extern int errno;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487 char zeros[128];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489 if( sptr->s_scnptr == 0 )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490 return; /* Nothing to do */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
491
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
492 if( lseek( new, (long) sptr->s_scnptr, 0 ) == -1 )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
493 PERROR( "unexecing" );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
494
282
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 0
diff changeset
495 memset (zeros, 0, sizeof zeros);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497 ptr = (char *) sptr->s_vaddr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498 end = ptr + sptr->s_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500 while( ptr < end ) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
502 /* distance to next multiple of 128. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
503 nwrite = (((int) ptr + 128) & -128) - (int) ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
504 /* But not beyond specified end. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505 if (nwrite > end - ptr) nwrite = end - ptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
506 ret = write (new, ptr, nwrite);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
507 /* If write gets a page fault, it means we reached
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
508 a gap between the old text segment and the old data segment.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
509 This gap has probably been remapped into part of the text segment.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
510 So write zeros for it. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
511 if (ret == -1 && errno == EFAULT)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
512 write (new, zeros, nwrite);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
513 else if (nwrite != ret) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
514 sprintf (buf,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
515 "unexec write failure: addr 0x%x, fileno %d, size 0x%x, wrote 0x%x, errno %d",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
516 ptr, new, nwrite, ret, errno);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
517 PERROR (buf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
518 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
519 ptr += nwrite;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
520 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
521 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
522
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
523 /* ****************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
524 * copy_sym
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
525 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
526 * Copy the relocation information and symbol table from the a.out to the new
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
527 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
528 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
529 copy_sym (new, a_out, a_name, new_name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
530 int new, a_out;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
531 char *a_name, *new_name;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
532 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
533 char page[1024];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
534 int n;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
535
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
536 if (a_out < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
537 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
538
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
539 if (SYMS_START == 0L)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
540 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
541
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
542 lseek (a_out, SYMS_START, 0); /* Position a.out to symtab. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
543 lseek( new, (long)f_ohdr.o_symptr, 0 );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
544
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
545 while ((n = read (a_out, page, sizeof page)) > 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
546 if (write (new, page, n) != n) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
547 PERROR (new_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
548 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
549 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
550 if (n < 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
551 PERROR (a_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
552 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
553 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
554 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
555
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
556 /* ****************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
557 * mark_x
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
558 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
559 * After successfully building the new a.out, mark it executable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
560 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
561 static
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
562 mark_x (name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
563 char *name;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
564 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
565 struct stat sbuf;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
566 int um;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
567 int new = 0; /* for PERROR */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
568
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
569 um = umask (777);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
570 umask (um);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
571 if (stat (name, &sbuf) == -1) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
572 PERROR (name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
573 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
574 sbuf.st_mode |= 0111 & ~um;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
575 if (chmod (name, sbuf.st_mode) == -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
576 PERROR (name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
577 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
578
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
579 /* Find the first pty letter. This is usually 'p', as in ptyp0, but
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
580 is sometimes configured down to 'm', 'n', or 'o' for some reason. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
581
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
582 first_pty_letter ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
583 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
584 struct stat buf;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
585 char pty_name[16];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
586 char c;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
587
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
588 for (c = 'o'; c >= 'a'; c--)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
589 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
590 sprintf (pty_name, "/dev/pty%c0", c);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
591 if (stat (pty_name, &buf) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
592 return c + 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
593 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
594 return 'a';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
595 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
596