annotate src/unexhp9k3.c @ 1296:87084e8445a7

[xemacs-hg @ 2003-02-14 09:50:15 by ben] syntax-table fixes 1. the updating of mirror tables every time a syntax table was modified was taking up huge amounts of time so i added a dirty flag and made the updating "just-in-time". 2. no-longer-used char-table-entries were not getting "freed", generating tons of garbage. 3. syntax_match() was being incorrectly called on mirror tables in the cache, not the original syntax table. buffer.c, syntax.c: Move syntax table description from buffer.c to syntax.c. chartab.c, chartab.h: Free extra char table entries to avoid excessive garbage. Add flags for dirty and mirror_table_p to char tables. Add a back pointer from mirror tables to the original syntax table. When modifying a syntax table, don't update the mirror table right away, just mark as dirty. Add various asserts to make sure we are dealing with the right type of table (mirror or non-mirror). font-lock.c, syntax.c, syntax.h: Add entry to syntax caches for the non-mirror table. Set it appropriately when initializing the syntax table. Use it, not the mirror table, for calls to syntax_match(). Don't create a bogus float each time, just once at startup. Add some asserts, as in chartab.c. syntax.h: When retrieving the syntax code, check the dirty flag and update the mirror tables as appropriate. Add some asserts, as above.
author ben
date Fri, 14 Feb 2003 09:50:17 +0000
parents 13e47461d509
children 04bc9d2f42c7
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"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
54
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
55 /* XEmacs: Richard Cognot <cognot@ensg.u-nancy.fr> says we need these */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
56 extern void perror(const char*);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
57 extern int sys_nerr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
58 extern char *sys_errlist[];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
59 extern char *strerror (int);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
60
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 /********************** Macros *************************************/
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
63
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
64 #define SYS_ERR \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
65 ((errno > 0)?((errno < sys_nerr)?(sys_errlist[errno]):\
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
66 "unknown system error"): "unknown error")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
67
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
68 #define MASK_UP(x,p_of_two) \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
69 ((((unsigned long) (x)) + ((p_of_two) - 1)) & (~((p_of_two) - 1)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
70
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
71 #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
72
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
73 /********************** Function Prototypes/Declarations ***********/
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
74
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 438
diff changeset
75 static void unexec_error (const char *fmt, int use_errno, ...);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
76 static int unexec_open (char *filename, int flag, int mode);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
77 static long unexec_seek (int fd, long position);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
78 static void unexec_read (int fd, long position, char *buf, int bytes);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
79 static void unexec_write (int fd, long position, char *buf, int bytes);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
80 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
81 int bytes);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
82 static void unexec_pad (int fd, int bytes);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
83 static void unexec_fstat (int fd, struct stat *statptr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
84 static void unexec_fchmod (int fd, int mode);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
85 int run_time_remap (char *dummy);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
86
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
87 /********************** Variables **********************************/
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
88
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
89 /* for reporting error messages from system calls */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
90 extern int sys_nerr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
91 extern int _DYNAMIC;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
92 extern char **environ;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
93
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
94 static unsigned long sbrk_of_0_at_unexec;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
95
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 static void
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 438
diff changeset
99 unexec_error (const char *fmt, int use_errno, ...)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
100 {
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 438
diff changeset
101 const char *err_msg = SYS_ERR;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
102 va_list args;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
103
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
104 fprintf (stderr, "unexec - ");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
105 va_start (args, use_errno);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
106 vfprintf (stderr, fmt, args);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
107 va_end (args);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
108
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
109 if (use_errno)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
110 fprintf (stderr, ": %s", err_msg);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
111 fprintf (stderr, "\n");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
112 exit (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
113 return;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
114 }
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 static int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
117 unexec_open (char *filename, int flag, int mode)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
118 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
119 int fd;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
120
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
121 errno = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
122
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
123 fd = open (filename, flag, mode);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
124
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
125 if (fd < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
126 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
127 unexec_error ("Failure opening file %s", 1, (void *) filename, 0, 0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
128 return -1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
129 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
130 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
131 return fd;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
132 }
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 static long
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
135 unexec_seek (int fd, long position)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
136 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
137 long seek_value;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
138
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
139 if (fd <= 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
140 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
141
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
142 errno = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
143
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
144 if (position < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
145 seek_value = (long) lseek (fd, 0, L_INCR);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
146 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
147 seek_value = (long) lseek (fd, position, L_SET);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
148
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
149 if (seek_value < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
150 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
151 (char *) position, "unexec() output file", 0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
152
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
153 return seek_value;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
154 }
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 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
157 unexec_read (int fd, long position, char *buf, int bytes)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
158 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
159 int n_read;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
160 int remains = bytes;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
161 position = unexec_seek (fd, position);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
162
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
163 if (bytes < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
164 unexec_error ("Attempted read of %d bytes", 0, (char *) bytes, 0, 0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
165
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
166 errno = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
167
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
168 while (remains > 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
169 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
170 n_read = read (fd, buf, remains);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
171 if (n_read <= 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
172 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
173 1, (char *) bytes, (char *) position,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
174 "unexec() output file");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
175 buf += n_read;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
176 remains -= n_read;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
177 }
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 return;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
180 }
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 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
183 unexec_write (int fd, long position, char *buf, int bytes)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
184 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
185 int n_written;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
186 int remains = bytes;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
187 position = unexec_seek (fd, position);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
188
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
189 if (bytes < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
190 unexec_error ("Attempted write of %d bytes in %s",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
191 0, (char *) bytes, "unexec() output file", 0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
192
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
193 errno = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
194
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
195 while (remains > 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
196 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
197 n_written = write (fd, buf, remains);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
198 if (n_written <= 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
199 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
200 1, (char *) bytes, (char *) position,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
201 "unexec() output file");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
202 buf += n_written;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
203 remains -= n_written;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
204 }
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 return;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
207 }
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 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
210 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
211 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
212 int remains = bytes;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
213 char buf[128];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
214
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
215 while (remains > 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
216 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
217 int n_to_copy = remains > sizeof(buf) ? sizeof(buf) : remains;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
218
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
219 unexec_read (old_fd, old_pos, buf, n_to_copy);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
220 unexec_write (new_fd, new_pos, buf, n_to_copy);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
221
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
222 old_pos += n_to_copy;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
223 new_pos += n_to_copy;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
224 remains -= n_to_copy;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
225 }
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 return;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
228 }
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 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
231 unexec_pad (int fd, int bytes)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
232 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
233 if (bytes > 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
234 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
235 char buf[1024];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
236 int remaining = bytes;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
237
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
238 memset (buf, 0, sizeof(buf));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
239
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
240 while (remaining > 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
241 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
242 int this_write = (remaining > sizeof(buf))?sizeof(buf):remaining;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
243 unexec_write (fd, -1, buf, this_write);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
244 remaining -= this_write;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
245 }
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 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
250 unexec_fstat (int fd, struct stat *statptr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
251 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
252 errno = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
253 if (-1 == fstat (fd, statptr))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
254 unexec_error ("fstat() failed for descriptor %d", 1, (char *) fd, 0, 0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
255 return;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
256 }
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 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
259 unexec_fchmod (int fd, int mode)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
260 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
261 errno = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
262 if (-1 == fchmod (fd, mode))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
263 unexec_error ("fchmod() failed for descriptor %d", 1, (char *) fd, 0, 0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
264 return;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
265 }
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 * EXPORTED FUNCTIONS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
269 */
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 /* this has to be a global variable to prevent the optimizers from
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
272 * assuming that it can not be 0.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
273 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
274 static void *dynamic_addr = (void *) &_DYNAMIC;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
275
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
276 int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
277 unexec (char *new_name, char *old_name,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
278 unsigned int emacs_edata, unsigned int dummy1, unsigned int dummy2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
279 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
280 /* /dld.sl data */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
281 struct dynamic *ld = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
282 /* old and new state */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
283 int old_fd;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
284 int new_fd;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
285 struct exec old_hdr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
286 struct exec new_hdr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
287 struct stat old_buf;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
288 /* some process specific "constants" */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
289 unsigned long n_pagsiz;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
290 caddr_t dynamic_beg;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
291 caddr_t current_break = (caddr_t) sbrk (0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
292
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
293 /* dynamically linked image? -- if so, find dld.sl structures */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
294 if (dynamic_addr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
295 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
296 ld = (struct dynamic *) dynamic_addr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
297 #ifdef DEBUG
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
298 printf ("dl_text = %#x\n", ld->text);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
299 printf ("dl_data = %#x\n", ld->data);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
300 printf ("dl_bss = %#x\n", ld->bss);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
301 printf ("dl_end = %#x\n", ld->end);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
302 printf ("dl_dmodule = %#x\n", ld->dmodule);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
303 printf ("dl_dlt = %#x\n", ld->dlt);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
304 printf ("dl_plt = %#x\n", ld->plt);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
305 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
306 }
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 /* 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
309 so that we can map it in */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
310 old_fd = unexec_open (old_name, O_RDONLY, 0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
311 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
312
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
313 /* setup the header and the statbuf for old_fd */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
314 unexec_read (old_fd, 0, (char *) &old_hdr, sizeof (old_hdr));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
315 unexec_fstat (old_fd, &old_buf);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
316
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
317 /* set up some important constants */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
318 n_pagsiz = EXEC_PAGESIZE;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
319
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
320 /* setup beginning of data to copy from executable */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
321 if (ld)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
322 dynamic_beg = ld->dmodule;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
323 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
324 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
325
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
326 /* set up the new exec */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
327 new_hdr = old_hdr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
328 new_hdr.a_text = MASK_DOWN (emacs_edata, n_pagsiz);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
329 new_hdr.a_data = MASK_UP (current_break, n_pagsiz)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
330 - EXEC_ALIGN(new_hdr.a_text);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
331 new_hdr.a_bss = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
332
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
333 #ifdef DEBUG
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
334 printf ("old text %#x\n", old_hdr.a_text);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
335 printf ("new text %#x\n", new_hdr.a_text);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
336 printf ("old data %#x\n", old_hdr.a_data);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
337 printf ("new data %#x\n", new_hdr.a_data);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
338 printf ("old bss %#x\n", old_hdr.a_bss);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
339 printf ("new bss %#x\n", new_hdr.a_bss);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
340 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
341
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
342 /* set up this variable, in case we want to reset "the break"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
343 when restarting */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
344 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
345
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
346 /* Write out the first approximation to the new file. The sizes of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
347 each section will be correct, but there will be a number of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
348 corrections that will need to be made. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
349 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
350 long old_datoff = DATA_OFFSET (old_hdr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
351 long new_datoff = DATA_OFFSET (new_hdr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
352 long old_dataddr = EXEC_ALIGN (old_hdr.a_text);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
353 long new_dataddr = EXEC_ALIGN (new_hdr.a_text);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
354 long new_mcaloff = MODCAL_OFFSET (new_hdr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
355 long old_mcaloff = MODCAL_OFFSET (old_hdr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
356 long newtext_size = new_hdr.a_text - old_dataddr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
357 long newdata1_size = (unsigned long)dynamic_beg - new_dataddr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
358 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
359 - (unsigned long)dynamic_beg;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
360 long newdata2_size = (unsigned long)current_break
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
361 - ((unsigned long)dynamic_beg + dyn_size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
362 long pad_size =
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
363 MASK_UP (current_break, n_pagsiz) - ((unsigned long) current_break);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
364
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
365 #ifdef DEBUG
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
366 printf ("current break is %#lx\n", current_break);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
367
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
368 printf ("old_dataddr = %#lx, dynamic_beg = %#lx\n",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
369 old_dataddr, dynamic_beg);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
370 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
371
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 * First, write the text segment with new header -- copy
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
374 * everything until the start of the data segment from the old
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
375 * file
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
376 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
377 #ifdef DEBUG
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
378 printf ("copying %#lx bytes of text from 0\n", old_datoff);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
379 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
380 unexec_copy (new_fd, old_fd, 0, 0, old_datoff);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
381 /* pad out the text segment */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
382 #ifdef DEBUG
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
383 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
384 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
385 unexec_pad (new_fd, old_dataddr - old_hdr.a_text);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
386
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 * Update debug header spoo
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
389 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
390 if (new_hdr.a_extension > 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
391 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
392 new_hdr.a_extension += LESYM_OFFSET(new_hdr) - LESYM_OFFSET(old_hdr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
393 }
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 * go back and write the new header.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
397 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
398 unexec_write (new_fd, 0, (char *) &new_hdr, sizeof (new_hdr));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
399
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 * Copy the part of the data segment which becomes text from the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
403 * running image.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
404 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
405 #ifdef DEBUG
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
406 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
407 newtext_size, old_dataddr, TEXT_OFFSET(new_hdr) + old_dataddr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
408 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
409 unexec_write (new_fd, TEXT_OFFSET(new_hdr) + old_dataddr,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
410 (caddr_t)old_dataddr, newtext_size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
411
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
412 #ifdef DEBUG
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
413 printf ("new DATA_OFFSET is %#lx\n", new_datoff);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
414 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
415
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 * Copy the part of the old data segment which will be data
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
418 * in the new executable (before the dynamic stuff)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
419 * from the running image.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
420 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
421 #ifdef DEBUG
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
422 printf ("copying %#lx bytes of data from %#lx to position %#lx\n",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
423 newdata1_size, new_dataddr, new_datoff);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
424 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
425 unexec_write (new_fd, new_datoff, (caddr_t)new_dataddr, newdata1_size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
426
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
427 /* copy the dynamic part of the data segment from the old executable */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
428 if (dyn_size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
429 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
430 #ifdef DEBUG
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
431 printf ("copying %#lx bytes of dyn data from executable"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
432 " at address %#lx to position %#lx\n",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
433 dyn_size, dynamic_beg, new_datoff + newdata1_size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
434 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
435 unexec_copy (new_fd, old_fd, old_datoff + newtext_size + newdata1_size,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
436 new_datoff + newdata1_size, dyn_size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
437 }
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 /* copy remaining data (old bss) from the running image */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
440 #ifdef DEBUG
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
441 printf ("copying %#lx bytes of data from %#lx to position %#lx\n",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
442 newdata2_size, new_dataddr + newdata1_size + dyn_size,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
443 new_datoff + newdata1_size + dyn_size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
444 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
445 unexec_write (new_fd, new_datoff + newdata1_size + dyn_size,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
446 (caddr_t)(new_dataddr + newdata1_size + dyn_size),
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
447 newdata2_size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
448
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
449 /* pad out the data segment */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
450 #ifdef DEBUG
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
451 printf ( "pad size is %#x\n", pad_size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
452 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
453 unexec_pad (new_fd, pad_size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
454
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
455 /* Finally, copy the rest of the junk from the old file. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
456 #ifdef DEBUG
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
457 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
458 old_buf.st_size - old_mcaloff, old_mcaloff, new_mcaloff);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
459 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
460 unexec_copy (new_fd, old_fd, old_mcaloff, new_mcaloff,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
461 old_buf.st_size - old_mcaloff);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
462
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 long curpos, offset;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
465 struct _debug_header dhdr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
466 int new_header_delta;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
467
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
468 new_header_delta = LESYM_OFFSET(new_hdr) - LESYM_OFFSET(old_hdr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
469 if ((new_header_delta > 0) &&
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
470 ((offset = EXT_OFFSET(old_hdr)) > 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
471 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
472 curpos = lseek(new_fd, 0, SEEK_CUR);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
473 lseek(old_fd, offset, 0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
474 if (read(old_fd, &dhdr, sizeof(dhdr)) == sizeof(dhdr))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
475 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
476 dhdr.header_offset += new_header_delta;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
477 dhdr.gntt_offset += new_header_delta;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
478 dhdr.lntt_offset += new_header_delta;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
479 dhdr.slt_offset += new_header_delta;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
480 dhdr.vt_offset += new_header_delta;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
481 dhdr.xt_offset += new_header_delta;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
482 lseek(new_fd, EXT_OFFSET(new_hdr), SEEK_SET);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
483 if (write(new_fd, &dhdr, sizeof(dhdr)) != sizeof(dhdr))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
484 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
485 unexec_error("Unable to write debug information to \"%s\"\n",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
486 1, new_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
487 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
488 lseek(new_fd, curpos, SEEK_SET);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
489 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
490 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
491 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
492 unexec_error("Unable to read debug information from \"%s\"\n",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
493 1, old_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
494 }
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 /* make the output file executable -- then quit */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
501 unexec_fchmod (new_fd, 0755);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
502 close (old_fd);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
503 close (new_fd);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
504 return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
505 }
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 int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
509 run_time_remap (char *dummy)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
510 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
511 unsigned long current_sbrk = (unsigned long) sbrk (0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
512
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
513 if (sbrk_of_0_at_unexec < current_sbrk)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
514 fprintf (stderr, "Absurd new brk addr = 0x%x (current = 0x%x)\n",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
515 sbrk_of_0_at_unexec, current_sbrk);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
516 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
517 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
518 errno = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
519 if (brk ((caddr_t) sbrk_of_0_at_unexec))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
520 fprintf (stderr, "failed to change brk addr to 0x%x: %s\n",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
521 sbrk_of_0_at_unexec, SYS_ERR);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
522 }
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 return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
525 }