annotate src/unexhp9k3.c @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children ac2d302a0011
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 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 * Code to do an unexec for HPUX 8.0 on an HP9000/[34]00 for a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 * dynamically linked temacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 Copyright (C) 1992-1993 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 under the terms of the GNU General Public License as published by the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 Free Software Foundation; either version 2, or (at your option) any
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 XEmacs is distributed in the hope that it will be useful, but WITHOUT
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 along with XEmacs; see the file COPYING. If not, write to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 Boston, MA 02111-1307, USA. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 /* Synched up with: Not in FSF. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 Created 29-Oct-92 by Harlan Sexton for SunOS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 Modified Jan 93 by Hamish Macdonald for HPUX
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 /********************** Included .h Files **************************/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 #include <config.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 #include <stdarg.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 #include <sys/param.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 #include <sys/file.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 #include <sys/stat.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 #include <sys/types.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 #include <string.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 #include <stdio.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 #include <signal.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 #include <a.out.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 #include <unistd.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 #include <ctype.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 #include <sys/dir.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 #include "sysdep.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 /* XEmacs: Richard Cognot <cognot@ensg.u-nancy.fr> says we need these */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 extern void perror(const char*);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 extern int sys_nerr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 extern char *sys_errlist[];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 extern char *strerror (int);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 /********************** Macros *************************************/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 #define SYS_ERR \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 ((errno > 0)?((errno < sys_nerr)?(sys_errlist[errno]):\
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 "unknown system error"): "unknown error")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 #define MASK_UP(x,p_of_two) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 ((((unsigned long) (x)) + ((p_of_two) - 1)) & (~((p_of_two) - 1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 #define MASK_DOWN(x,p_of_two) (((unsigned long) (x)) & (~((p_of_two) - 1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 /********************** Function Prototypes/Declarations ***********/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 static void unexec_error (const char *fmt, int use_errno, ...);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 static int unexec_open (char *filename, int flag, int mode);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 static long unexec_seek (int fd, long position);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 static void unexec_read (int fd, long position, char *buf, int bytes);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 static void unexec_write (int fd, long position, char *buf, int bytes);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 static void unexec_copy (int new_fd, int old_fd, long old_pos, long new_pos,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 int bytes);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 static void unexec_pad (int fd, int bytes);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 static void unexec_fstat (int fd, struct stat *statptr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 static void unexec_fchmod (int fd, int mode);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 int run_time_remap (char *dummy);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 /********************** Variables **********************************/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 /* for reporting error messages from system calls */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 extern int sys_nerr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 extern int errno;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 extern int _DYNAMIC;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 extern char **environ;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 static unsigned long sbrk_of_0_at_unexec;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 /*******************************************************************/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 unexec_error (const char *fmt, int use_errno, ...)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 const char *err_msg = SYS_ERR;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 va_list args;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 fprintf (stderr, "unexec - ");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 va_start (args, use_errno);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 vfprintf (stderr, fmt, args);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 va_end (args);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 if (use_errno)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 fprintf (stderr, ": %s", err_msg);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 fprintf (stderr, "\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 unexec_open (char *filename, int flag, int mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 int fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 errno = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 fd = open (filename, flag, mode);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 if (fd < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 unexec_error ("Failure opening file %s", 1, (void *) filename, 0, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 return -1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 return fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 static long
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 unexec_seek (int fd, long position)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 long seek_value;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 if (fd <= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 unexec_error ("No file open in which to seek", 0, 0, 0, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 errno = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 if (position < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 seek_value = (long) lseek (fd, 0, L_INCR);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 seek_value = (long) lseek (fd, position, L_SET);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 if (seek_value < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 unexec_error ("Failed to do a seek to 0x%x in %s", 1,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 (char *) position, "unexec() output file", 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 return seek_value;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 unexec_read (int fd, long position, char *buf, int bytes)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 int n_read;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 int remains = bytes;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 position = unexec_seek (fd, position);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 if (bytes < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 unexec_error ("Attempted read of %d bytes", 0, (char *) bytes, 0, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 errno = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 while (remains > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 n_read = read (fd, buf, remains);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 if (n_read <= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 unexec_error ("Read failed for 0x%x bytes at offset 0x%x in %s",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 1, (char *) bytes, (char *) position,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 "unexec() output file");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 buf += n_read;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 remains -= n_read;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 unexec_write (int fd, long position, char *buf, int bytes)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 int n_written;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 int remains = bytes;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 position = unexec_seek (fd, position);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 if (bytes < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 unexec_error ("Attempted write of %d bytes in %s",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 0, (char *) bytes, "unexec() output file", 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 errno = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 while (remains > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 n_written = write (fd, buf, remains);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 if (n_written <= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 unexec_error ("Write failed for 0x%x bytes at offset 0x%x in %s",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 1, (char *) bytes, (char *) position,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 "unexec() output file");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 buf += n_written;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 remains -= n_written;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 unexec_copy (int new_fd, int old_fd, long old_pos, long new_pos, int bytes)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 int remains = bytes;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 char buf[128];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 while (remains > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 int n_to_copy = remains > sizeof(buf) ? sizeof(buf) : remains;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 unexec_read (old_fd, old_pos, buf, n_to_copy);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 unexec_write (new_fd, new_pos, buf, n_to_copy);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 old_pos += n_to_copy;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 new_pos += n_to_copy;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 remains -= n_to_copy;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 unexec_pad (int fd, int bytes)
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 (bytes > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 char buf[1024];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 int remaining = bytes;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 bzero (buf, sizeof(buf));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 while (remaining > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 int this_write = (remaining > sizeof(buf))?sizeof(buf):remaining;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 unexec_write (fd, -1, buf, this_write);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 remaining -= this_write;
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 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 unexec_fstat (int fd, struct stat *statptr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 errno = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 if (-1 == fstat (fd, statptr))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 unexec_error ("fstat() failed for descriptor %d", 1, (char *) fd, 0, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 unexec_fchmod (int fd, int mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 errno = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 if (-1 == fchmod (fd, mode))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 unexec_error ("fchmod() failed for descriptor %d", 1, (char *) fd, 0, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 * EXPORTED FUNCTIONS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 /* this has to be a global variable to prevent the optimizers from
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 * assuming that it can not be 0.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 static void *dynamic_addr = (void *) &_DYNAMIC;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 unexec (char *new_name, char *old_name,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 unsigned int emacs_edata, unsigned int dummy1, unsigned int dummy2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 /* /dld.sl data */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 struct dynamic *ld = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 /* old and new state */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 int old_fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 int new_fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 struct exec old_hdr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 struct exec new_hdr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 struct stat old_buf;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 /* some process specific "constants" */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 unsigned long n_pagsiz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 caddr_t dynamic_beg;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 caddr_t current_break = (caddr_t) sbrk (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 /* dynamically linked image? -- if so, find dld.sl structures */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 if (dynamic_addr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 ld = (struct dynamic *) dynamic_addr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 #ifdef DEBUG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 printf ("dl_text = %#x\n", ld->text);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 printf ("dl_data = %#x\n", ld->data);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 printf ("dl_bss = %#x\n", ld->bss);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 printf ("dl_end = %#x\n", ld->end);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 printf ("dl_dmodule = %#x\n", ld->dmodule);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 printf ("dl_dlt = %#x\n", ld->dlt);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 printf ("dl_plt = %#x\n", ld->plt);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 #endif
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 /* open the old and new files, figuring out how big the old one is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 so that we can map it in */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 old_fd = unexec_open (old_name, O_RDONLY, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 new_fd = unexec_open (new_name, O_RDWR | O_CREAT | O_TRUNC, 0666);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 /* setup the header and the statbuf for old_fd */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 unexec_read (old_fd, 0, (char *) &old_hdr, sizeof (old_hdr));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 unexec_fstat (old_fd, &old_buf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 /* set up some important constants */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 n_pagsiz = EXEC_PAGESIZE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 /* setup beginning of data to copy from executable */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 if (ld)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 dynamic_beg = ld->dmodule;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 dynamic_beg = (caddr_t)EXEC_ALIGN (old_hdr.a_text) + old_hdr.a_data;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 /* set up the new exec */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 new_hdr = old_hdr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 new_hdr.a_text = MASK_DOWN (emacs_edata, n_pagsiz);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 new_hdr.a_data = MASK_UP (current_break, n_pagsiz)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 - EXEC_ALIGN(new_hdr.a_text);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 new_hdr.a_bss = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 #ifdef DEBUG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 printf ("old text %#x\n", old_hdr.a_text);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 printf ("new text %#x\n", new_hdr.a_text);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 printf ("old data %#x\n", old_hdr.a_data);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 printf ("new data %#x\n", new_hdr.a_data);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 printf ("old bss %#x\n", old_hdr.a_bss);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 printf ("new bss %#x\n", new_hdr.a_bss);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 /* set up this variable, in case we want to reset "the break"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 when restarting */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 sbrk_of_0_at_unexec = ((unsigned long) MASK_UP (current_break, n_pagsiz));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 /* Write out the first approximation to the new file. The sizes of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 each section will be correct, but there will be a number of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 corrections that will need to be made. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 long old_datoff = DATA_OFFSET (old_hdr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 long new_datoff = DATA_OFFSET (new_hdr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 long old_dataddr = EXEC_ALIGN (old_hdr.a_text);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 long new_dataddr = EXEC_ALIGN (new_hdr.a_text);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 long new_mcaloff = MODCAL_OFFSET (new_hdr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 long old_mcaloff = MODCAL_OFFSET (old_hdr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 long newtext_size = new_hdr.a_text - old_dataddr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 long newdata1_size = (unsigned long)dynamic_beg - new_dataddr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 long dyn_size = (EXEC_ALIGN (old_hdr.a_text) + old_hdr.a_data)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 - (unsigned long)dynamic_beg;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 long newdata2_size = (unsigned long)current_break
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 - ((unsigned long)dynamic_beg + dyn_size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 long pad_size =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 MASK_UP (current_break, n_pagsiz) - ((unsigned long) current_break);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 #ifdef DEBUG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 printf ("current break is %#lx\n", current_break);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 printf ("old_dataddr = %#lx, dynamic_beg = %#lx\n",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 old_dataddr, dynamic_beg);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 * First, write the text segment with new header -- copy
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 * everything until the start of the data segment from the old
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 * file
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 #ifdef DEBUG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 printf ("copying %#lx bytes of text from 0\n", old_datoff);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 unexec_copy (new_fd, old_fd, 0, 0, old_datoff);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 /* pad out the text segment */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 #ifdef DEBUG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 printf ( "text pad size is %#x\n", old_dataddr - old_hdr.a_text);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 unexec_pad (new_fd, old_dataddr - old_hdr.a_text);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 * go back and write the new header.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387 unexec_write (new_fd, 0, (char *) &new_hdr, sizeof (new_hdr));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391 * Copy the part of the data segment which becomes text from the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 * running image.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 #ifdef DEBUG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 printf ("copying %#lx bytes of new text from %#lx to position %#lx\n",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 newtext_size, old_dataddr, TEXT_OFFSET(new_hdr) + old_dataddr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 unexec_write (new_fd, TEXT_OFFSET(new_hdr) + old_dataddr,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 (caddr_t)old_dataddr, newtext_size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 #ifdef DEBUG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 printf ("new DATA_OFFSET is %#lx\n", new_datoff);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 * Copy the part of the old data segment which will be data
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 * in the new executable (before the dynamic stuff)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408 * from the running image.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 #ifdef DEBUG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 printf ("copying %#lx bytes of data from %#lx to position %#lx\n",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 newdata1_size, new_dataddr, new_datoff);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414 unexec_write (new_fd, new_datoff, (caddr_t)new_dataddr, newdata1_size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 /* copy the dynamic part of the data segment from the old executable */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 if (dyn_size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 #ifdef DEBUG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 printf ("copying %#lx bytes of dyn data from executable"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 " at address %#lx to position %#lx\n",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 dyn_size, dynamic_beg, new_datoff + newdata1_size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424 unexec_copy (new_fd, old_fd, old_datoff + newtext_size + newdata1_size,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 new_datoff + newdata1_size, dyn_size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 /* copy remaining data (old bss) from the running image */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 #ifdef DEBUG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430 printf ("copying %#lx bytes of data from %#lx to position %#lx\n",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
431 newdata2_size, new_dataddr + newdata1_size + dyn_size,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432 new_datoff + newdata1_size + dyn_size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434 unexec_write (new_fd, new_datoff + newdata1_size + dyn_size,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 (caddr_t)(new_dataddr + newdata1_size + dyn_size),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436 newdata2_size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
437
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438 /* pad out the data segment */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 #ifdef DEBUG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440 printf ( "pad size is %#x\n", pad_size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442 unexec_pad (new_fd, pad_size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 /* Finally, copy the rest of the junk from the old file. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445 #ifdef DEBUG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446 printf ("Copying %#lx bytes of junk from %#lx (old) to %#lx (new)\n",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447 old_buf.st_size - old_mcaloff, old_mcaloff, new_mcaloff);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449 unexec_copy (new_fd, old_fd, old_mcaloff, new_mcaloff,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450 old_buf.st_size - old_mcaloff);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454 /* make the output file executable -- then quit */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455 unexec_fchmod (new_fd, 0755);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 close (old_fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457 close (new_fd);
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 int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463 run_time_remap (char *dummy)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465 unsigned long current_sbrk = (unsigned long) sbrk (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
467 if (sbrk_of_0_at_unexec < current_sbrk)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
468 fprintf (stderr, "Absurd new brk addr = 0x%x (current = 0x%x)\n",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469 sbrk_of_0_at_unexec, current_sbrk);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
472 errno = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473 if (brk ((caddr_t) sbrk_of_0_at_unexec))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474 fprintf (stderr, "failed to change brk addr to 0x%x: %s\n",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475 sbrk_of_0_at_unexec, SYS_ERR);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
478 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479 }