annotate src/unexsunos4.c @ 6473:76bb655df959

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