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