annotate src/unexhp9k3.c @ 4072:aa28d959af41

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