Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-483
[bpt/emacs.git] / src / unexsunos4.c
CommitLineData
1a63b3de 1/* Unexec for Sunos 4 using shared libraries.
68c45bf0 2 Copyright (C) 1990, 1994, 1999 Free Software Foundation, Inc.
1a63b3de
RS
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
1a63b3de 20
2cad8dd6 21/* Contributed by Viktor Dukhovni. */
9889c728
JB
22/*
23 * Unexec for Berkeley a.out format + SUNOS shared libraries
24 * The unexeced executable contains the __DYNAMIC area from the
25 * original text file, and then the rest of data + bss + malloced area of
26 * the current process. (The __DYNAMIC area is at the top of the process
27 * data segment, we use "data_start" defined externally to mark the start
28 * of the "real" data segment.)
29 *
30 * For programs that want to remap some of the data segment read only
31 * a run_time_remap is provided. This attempts to remap largest area starting
32 * and ending on page boundaries between "data_start" and "bndry"
33 * For this it to figure out where the text file is located. A path search
34 * is attempted after trying argv[0] and if all fails we simply do not remap
35 *
36 * One feature of run_time_remap () is mandatory: reseting the break.
37 *
38 * Note that we can no longer map data into the text segment, as this causes
39 * the __DYNAMIC struct to become read only, breaking the runtime loader.
40 * Thus we no longer need to mess with a private crt0.c, the standard one
41 * will do just fine, since environ can live in the writable area between
42 * __DYNAMIC and data_start, just make sure that pre-crt0.o (the name
43 * is somewhat abused here) is loaded first!
44 *
9889c728 45 */
68c45bf0
PE
46
47#ifdef emacs
48#include <config.h>
49#endif
50
9889c728
JB
51#include <sys/param.h>
52#include <sys/mman.h>
53#include <sys/file.h>
54#include <sys/stat.h>
55#include <string.h>
56#include <stdio.h>
57#include <a.out.h>
58
7d158964 59#if defined (SUNOS4) || defined (__FreeBSD__) || defined (__NetBSD__)
ee3e4769
RS
60#define UNDO_RELOCATION
61#endif
62
63#ifdef UNDO_RELOCATION
1a63b3de
RS
64#include <link.h>
65#endif
66
cda2316f
RS
67#ifdef HAVE_UNISTD_H
68#include <unistd.h>
69#endif
26e0a910 70
3b95c869
RM
71/* NetBSD needs this bit, but SunOS does not have it. */
72#ifndef MAP_FILE
73#define MAP_FILE 0
74#endif
75
76
9889c728
JB
77/*
78 * for programs other than emacs
79 * define data_start + initialized here, and make sure
80 * this object is loaded first!
81 * emacs will define these elsewhere, and load the object containing
82 * data_start (pre-crt0.o or firstfile.o?) first!
83 * The custom crt0.o *must not* be loaded!
84 */
85#ifndef emacs
86 static int data_start = 0;
87 static int initialized = 0;
88#else
89 extern int initialized;
90 extern unsigned data_start;
91 extern int pureptr;
92#endif
93
94extern char *getenv ();
0c97bd6a 95static unsigned brk_value;
9889c728
JB
96static struct exec nhdr;
97static int rd_only_len;
98static long cookie;
99
100
177c0ea7 101unexec (new_name, a_name, bndry, bss_start, entry)
9889c728
JB
102 char *new_name, *a_name;
103 unsigned bndry, bss_start, entry;
104{
9889c728
JB
105 int fd, new;
106 char *old;
107 struct exec ohdr; /* Allocate on the stack, not needed in the next life */
108 struct stat stat;
109
9889c728
JB
110 if ((fd = open (a_name, O_RDONLY)) < 0)
111 {
112 fprintf (stderr, "%s: open: ", a_name);
113 perror (a_name);
114 exit (1);
115 }
116 if ((new = open (new_name, O_WRONLY | O_CREAT, 0666)) == -1)
117 {
118 fprintf (stderr, "%s: open: ", a_name);
119 perror (new_name);
120 exit (1);
121 }
122
123 if ((fstat (fd, &stat) == -1))
124 {
125 fprintf (stderr, "%s: ", a_name);
126 perror ("fstat");
127 exit (1);
128 }
129
3b95c869 130 old = (char *)mmap (0, stat.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd, 0);
9889c728
JB
131 if (old == (char *)-1)
132 {
133 fprintf (stderr, "%s: ", a_name);
134 perror ("mmap");
135 exit (1);
136 }
137 close (fd);
138
139 nhdr = ohdr = (*(struct exec *)old);
140
141
142 /*
eb8c3be9
JB
143 * Remember a magic cookie so we know we've got the right binary
144 * when remapping.
9889c728
JB
145 */
146 cookie = time (0);
147
0c97bd6a
RS
148 /* Save the break, it is reset to &_end (by ld.so?). */
149 brk_value = (unsigned) sbrk (0);
9889c728
JB
150
151 /*
152 * Round up data start to a page boundary (Lose if not a 2 power!)
153 */
154 data_start = ((((int)&data_start) - 1) & ~(N_PAGSIZ (nhdr) - 1)) + N_PAGSIZ (nhdr);
155
156 /*
157 * Round down read only pages to a multiple of the page size
158 */
159 if (bndry)
160 rd_only_len = ((int)bndry & ~(N_PAGSIZ (nhdr) - 1)) - data_start;
161
162#ifndef emacs
163 /* Have to do this some time before dumping the data */
164 initialized = 1;
165#endif
177c0ea7 166
0c97bd6a
RS
167 /* Handle new data and bss sizes and optional new entry point.
168 No one actually uses bss_start and entry, but tradition compels
169 one to support them.
170 Could complain if bss_start > brk_value,
171 but the caller is *supposed* to know what she is doing. */
172 nhdr.a_data = (bss_start ? bss_start : brk_value) - N_DATADDR (nhdr);
173 nhdr.a_bss = bss_start ? brk_value - bss_start : 0;
177c0ea7 174 if (entry)
9889c728
JB
175 nhdr.a_entry = entry;
176
177 /*
178 * Write out the text segment with new header
179 * Dynamic executables are ZMAGIC with N_TXTOFF==0 and the header
180 * part of the text segment, but no need to rely on this.
181 * So write the TEXT first, then go back replace the header.
182 * Doing it in the other order is less general!
183 */
184 lseek (new, N_TXTOFF (nhdr), L_SET);
185 write (new, old + N_TXTOFF (ohdr), N_TXTOFF (ohdr) + ohdr.a_text);
186 lseek (new, 0L, L_SET);
187 write (new, &nhdr, sizeof (nhdr));
188
189 /*
190 * Write out the head of the old data segment from the file not
191 * from core, this has the unresolved __DYNAMIC relocation data
192 * we need to reload
193 */
194 lseek (new, N_DATOFF (nhdr), L_SET);
195 write (new, old + N_DATOFF (ohdr), (int)&data_start - N_DATADDR (ohdr));
196
197 /*
198 * Copy the rest of the data from core
199 */
200 write (new, &data_start, N_BSSADDR (nhdr) - (int)&data_start);
201
202 /*
203 * Copy the symbol table and line numbers
204 */
205 lseek (new, N_TRELOFF (nhdr), L_SET);
206 write (new, old + N_TRELOFF (ohdr), stat.st_size - N_TRELOFF (ohdr));
207
1a63b3de
RS
208 /* Some other BSD systems use this file.
209 We don't know whether this change is right for them. */
ee3e4769 210#ifdef UNDO_RELOCATION
1a63b3de
RS
211 /* Undo the relocations done at startup by ld.so.
212 It will do these relocations again when we start the dumped Emacs.
213 Doing them twice gives incorrect results. */
214 {
a783251b 215 unsigned long daddr = N_DATADDR (ohdr);
1a63b3de 216 unsigned long rel, erel;
ee3e4769 217#ifdef SUNOS4
499a514d 218#ifdef SUNOS4_SHARED_LIBRARIES
a783251b
RS
219 extern struct link_dynamic _DYNAMIC;
220
221 /* SunOS4.x's ld_rel is relative to N_TXTADDR. */
499a514d
RS
222 if (!ohdr.a_dynamic)
223 /* This was statically linked. */
224 rel = erel = 0;
225 else if (_DYNAMIC.ld_version < 2)
1a63b3de 226 {
a783251b
RS
227 rel = _DYNAMIC.ld_un.ld_1->ld_rel + N_TXTADDR (ohdr);
228 erel = _DYNAMIC.ld_un.ld_1->ld_hash + N_TXTADDR (ohdr);
1a63b3de
RS
229 }
230 else
231 {
a783251b
RS
232 rel = _DYNAMIC.ld_un.ld_2->ld_rel + N_TXTADDR (ohdr);
233 erel = _DYNAMIC.ld_un.ld_2->ld_hash + N_TXTADDR (ohdr);
1a63b3de 234 }
499a514d
RS
235#else /* not SUNOS4_SHARED_LIBRARIES */
236 rel = erel = 0;
237#endif /* not SUNOS4_SHARED_LIBRARIES */
a783251b
RS
238#ifdef sparc
239#define REL_INFO_TYPE struct reloc_info_sparc
240#else
9629ced8 241#define REL_INFO_TYPE struct relocation_info
a783251b
RS
242#endif /* sparc */
243#define REL_TARGET_ADDRESS(r) (((REL_INFO_TYPE *)(r))->r_address)
ee3e4769 244#endif /* SUNOS4 */
7d158964 245#if defined (__FreeBSD__) || defined (__NetBSD__)
a783251b
RS
246 extern struct _dynamic _DYNAMIC;
247
248 /* FreeBSD's LD_REL is a virtual address itself. */
249 rel = LD_REL (&_DYNAMIC);
250 erel = rel + LD_RELSZ (&_DYNAMIC);
251#define REL_INFO_TYPE struct relocation_info
252#define REL_TARGET_ADDRESS(r) (((REL_INFO_TYPE *)(r))->r_address)
ee3e4769 253#endif
1a63b3de 254
a783251b 255 for (; rel < erel; rel += sizeof (REL_INFO_TYPE))
1a63b3de 256 {
a783251b
RS
257 /* This is the virtual address where ld.so will do relocation. */
258 unsigned long target = REL_TARGET_ADDRESS (rel);
259 /* This is the offset in the data segment. */
260 unsigned long segoffset = target - daddr;
261
262 /* If it is located below data_start, we have to do nothing here,
263 because the old data has been already written to the location. */
264 if (target < (unsigned long)&data_start)
265 continue;
266
267 lseek (new, N_DATOFF (nhdr) + segoffset, L_SET);
268 write (new, old + N_DATOFF (ohdr) + segoffset, sizeof (unsigned long));
1a63b3de
RS
269 }
270 }
ee3e4769 271#endif /* UNDO_RELOCATION */
1a63b3de 272
9889c728
JB
273 fchmod (new, 0755);
274}
275
276void
277run_time_remap (progname)
278 char *progname;
279{
280 char aout[MAXPATHLEN];
281 register char *path, *p;
282
283 /* Just in case */
284 if (!initialized)
285 return;
286
287 /* Restore the break */
0c97bd6a 288 brk ((char *) brk_value);
9889c728
JB
289
290 /* If nothing to remap: we are done! */
291 if (rd_only_len == 0)
292 return;
293
294 /*
295 * Attempt to find the executable
296 * First try argv[0], will almost always succeed as shells tend to give
297 * the full path from the hash list rather than using execvp ()
298 */
177c0ea7 299 if (is_it (progname))
9889c728
JB
300 return;
301
302 /*
303 * If argv[0] is a full path and does not exist, not much sense in
304 * searching further
305 */
177c0ea7 306 if (strchr (progname, '/'))
9889c728
JB
307 return;
308
309 /*
310 * Try to search for argv[0] on the PATH
311 */
312 path = getenv ("PATH");
313 if (path == NULL)
314 return;
315
316 while (*path)
317 {
318 /* copy through ':' or end */
319 for (p = aout; *p = *path; ++p, ++path)
320 if (*p == ':')
321 {
322 ++path; /* move past ':' */
323 break;
324 }
325 *p++ = '/';
326 strcpy (p, progname);
327 /*
328 * aout is a candidate full path name
329 */
330 if (is_it (aout))
331 return;
332 }
333}
334
a783251b
RS
335is_it (filename)
336 char *filename;
9889c728
JB
337{
338 int fd;
a783251b 339 long filenames_cookie;
9889c728
JB
340 struct exec hdr;
341
342 /*
343 * Open an executable and check for a valid header!
a783251b 344 * Can't bcmp the header with what we had, it may have been stripped!
9889c728
JB
345 * so we may save looking at non executables with the same name, mostly
346 * directories.
347 */
a783251b 348 fd = open (filename, O_RDONLY);
9889c728
JB
349 if (fd != -1)
350 {
351 if (read (fd, &hdr, sizeof (hdr)) == sizeof (hdr)
352 && !N_BADMAG (hdr) && N_DATOFF (hdr) == N_DATOFF (nhdr)
353 && N_TRELOFF (hdr) == N_TRELOFF (nhdr))
354 {
355 /* compare cookies */
356 lseek (fd, N_DATOFF (hdr) + (int)&cookie - N_DATADDR (hdr), L_SET);
a783251b
RS
357 read (fd, &filenames_cookie, sizeof (filenames_cookie));
358 if (filenames_cookie == cookie)
9889c728
JB
359 { /* Eureka */
360
361 /*
362 * Do the mapping
363 * The PROT_EXEC may not be needed, but it is safer this way.
364 * should the shared library decide to indirect through
365 * addresses in the data segment not part of __DYNAMIC
366 */
0c97bd6a 367 mmap ((char *) data_start, rd_only_len, PROT_READ | PROT_EXEC,
3b95c869 368 MAP_FILE | MAP_SHARED | MAP_FIXED, fd,
9889c728
JB
369 N_DATOFF (hdr) + data_start - N_DATADDR (hdr));
370 close (fd);
371 return 1;
372 }
373 }
374 close (fd);
375 }
376 return 0;
377}
ab5796a9
MB
378
379/* arch-tag: 30227420-2c6f-4700-a4f8-9e45e52f53b1
380 (do not change this comment) */