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