annotate src/unexsunos4.c @ 49597:e88404e8f2cf

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