Mercurial > emacs
annotate src/unexsunos4.c @ 5648:bd8a172bf8a0
(update_end): Clear updating_frame before calling hook.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Fri, 21 Jan 1994 00:09:47 +0000 |
parents | e094a62cadcd |
children | a8aa874afd11 |
rev | line source |
---|---|
685 | 1 /* Contributed by Viktor Dukhovni. */ |
486 | 2 /* |
3 * Unexec for Berkeley a.out format + SUNOS shared libraries | |
4 * The unexeced executable contains the __DYNAMIC area from the | |
5 * original text file, and then the rest of data + bss + malloced area of | |
6 * the current process. (The __DYNAMIC area is at the top of the process | |
7 * data segment, we use "data_start" defined externally to mark the start | |
8 * of the "real" data segment.) | |
9 * | |
10 * For programs that want to remap some of the data segment read only | |
11 * a run_time_remap is provided. This attempts to remap largest area starting | |
12 * and ending on page boundaries between "data_start" and "bndry" | |
13 * For this it to figure out where the text file is located. A path search | |
14 * is attempted after trying argv[0] and if all fails we simply do not remap | |
15 * | |
16 * One feature of run_time_remap () is mandatory: reseting the break. | |
17 * | |
18 * Note that we can no longer map data into the text segment, as this causes | |
19 * the __DYNAMIC struct to become read only, breaking the runtime loader. | |
20 * Thus we no longer need to mess with a private crt0.c, the standard one | |
21 * will do just fine, since environ can live in the writable area between | |
22 * __DYNAMIC and data_start, just make sure that pre-crt0.o (the name | |
23 * is somewhat abused here) is loaded first! | |
24 * | |
25 */ | |
26 #ifdef emacs | |
4696
1fc792473491
Include <config.h> instead of "config.h".
Roland McGrath <roland@gnu.org>
parents:
3591
diff
changeset
|
27 #include <config.h> |
486 | 28 #endif |
29 | |
30 #include <sys/param.h> | |
31 #include <sys/mman.h> | |
32 #include <sys/file.h> | |
33 #include <sys/stat.h> | |
34 #include <string.h> | |
35 #include <stdio.h> | |
36 #include <a.out.h> | |
37 | |
5216
e094a62cadcd
[! MAP_FILE] (MAP_FILE): Define to zero.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
38 /* NetBSD needs this bit, but SunOS does not have it. */ |
e094a62cadcd
[! MAP_FILE] (MAP_FILE): Define to zero.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
39 #ifndef MAP_FILE |
e094a62cadcd
[! MAP_FILE] (MAP_FILE): Define to zero.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
40 #define MAP_FILE 0 |
e094a62cadcd
[! MAP_FILE] (MAP_FILE): Define to zero.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
41 #endif |
e094a62cadcd
[! MAP_FILE] (MAP_FILE): Define to zero.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
42 |
e094a62cadcd
[! MAP_FILE] (MAP_FILE): Define to zero.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
43 |
486 | 44 /* |
45 * for programs other than emacs | |
46 * define data_start + initialized here, and make sure | |
47 * this object is loaded first! | |
48 * emacs will define these elsewhere, and load the object containing | |
49 * data_start (pre-crt0.o or firstfile.o?) first! | |
50 * The custom crt0.o *must not* be loaded! | |
51 */ | |
52 #ifndef emacs | |
53 static int data_start = 0; | |
54 static int initialized = 0; | |
55 #else | |
56 extern int initialized; | |
57 extern unsigned data_start; | |
58 extern int pureptr; | |
59 #endif | |
60 | |
61 extern char *getenv (); | |
62 static unsigned Brk; | |
63 static struct exec nhdr; | |
64 static int rd_only_len; | |
65 static long cookie; | |
66 | |
67 | |
68 unexec (new_name, a_name, bndry, bss_start, entry) | |
69 char *new_name, *a_name; | |
70 unsigned bndry, bss_start, entry; | |
71 { | |
72 int fd, new; | |
73 char *old; | |
74 struct exec ohdr; /* Allocate on the stack, not needed in the next life */ | |
75 struct stat stat; | |
76 | |
77 #ifdef emacs | |
78 fprintf (stderr, "Used %d bytes of Pure Storage\n", pureptr); | |
79 #endif | |
80 | |
81 if ((fd = open (a_name, O_RDONLY)) < 0) | |
82 { | |
83 fprintf (stderr, "%s: open: ", a_name); | |
84 perror (a_name); | |
85 exit (1); | |
86 } | |
87 if ((new = open (new_name, O_WRONLY | O_CREAT, 0666)) == -1) | |
88 { | |
89 fprintf (stderr, "%s: open: ", a_name); | |
90 perror (new_name); | |
91 exit (1); | |
92 } | |
93 | |
94 if ((fstat (fd, &stat) == -1)) | |
95 { | |
96 fprintf (stderr, "%s: ", a_name); | |
97 perror ("fstat"); | |
98 exit (1); | |
99 } | |
100 | |
5216
e094a62cadcd
[! MAP_FILE] (MAP_FILE): Define to zero.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
101 old = (char *)mmap (0, stat.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd, 0); |
486 | 102 if (old == (char *)-1) |
103 { | |
104 fprintf (stderr, "%s: ", a_name); | |
105 perror ("mmap"); | |
106 exit (1); | |
107 } | |
108 close (fd); | |
109 | |
110 nhdr = ohdr = (*(struct exec *)old); | |
111 | |
112 | |
113 /* | |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
971
diff
changeset
|
114 * Remember a magic cookie so we know we've got the right binary |
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
971
diff
changeset
|
115 * when remapping. |
486 | 116 */ |
117 cookie = time (0); | |
118 | |
119 Brk = sbrk (0); /* Save the break, it is reset to &_end (by ld.so?) */ | |
120 | |
121 /* | |
122 * Round up data start to a page boundary (Lose if not a 2 power!) | |
123 */ | |
124 data_start = ((((int)&data_start) - 1) & ~(N_PAGSIZ (nhdr) - 1)) + N_PAGSIZ (nhdr); | |
125 | |
126 /* | |
127 * Round down read only pages to a multiple of the page size | |
128 */ | |
129 if (bndry) | |
130 rd_only_len = ((int)bndry & ~(N_PAGSIZ (nhdr) - 1)) - data_start; | |
131 | |
132 #ifndef emacs | |
133 /* Have to do this some time before dumping the data */ | |
134 initialized = 1; | |
135 #endif | |
136 | |
137 /* | |
138 * Handle new data and bss sizes and optional new entry point. | |
139 * No one actually uses bss_start and entry, but tradition compels | |
140 * one to support them. | |
141 * Could complain if bss_start > Brk, but the caller is *supposed* to know | |
142 * what she is doing. | |
143 */ | |
144 nhdr.a_data = (bss_start ? bss_start : Brk) - N_DATADDR (nhdr); | |
145 nhdr.a_bss = bss_start ? Brk - bss_start : 0; | |
146 if (entry) | |
147 nhdr.a_entry = entry; | |
148 | |
149 /* | |
150 * Write out the text segment with new header | |
151 * Dynamic executables are ZMAGIC with N_TXTOFF==0 and the header | |
152 * part of the text segment, but no need to rely on this. | |
153 * So write the TEXT first, then go back replace the header. | |
154 * Doing it in the other order is less general! | |
155 */ | |
156 lseek (new, N_TXTOFF (nhdr), L_SET); | |
157 write (new, old + N_TXTOFF (ohdr), N_TXTOFF (ohdr) + ohdr.a_text); | |
158 lseek (new, 0L, L_SET); | |
159 write (new, &nhdr, sizeof (nhdr)); | |
160 | |
161 /* | |
162 * Write out the head of the old data segment from the file not | |
163 * from core, this has the unresolved __DYNAMIC relocation data | |
164 * we need to reload | |
165 */ | |
166 lseek (new, N_DATOFF (nhdr), L_SET); | |
167 write (new, old + N_DATOFF (ohdr), (int)&data_start - N_DATADDR (ohdr)); | |
168 | |
169 /* | |
170 * Copy the rest of the data from core | |
171 */ | |
172 write (new, &data_start, N_BSSADDR (nhdr) - (int)&data_start); | |
173 | |
174 /* | |
175 * Copy the symbol table and line numbers | |
176 */ | |
177 lseek (new, N_TRELOFF (nhdr), L_SET); | |
178 write (new, old + N_TRELOFF (ohdr), stat.st_size - N_TRELOFF (ohdr)); | |
179 | |
180 fchmod (new, 0755); | |
181 } | |
182 | |
183 void | |
184 run_time_remap (progname) | |
185 char *progname; | |
186 { | |
187 char aout[MAXPATHLEN]; | |
188 register char *path, *p; | |
189 | |
190 /* Just in case */ | |
191 if (!initialized) | |
192 return; | |
193 | |
194 /* Restore the break */ | |
195 brk (Brk); | |
196 | |
197 /* If nothing to remap: we are done! */ | |
198 if (rd_only_len == 0) | |
199 return; | |
200 | |
201 /* | |
202 * Attempt to find the executable | |
203 * First try argv[0], will almost always succeed as shells tend to give | |
204 * the full path from the hash list rather than using execvp () | |
205 */ | |
206 if (is_it (progname)) | |
207 return; | |
208 | |
209 /* | |
210 * If argv[0] is a full path and does not exist, not much sense in | |
211 * searching further | |
212 */ | |
213 if (strchr (progname, '/')) | |
214 return; | |
215 | |
216 /* | |
217 * Try to search for argv[0] on the PATH | |
218 */ | |
219 path = getenv ("PATH"); | |
220 if (path == NULL) | |
221 return; | |
222 | |
223 while (*path) | |
224 { | |
225 /* copy through ':' or end */ | |
226 for (p = aout; *p = *path; ++p, ++path) | |
227 if (*p == ':') | |
228 { | |
229 ++path; /* move past ':' */ | |
230 break; | |
231 } | |
232 *p++ = '/'; | |
233 strcpy (p, progname); | |
234 /* | |
235 * aout is a candidate full path name | |
236 */ | |
237 if (is_it (aout)) | |
238 return; | |
239 } | |
240 } | |
241 | |
242 is_it (path) | |
243 char *path; | |
244 { | |
245 int fd; | |
246 long paths_cookie; | |
247 struct exec hdr; | |
248 | |
249 /* | |
250 * Open an executable and check for a valid header! | |
251 * Can't bcmp() the header with what we had, it may have been stripped! | |
252 * so we may save looking at non executables with the same name, mostly | |
253 * directories. | |
254 */ | |
255 fd = open (path, O_RDONLY); | |
256 if (fd != -1) | |
257 { | |
258 if (read (fd, &hdr, sizeof (hdr)) == sizeof (hdr) | |
259 && !N_BADMAG (hdr) && N_DATOFF (hdr) == N_DATOFF (nhdr) | |
260 && N_TRELOFF (hdr) == N_TRELOFF (nhdr)) | |
261 { | |
262 /* compare cookies */ | |
263 lseek (fd, N_DATOFF (hdr) + (int)&cookie - N_DATADDR (hdr), L_SET); | |
264 read (fd, &paths_cookie, sizeof (paths_cookie)); | |
265 if (paths_cookie == cookie) | |
266 { /* Eureka */ | |
267 | |
268 /* | |
269 * Do the mapping | |
270 * The PROT_EXEC may not be needed, but it is safer this way. | |
271 * should the shared library decide to indirect through | |
272 * addresses in the data segment not part of __DYNAMIC | |
273 */ | |
274 mmap (data_start, rd_only_len, PROT_READ | PROT_EXEC, | |
5216
e094a62cadcd
[! MAP_FILE] (MAP_FILE): Define to zero.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
275 MAP_FILE | MAP_SHARED | MAP_FIXED, fd, |
486 | 276 N_DATOFF (hdr) + data_start - N_DATADDR (hdr)); |
277 close (fd); | |
278 return 1; | |
279 } | |
280 } | |
281 close (fd); | |
282 } | |
283 return 0; | |
284 } |