Mercurial > emacs
annotate src/unexsunos4.c @ 9011:cece83c47ca5
(x_destroy_window): Decrement ref count of x_screen struct.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Fri, 23 Sep 1994 04:54:46 +0000 |
parents | 0675fa3d65fa |
children | a9fb36b25984 |
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. |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
2 Copyright (C) 1990, 1994 Free Software Foundation, Inc. |
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 |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
19 |
685 | 20 /* Contributed by Viktor Dukhovni. */ |
486 | 21 /* |
22 * Unexec for Berkeley a.out format + SUNOS shared libraries | |
23 * The unexeced executable contains the __DYNAMIC area from the | |
24 * original text file, and then the rest of data + bss + malloced area of | |
25 * the current process. (The __DYNAMIC area is at the top of the process | |
26 * data segment, we use "data_start" defined externally to mark the start | |
27 * of the "real" data segment.) | |
28 * | |
29 * For programs that want to remap some of the data segment read only | |
30 * a run_time_remap is provided. This attempts to remap largest area starting | |
31 * and ending on page boundaries between "data_start" and "bndry" | |
32 * For this it to figure out where the text file is located. A path search | |
33 * is attempted after trying argv[0] and if all fails we simply do not remap | |
34 * | |
35 * One feature of run_time_remap () is mandatory: reseting the break. | |
36 * | |
37 * Note that we can no longer map data into the text segment, as this causes | |
38 * the __DYNAMIC struct to become read only, breaking the runtime loader. | |
39 * Thus we no longer need to mess with a private crt0.c, the standard one | |
40 * will do just fine, since environ can live in the writable area between | |
41 * __DYNAMIC and data_start, just make sure that pre-crt0.o (the name | |
42 * is somewhat abused here) is loaded first! | |
43 * | |
44 */ | |
45 #include <sys/param.h> | |
46 #include <sys/mman.h> | |
47 #include <sys/file.h> | |
48 #include <sys/stat.h> | |
49 #include <string.h> | |
50 #include <stdio.h> | |
51 #include <a.out.h> | |
52 | |
6016
a8aa874afd11
Move config.h after system includes.
Roland McGrath <roland@gnu.org>
parents:
5216
diff
changeset
|
53 /* Do this after the above #include's in case a configuration file wants |
a8aa874afd11
Move config.h after system includes.
Roland McGrath <roland@gnu.org>
parents:
5216
diff
changeset
|
54 to define things for this file based on what <a.out.h> defines. */ |
a8aa874afd11
Move config.h after system includes.
Roland McGrath <roland@gnu.org>
parents:
5216
diff
changeset
|
55 #ifdef emacs |
a8aa874afd11
Move config.h after system includes.
Roland McGrath <roland@gnu.org>
parents:
5216
diff
changeset
|
56 #include <config.h> |
a8aa874afd11
Move config.h after system includes.
Roland McGrath <roland@gnu.org>
parents:
5216
diff
changeset
|
57 #endif |
a8aa874afd11
Move config.h after system includes.
Roland McGrath <roland@gnu.org>
parents:
5216
diff
changeset
|
58 |
8952
0675fa3d65fa
(SUNOS4 or __FreeBSD__): Define UNDO_RELOCATION.
Richard M. Stallman <rms@gnu.org>
parents:
8865
diff
changeset
|
59 #if defined (SUNOS4) || defined (__FreeBSD__) |
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 |
8952
0675fa3d65fa
(SUNOS4 or __FreeBSD__): Define UNDO_RELOCATION.
Richard M. Stallman <rms@gnu.org>
parents:
8865
diff
changeset
|
67 #ifdef __FreeBSD__ |
0675fa3d65fa
(SUNOS4 or __FreeBSD__): Define UNDO_RELOCATION.
Richard M. Stallman <rms@gnu.org>
parents:
8865
diff
changeset
|
68 #define link_dynamic _dynamic |
0675fa3d65fa
(SUNOS4 or __FreeBSD__): Define UNDO_RELOCATION.
Richard M. Stallman <rms@gnu.org>
parents:
8865
diff
changeset
|
69 #define ld_un d_un |
0675fa3d65fa
(SUNOS4 or __FreeBSD__): Define UNDO_RELOCATION.
Richard M. Stallman <rms@gnu.org>
parents:
8865
diff
changeset
|
70 #define ld_2 d_sdt |
0675fa3d65fa
(SUNOS4 or __FreeBSD__): Define UNDO_RELOCATION.
Richard M. Stallman <rms@gnu.org>
parents:
8865
diff
changeset
|
71 #define ld_rel sdt_rel |
0675fa3d65fa
(SUNOS4 or __FreeBSD__): Define UNDO_RELOCATION.
Richard M. Stallman <rms@gnu.org>
parents:
8865
diff
changeset
|
72 #define ld_hash sdt_hash |
0675fa3d65fa
(SUNOS4 or __FreeBSD__): Define UNDO_RELOCATION.
Richard M. Stallman <rms@gnu.org>
parents:
8865
diff
changeset
|
73 #endif /* __FreeBSD__ */ |
0675fa3d65fa
(SUNOS4 or __FreeBSD__): Define UNDO_RELOCATION.
Richard M. Stallman <rms@gnu.org>
parents:
8865
diff
changeset
|
74 |
6865
82b01d3bcead
[HAVE_UNISTD_H]: Include unistd.h.
Richard M. Stallman <rms@gnu.org>
parents:
6016
diff
changeset
|
75 #ifdef HAVE_UNISTD_H |
82b01d3bcead
[HAVE_UNISTD_H]: Include unistd.h.
Richard M. Stallman <rms@gnu.org>
parents:
6016
diff
changeset
|
76 #include <unistd.h> |
82b01d3bcead
[HAVE_UNISTD_H]: Include unistd.h.
Richard M. Stallman <rms@gnu.org>
parents:
6016
diff
changeset
|
77 #endif |
6016
a8aa874afd11
Move config.h after system includes.
Roland McGrath <roland@gnu.org>
parents:
5216
diff
changeset
|
78 |
5216
e094a62cadcd
[! MAP_FILE] (MAP_FILE): Define to zero.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
79 /* 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
|
80 #ifndef MAP_FILE |
e094a62cadcd
[! MAP_FILE] (MAP_FILE): Define to zero.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
81 #define MAP_FILE 0 |
e094a62cadcd
[! MAP_FILE] (MAP_FILE): Define to zero.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
82 #endif |
e094a62cadcd
[! MAP_FILE] (MAP_FILE): Define to zero.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
83 |
e094a62cadcd
[! MAP_FILE] (MAP_FILE): Define to zero.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
84 |
486 | 85 /* |
86 * for programs other than emacs | |
87 * define data_start + initialized here, and make sure | |
88 * this object is loaded first! | |
89 * emacs will define these elsewhere, and load the object containing | |
90 * data_start (pre-crt0.o or firstfile.o?) first! | |
91 * The custom crt0.o *must not* be loaded! | |
92 */ | |
93 #ifndef emacs | |
94 static int data_start = 0; | |
95 static int initialized = 0; | |
96 #else | |
97 extern int initialized; | |
98 extern unsigned data_start; | |
99 extern int pureptr; | |
100 #endif | |
101 | |
102 extern char *getenv (); | |
103 static unsigned Brk; | |
104 static struct exec nhdr; | |
105 static int rd_only_len; | |
106 static long cookie; | |
107 | |
108 | |
109 unexec (new_name, a_name, bndry, bss_start, entry) | |
110 char *new_name, *a_name; | |
111 unsigned bndry, bss_start, entry; | |
112 { | |
113 int fd, new; | |
114 char *old; | |
115 struct exec ohdr; /* Allocate on the stack, not needed in the next life */ | |
116 struct stat stat; | |
117 | |
118 #ifdef emacs | |
119 fprintf (stderr, "Used %d bytes of Pure Storage\n", pureptr); | |
120 #endif | |
121 | |
122 if ((fd = open (a_name, O_RDONLY)) < 0) | |
123 { | |
124 fprintf (stderr, "%s: open: ", a_name); | |
125 perror (a_name); | |
126 exit (1); | |
127 } | |
128 if ((new = open (new_name, O_WRONLY | O_CREAT, 0666)) == -1) | |
129 { | |
130 fprintf (stderr, "%s: open: ", a_name); | |
131 perror (new_name); | |
132 exit (1); | |
133 } | |
134 | |
135 if ((fstat (fd, &stat) == -1)) | |
136 { | |
137 fprintf (stderr, "%s: ", a_name); | |
138 perror ("fstat"); | |
139 exit (1); | |
140 } | |
141 | |
5216
e094a62cadcd
[! MAP_FILE] (MAP_FILE): Define to zero.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
142 old = (char *)mmap (0, stat.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd, 0); |
486 | 143 if (old == (char *)-1) |
144 { | |
145 fprintf (stderr, "%s: ", a_name); | |
146 perror ("mmap"); | |
147 exit (1); | |
148 } | |
149 close (fd); | |
150 | |
151 nhdr = ohdr = (*(struct exec *)old); | |
152 | |
153 | |
154 /* | |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
971
diff
changeset
|
155 * 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
|
156 * when remapping. |
486 | 157 */ |
158 cookie = time (0); | |
159 | |
160 Brk = sbrk (0); /* Save the break, it is reset to &_end (by ld.so?) */ | |
161 | |
162 /* | |
163 * Round up data start to a page boundary (Lose if not a 2 power!) | |
164 */ | |
165 data_start = ((((int)&data_start) - 1) & ~(N_PAGSIZ (nhdr) - 1)) + N_PAGSIZ (nhdr); | |
166 | |
167 /* | |
168 * Round down read only pages to a multiple of the page size | |
169 */ | |
170 if (bndry) | |
171 rd_only_len = ((int)bndry & ~(N_PAGSIZ (nhdr) - 1)) - data_start; | |
172 | |
173 #ifndef emacs | |
174 /* Have to do this some time before dumping the data */ | |
175 initialized = 1; | |
176 #endif | |
177 | |
178 /* | |
179 * Handle new data and bss sizes and optional new entry point. | |
180 * No one actually uses bss_start and entry, but tradition compels | |
181 * one to support them. | |
182 * Could complain if bss_start > Brk, but the caller is *supposed* to know | |
183 * what she is doing. | |
184 */ | |
185 nhdr.a_data = (bss_start ? bss_start : Brk) - N_DATADDR (nhdr); | |
186 nhdr.a_bss = bss_start ? Brk - bss_start : 0; | |
187 if (entry) | |
188 nhdr.a_entry = entry; | |
189 | |
190 /* | |
191 * Write out the text segment with new header | |
192 * Dynamic executables are ZMAGIC with N_TXTOFF==0 and the header | |
193 * part of the text segment, but no need to rely on this. | |
194 * So write the TEXT first, then go back replace the header. | |
195 * Doing it in the other order is less general! | |
196 */ | |
197 lseek (new, N_TXTOFF (nhdr), L_SET); | |
198 write (new, old + N_TXTOFF (ohdr), N_TXTOFF (ohdr) + ohdr.a_text); | |
199 lseek (new, 0L, L_SET); | |
200 write (new, &nhdr, sizeof (nhdr)); | |
201 | |
202 /* | |
203 * Write out the head of the old data segment from the file not | |
204 * from core, this has the unresolved __DYNAMIC relocation data | |
205 * we need to reload | |
206 */ | |
207 lseek (new, N_DATOFF (nhdr), L_SET); | |
208 write (new, old + N_DATOFF (ohdr), (int)&data_start - N_DATADDR (ohdr)); | |
209 | |
210 /* | |
211 * Copy the rest of the data from core | |
212 */ | |
213 write (new, &data_start, N_BSSADDR (nhdr) - (int)&data_start); | |
214 | |
215 /* | |
216 * Copy the symbol table and line numbers | |
217 */ | |
218 lseek (new, N_TRELOFF (nhdr), L_SET); | |
219 write (new, old + N_TRELOFF (ohdr), stat.st_size - N_TRELOFF (ohdr)); | |
220 | |
8859
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
221 /* Some other BSD systems use this file. |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
222 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
|
223 #ifdef UNDO_RELOCATION |
8859
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
224 /* Undo the relocations done at startup by ld.so. |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
225 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
|
226 Doing them twice gives incorrect results. */ |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
227 { |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
228 extern struct link_dynamic _DYNAMIC; |
8865
3fe65c32616b
(unexec): Fix logic for rounding section boundaries.
Richard M. Stallman <rms@gnu.org>
parents:
8859
diff
changeset
|
229 unsigned long taddr = N_TXTADDR (ohdr) - N_TXTOFF (ohdr); |
3fe65c32616b
(unexec): Fix logic for rounding section boundaries.
Richard M. Stallman <rms@gnu.org>
parents:
8859
diff
changeset
|
230 unsigned long daddr = N_DATADDR (ohdr) - N_DATOFF (ohdr); |
8859
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
231 unsigned long rel, erel; |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
232 unsigned rel_size; |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
233 |
8952
0675fa3d65fa
(SUNOS4 or __FreeBSD__): Define UNDO_RELOCATION.
Richard M. Stallman <rms@gnu.org>
parents:
8865
diff
changeset
|
234 #ifdef SUNOS4 |
8859
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
235 if (_DYNAMIC.ld_version < 2) |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
236 { |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
237 rel = _DYNAMIC.ld_un.ld_1->ld_rel; |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
238 erel = _DYNAMIC.ld_un.ld_1->ld_hash; |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
239 } |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
240 else |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
241 { |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
242 rel = _DYNAMIC.ld_un.ld_2->ld_rel; |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
243 erel = _DYNAMIC.ld_un.ld_2->ld_hash; |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
244 } |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
245 |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
246 switch (ohdr.a_machtype) |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
247 { |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
248 case M_68010: |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
249 case M_68020: |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
250 rel_size = 8; /* sizeof(struct reloc_info_m68k) */ |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
251 break; |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
252 case M_SPARC: |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
253 rel_size = 12; /* sizeof(struct reloc_info_sparc) */ |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
254 break; |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
255 case M_OLDSUN2: |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
256 default: |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
257 fatal ("unknown machine type in unexec!\n"); |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
258 } |
8952
0675fa3d65fa
(SUNOS4 or __FreeBSD__): Define UNDO_RELOCATION.
Richard M. Stallman <rms@gnu.org>
parents:
8865
diff
changeset
|
259 #endif /* SUNOS4 */ |
0675fa3d65fa
(SUNOS4 or __FreeBSD__): Define UNDO_RELOCATION.
Richard M. Stallman <rms@gnu.org>
parents:
8865
diff
changeset
|
260 #ifdef __FreeBSD__ |
0675fa3d65fa
(SUNOS4 or __FreeBSD__): Define UNDO_RELOCATION.
Richard M. Stallman <rms@gnu.org>
parents:
8865
diff
changeset
|
261 rel = _DYNAMIC.ld_un.ld_2->ld_rel; |
0675fa3d65fa
(SUNOS4 or __FreeBSD__): Define UNDO_RELOCATION.
Richard M. Stallman <rms@gnu.org>
parents:
8865
diff
changeset
|
262 erel = _DYNAMIC.ld_un.ld_2->ld_hash; |
0675fa3d65fa
(SUNOS4 or __FreeBSD__): Define UNDO_RELOCATION.
Richard M. Stallman <rms@gnu.org>
parents:
8865
diff
changeset
|
263 rel_size = 8; /* sizeof(struct relocation_info) */ |
0675fa3d65fa
(SUNOS4 or __FreeBSD__): Define UNDO_RELOCATION.
Richard M. Stallman <rms@gnu.org>
parents:
8865
diff
changeset
|
264 #endif |
8859
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
265 |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
266 for (; rel < erel; rel += rel_size) |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
267 { |
8865
3fe65c32616b
(unexec): Fix logic for rounding section boundaries.
Richard M. Stallman <rms@gnu.org>
parents:
8859
diff
changeset
|
268 /* This is the unadjusted address from the reloc. */ |
3fe65c32616b
(unexec): Fix logic for rounding section boundaries.
Richard M. Stallman <rms@gnu.org>
parents:
8859
diff
changeset
|
269 unsigned long pos = *(unsigned long *)(taddr + rel); |
3fe65c32616b
(unexec): Fix logic for rounding section boundaries.
Richard M. Stallman <rms@gnu.org>
parents:
8859
diff
changeset
|
270 /* This is the amount by which to adjust it. It |
3fe65c32616b
(unexec): Fix logic for rounding section boundaries.
Richard M. Stallman <rms@gnu.org>
parents:
8859
diff
changeset
|
271 depends on which segment the address belongs to. */ |
3fe65c32616b
(unexec): Fix logic for rounding section boundaries.
Richard M. Stallman <rms@gnu.org>
parents:
8859
diff
changeset
|
272 unsigned long offset = (pos < (unsigned long)&data_start |
3fe65c32616b
(unexec): Fix logic for rounding section boundaries.
Richard M. Stallman <rms@gnu.org>
parents:
8859
diff
changeset
|
273 ? taddr : daddr); |
3fe65c32616b
(unexec): Fix logic for rounding section boundaries.
Richard M. Stallman <rms@gnu.org>
parents:
8859
diff
changeset
|
274 /* This is the adjusted address from the reloc. */ |
3fe65c32616b
(unexec): Fix logic for rounding section boundaries.
Richard M. Stallman <rms@gnu.org>
parents:
8859
diff
changeset
|
275 unsigned long rpos = pos - offset; |
8859
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
276 |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
277 lseek (new, N_TXTOFF (nhdr) + rpos, L_SET); |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
278 write (new, old + N_TXTOFF (ohdr) + rpos, sizeof (unsigned long)); |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
279 } |
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
280 } |
8952
0675fa3d65fa
(SUNOS4 or __FreeBSD__): Define UNDO_RELOCATION.
Richard M. Stallman <rms@gnu.org>
parents:
8865
diff
changeset
|
281 #endif /* UNDO_RELOCATION */ |
8859
678a41575de0
[SUNOS4]: Include link.h.
Richard M. Stallman <rms@gnu.org>
parents:
6865
diff
changeset
|
282 |
486 | 283 fchmod (new, 0755); |
284 } | |
285 | |
286 void | |
287 run_time_remap (progname) | |
288 char *progname; | |
289 { | |
290 char aout[MAXPATHLEN]; | |
291 register char *path, *p; | |
292 | |
293 /* Just in case */ | |
294 if (!initialized) | |
295 return; | |
296 | |
297 /* Restore the break */ | |
298 brk (Brk); | |
299 | |
300 /* If nothing to remap: we are done! */ | |
301 if (rd_only_len == 0) | |
302 return; | |
303 | |
304 /* | |
305 * Attempt to find the executable | |
306 * First try argv[0], will almost always succeed as shells tend to give | |
307 * the full path from the hash list rather than using execvp () | |
308 */ | |
309 if (is_it (progname)) | |
310 return; | |
311 | |
312 /* | |
313 * If argv[0] is a full path and does not exist, not much sense in | |
314 * searching further | |
315 */ | |
316 if (strchr (progname, '/')) | |
317 return; | |
318 | |
319 /* | |
320 * Try to search for argv[0] on the PATH | |
321 */ | |
322 path = getenv ("PATH"); | |
323 if (path == NULL) | |
324 return; | |
325 | |
326 while (*path) | |
327 { | |
328 /* copy through ':' or end */ | |
329 for (p = aout; *p = *path; ++p, ++path) | |
330 if (*p == ':') | |
331 { | |
332 ++path; /* move past ':' */ | |
333 break; | |
334 } | |
335 *p++ = '/'; | |
336 strcpy (p, progname); | |
337 /* | |
338 * aout is a candidate full path name | |
339 */ | |
340 if (is_it (aout)) | |
341 return; | |
342 } | |
343 } | |
344 | |
345 is_it (path) | |
346 char *path; | |
347 { | |
348 int fd; | |
349 long paths_cookie; | |
350 struct exec hdr; | |
351 | |
352 /* | |
353 * Open an executable and check for a valid header! | |
354 * Can't bcmp() the header with what we had, it may have been stripped! | |
355 * so we may save looking at non executables with the same name, mostly | |
356 * directories. | |
357 */ | |
358 fd = open (path, O_RDONLY); | |
359 if (fd != -1) | |
360 { | |
361 if (read (fd, &hdr, sizeof (hdr)) == sizeof (hdr) | |
362 && !N_BADMAG (hdr) && N_DATOFF (hdr) == N_DATOFF (nhdr) | |
363 && N_TRELOFF (hdr) == N_TRELOFF (nhdr)) | |
364 { | |
365 /* compare cookies */ | |
366 lseek (fd, N_DATOFF (hdr) + (int)&cookie - N_DATADDR (hdr), L_SET); | |
367 read (fd, &paths_cookie, sizeof (paths_cookie)); | |
368 if (paths_cookie == cookie) | |
369 { /* Eureka */ | |
370 | |
371 /* | |
372 * Do the mapping | |
373 * The PROT_EXEC may not be needed, but it is safer this way. | |
374 * should the shared library decide to indirect through | |
375 * addresses in the data segment not part of __DYNAMIC | |
376 */ | |
377 mmap (data_start, rd_only_len, PROT_READ | PROT_EXEC, | |
5216
e094a62cadcd
[! MAP_FILE] (MAP_FILE): Define to zero.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
378 MAP_FILE | MAP_SHARED | MAP_FIXED, fd, |
486 | 379 N_DATOFF (hdr) + data_start - N_DATADDR (hdr)); |
380 close (fd); | |
381 return 1; | |
382 } | |
383 } | |
384 close (fd); | |
385 } | |
386 return 0; | |
387 } |