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