428
|
1 /* Code to do an unexec for FreeBSD-1.1 for a temacs linked -Bdynamic.
|
|
2 Derived from unexnetbsd.c, which was derived from unexsunos4.c
|
|
3 Copyright (C) 1992, 1993 Free Software Foundation, Inc.
|
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
|
11
|
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to
|
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, USA. */
|
|
21
|
|
22 /* Synched up with: Not in FSF? */
|
|
23
|
|
24 /*
|
|
25 Created 29-Oct-92 by Harlan Sexton
|
|
26 Tweaked 06-Aug-93 by Dean Michaels to work with sun3.
|
|
27 Converted 01-Dec-93 by Paul Mackerras to work with NetBSD shared libraries.
|
|
28 Tweaked 26-Feb-94 by Shawn Carey for use with FreeBSD-1.1 shared libraries.
|
|
29 */
|
|
30
|
|
31 /********************** Included .h Files **************************/
|
|
32
|
|
33 #include <config.h>
|
|
34
|
|
35 #include <stdarg.h>
|
|
36 #include <sys/param.h>
|
|
37 #include <sys/mman.h>
|
|
38 #include <sys/file.h>
|
|
39 #include <sys/stat.h>
|
|
40 #include <sys/types.h>
|
|
41 #include <string.h>
|
|
42 #include <stdio.h>
|
438
|
43 #include <errno.h>
|
428
|
44 #include <a.out.h>
|
|
45 #include <unistd.h>
|
|
46 #include <ctype.h>
|
|
47 #include <stab.h>
|
|
48 #include <sys/dir.h>
|
|
49 #include <link.h>
|
2286
|
50 #include "compiler.h"
|
428
|
51
|
|
52 /********************** Macros *************************************/
|
|
53
|
|
54 #define SYS_ERR strerror(errno)
|
|
55
|
|
56 #define MASK_UP(x,p_of_two) \
|
|
57 ((((unsigned long) (x)) + ((p_of_two) - 1)) & (~((p_of_two) - 1)))
|
|
58
|
|
59 #define MASK_DOWN(x,p_of_two) (((unsigned long) (x)) & (~((p_of_two) - 1)))
|
|
60
|
|
61 /********************** Typedefs and Structs ***********************/
|
|
62
|
|
63 struct translation_struct
|
|
64 {
|
|
65 long txtaddr;
|
|
66 long txtoff;
|
|
67 long dataddr;
|
|
68 long datoff;
|
|
69 long bssaddr;
|
|
70 long endaddr;
|
|
71 };
|
|
72
|
|
73 /********************** Function Prototypes/Declarations ***********/
|
|
74
|
442
|
75 static void unexec_error (const char *m, int use_errno, ...);
|
428
|
76 static int unexec_open (char *filename, int flag, int mode);
|
|
77 static caddr_t unexec_mmap (int fd, size_t len, int prot, int flags);
|
|
78 static long unexec_seek (int fd, long position);
|
|
79 static void unexec_read (int fd, long position, char *buf, int bytes);
|
|
80 static void unexec_write (int fd, long position, char *buf, int bytes);
|
|
81 static void unexec_pad (int fd, int bytes);
|
|
82 static void unexec_fstat (int fd, struct stat *statptr);
|
|
83 static void unexec_fchmod (int fd, int mode);
|
|
84 static long unexec_addr_to_offset (long addr, struct translation_struct *ts);
|
|
85 static void copy_relocation_site (struct relocation_info *ri,
|
|
86 caddr_t from_base_addr,
|
|
87 caddr_t to_base_addr,
|
|
88 struct translation_struct *ts);
|
|
89 static void reset_symtab (struct nlist *start, struct nlist *end,
|
|
90 char *strtab, long edata_value, long end_value,
|
|
91 int shlib_image);
|
|
92 static void reset_ldso_symtab (struct nzlist *start, struct nzlist *end,
|
|
93 char *strtab, long edata_value, long end_value,
|
|
94 int shlib_image);
|
|
95 int run_time_remap (char *dummy);
|
|
96
|
|
97 /********************** Variables **********************************/
|
|
98
|
|
99 /* for reporting error messages from system calls */
|
|
100 extern int _DYNAMIC;
|
|
101 extern char **environ;
|
|
102
|
|
103 static unsigned long sbrk_of_0_at_unexec;
|
|
104
|
|
105 /*******************************************************************/
|
|
106
|
|
107 static void
|
442
|
108 unexec_error (const char *fmt, int use_errno, ...)
|
428
|
109 {
|
442
|
110 const char *err_msg = SYS_ERR;
|
428
|
111 va_list args;
|
|
112
|
|
113 fprintf (stderr, "unexec - ");
|
|
114 va_start (args, use_errno);
|
|
115 vfprintf (stderr, fmt, args);
|
|
116 va_end (args);
|
|
117
|
|
118 if (use_errno)
|
|
119 fprintf (stderr, ": %s", err_msg);
|
|
120 fprintf (stderr, "\n");
|
|
121 exit (1);
|
|
122 return;
|
|
123 }
|
|
124
|
|
125 static int
|
|
126 unexec_open (char *filename, int flag, int mode)
|
|
127 {
|
|
128 int fd;
|
|
129
|
|
130 errno = 0;
|
|
131
|
|
132 fd = open (filename, flag, mode);
|
|
133
|
|
134 if (fd < 0)
|
|
135 unexec_error ("Failure opening file %s", 1, filename);
|
|
136 return fd;
|
|
137 }
|
|
138
|
|
139 static caddr_t
|
|
140 unexec_mmap (int fd, size_t len, int prot, int flags)
|
|
141 {
|
|
142 caddr_t return_val;
|
|
143
|
|
144 unexec_seek (fd, 0);
|
|
145 errno = 0;
|
|
146 return_val = mmap (0, len, prot, flags, fd, 0);
|
|
147
|
|
148 if (return_val == (caddr_t) -1)
|
|
149 unexec_error ("Failure mmap'ing file", 1);
|
|
150 return return_val;
|
|
151 }
|
|
152
|
|
153
|
|
154 static long
|
|
155 unexec_seek (int fd, long position)
|
|
156 {
|
|
157 long seek_value;
|
|
158
|
|
159 if (fd <= 0)
|
|
160 unexec_error ("No file open in which to seek", 0);
|
|
161
|
|
162 errno = 0;
|
|
163
|
|
164 if (position < 0)
|
|
165 seek_value = (long) lseek (fd, 0, L_INCR);
|
|
166 else
|
|
167 seek_value = (long) lseek (fd, position, L_SET);
|
|
168
|
|
169 if (seek_value < 0)
|
|
170 unexec_error ("Failed to do a seek to 0x%x in %s", 1,
|
|
171 position, "unexec() output file");
|
|
172
|
|
173 return seek_value;
|
|
174 }
|
|
175
|
|
176 static void
|
|
177 unexec_read (int fd, long position, char *buf, int bytes)
|
|
178 {
|
|
179 int n_read;
|
|
180 int remains = bytes;
|
|
181 position = unexec_seek (fd, position);
|
|
182
|
|
183 if (bytes < 0)
|
|
184 unexec_error ("Attempted read of %d bytes", 0, bytes);
|
|
185
|
|
186 errno = 0;
|
|
187
|
|
188 while (remains > 0)
|
|
189 {
|
|
190 n_read = read (fd, buf, remains);
|
|
191 if (n_read <= 0)
|
|
192 unexec_error ("Read failed for 0x%x bytes at offset 0x%x in %s",
|
|
193 1, bytes, position, "unexec() output file");
|
|
194 buf += n_read;
|
|
195 remains -= n_read;
|
|
196 }
|
|
197
|
|
198 return;
|
|
199 }
|
|
200
|
|
201 static void
|
|
202 unexec_write (int fd, long position, char *buf, int bytes)
|
|
203 {
|
|
204 int n_written;
|
|
205 int remains = bytes;
|
|
206 position = unexec_seek (fd, position);
|
|
207
|
|
208 if (bytes < 0)
|
|
209 unexec_error ("Attempted write of %d bytes in %s",
|
|
210 0, bytes, "unexec() output file");
|
|
211
|
|
212 errno = 0;
|
|
213
|
|
214 while (remains > 0)
|
|
215 {
|
|
216 n_written = write (fd, buf, remains);
|
|
217 if (n_written <= 0)
|
|
218 unexec_error ("Write failed for 0x%x bytes at offset 0x%x in %s",
|
|
219 1, bytes, position, "unexec() output file");
|
|
220 buf += n_written;
|
|
221 remains -= n_written;
|
|
222 }
|
|
223
|
|
224 return;
|
|
225 }
|
|
226
|
|
227 static void
|
|
228 unexec_pad (int fd, int bytes)
|
|
229 {
|
|
230 if (bytes > 0)
|
|
231 {
|
|
232 char buf[1024];
|
|
233 int remaining = bytes;
|
|
234
|
|
235 memset (buf, 0, sizeof (buf));
|
|
236
|
|
237 while (remaining > 0)
|
|
238 {
|
|
239 int this_write = (remaining > sizeof(buf))?sizeof(buf):remaining;
|
|
240 unexec_write (fd, -1, buf, this_write);
|
|
241 remaining -= this_write;
|
|
242 }
|
|
243 }
|
|
244 }
|
|
245
|
|
246 static void
|
|
247 unexec_fstat (int fd, struct stat *statptr)
|
|
248 {
|
|
249 errno = 0;
|
|
250 if (-1 == fstat (fd, statptr))
|
|
251 unexec_error ("fstat() failed for descriptor %d", 1, fd);
|
|
252 return;
|
|
253 }
|
|
254
|
|
255 static void
|
|
256 unexec_fchmod (int fd, int mode)
|
|
257 {
|
|
258 errno = 0;
|
|
259 if (-1 == fchmod (fd, mode))
|
|
260 unexec_error ("fchmod() failed for descriptor %d", 1, fd);
|
|
261 return;
|
|
262 }
|
|
263
|
|
264 static long
|
|
265 unexec_addr_to_offset (long addr, struct translation_struct *ts)
|
|
266
|
|
267 {
|
|
268 if ((addr < ts->txtaddr) || (addr >= ts->bssaddr))
|
|
269 return -1;
|
|
270 else if (addr >= ts->dataddr)
|
|
271 return ((long) ((addr - ts->dataddr) + ts->datoff));
|
|
272 else
|
|
273 return ((long) ((addr - ts->txtaddr) + ts->txtoff));
|
|
274 }
|
|
275
|
|
276
|
|
277 /*
|
|
278 * "LD.SO" DATA AND SYMBOL TABLE OPERATIONS
|
|
279 */
|
|
280
|
|
281 static void
|
|
282 copy_relocation_site (struct relocation_info *ri,
|
|
283 caddr_t from_base_addr,
|
|
284 caddr_t to_base_addr,
|
|
285 struct translation_struct *ts)
|
|
286 {
|
|
287 long offset;
|
|
288 caddr_t from, to;
|
|
289
|
|
290 /* We can get relocation sites in the bss region, for objects whose
|
|
291 contents are copied from a shared library. We don't need or want
|
|
292 to restore these at present. */
|
|
293 #ifndef sparc
|
|
294 if (ri->r_copy)
|
|
295 return;
|
|
296 #else
|
|
297 /* Struct relocation_info_sparc doesn't have member r_copy.
|
|
298 Instead, we use the address to check if this is run-time-copied. */
|
|
299 if (ri->r_address >= ts->bssaddr && ri->r_address < ts->endaddr)
|
|
300 return;
|
|
301 #endif
|
|
302
|
|
303 offset = unexec_addr_to_offset (ri->r_address, ts);
|
|
304 if (offset == -1)
|
|
305 unexec_error ("bad relocation address 0x%x (0x%x)", 0, ri->r_address,
|
|
306 ((long *)ri)[1]);
|
|
307
|
|
308 from = from_base_addr + offset;
|
|
309 to = to_base_addr + offset;
|
|
310 /* This stuff should be in a md_ file somewhere... */
|
|
311 #ifndef sparc
|
|
312 switch (ri->r_length)
|
|
313 {
|
|
314 case 0:
|
|
315 *((char *) to) = *((char *) from);
|
|
316 break;
|
|
317 case 1:
|
|
318 *((short *) to) = *((short *) from);
|
|
319 break;
|
|
320 case 2:
|
|
321 *((long *) to) = *((long *) from);
|
|
322 break;
|
|
323 default:
|
|
324 unexec_error ("unknown reloc length %d seen during unexec()",
|
|
325 0, ri->r_length);
|
|
326 break;
|
|
327 }
|
|
328 #else /* sparc */
|
|
329 switch (ri->r_type)
|
|
330 {
|
|
331 case RELOC_8:
|
|
332 case RELOC_DISP8:
|
|
333 *((char *) to) = *((char *) from);
|
|
334 break;
|
|
335 case RELOC_16:
|
|
336 case RELOC_DISP16:
|
|
337 *((short *) to) = *((short *) from);
|
|
338 break;
|
|
339 case RELOC_LO10:
|
|
340 case RELOC_13:
|
|
341 case RELOC_22:
|
|
342 case RELOC_HI22:
|
|
343 case RELOC_WDISP22:
|
|
344 case RELOC_WDISP30:
|
|
345 case RELOC_32:
|
|
346 case RELOC_DISP32:
|
|
347 case RELOC_GLOB_DAT:
|
|
348 *((long *) to) = *((long *) from);
|
|
349 break;
|
|
350 case RELOC_JMP_SLOT:
|
|
351 {
|
|
352 long *target = (long *) to;
|
|
353 long *source = (long *) from;
|
|
354 *target = *source;
|
|
355 target++;
|
|
356 source++;
|
|
357 *target = *source;
|
|
358 target++;
|
|
359 source++;
|
|
360 *target = *source;
|
|
361 }
|
|
362 break;
|
|
363 default:
|
|
364 unexec_error ("unknown reloc type %d seen during unexec()",
|
|
365 0, ri->r_type);
|
|
366 break;
|
|
367 }
|
|
368 #endif /* sparc */
|
|
369 }
|
|
370
|
|
371 static void
|
|
372 reset_symtab (struct nlist *start, struct nlist *end, char *strtab,
|
|
373 long edata_value, long end_value, int shlib_image)
|
|
374 {
|
|
375 struct nlist *tmp = start;
|
|
376 int found_edata = 0;
|
|
377 int found_end = 0;
|
|
378
|
|
379 while (tmp < end)
|
|
380 {
|
|
381 int type = tmp->n_type;
|
|
382
|
|
383 if ((type == (N_UNDF | N_EXT)) &&
|
|
384 (tmp->n_value != 0))
|
|
385 unexec_error ("unexec'ing image has COMMON symbols in it -- we quit!",
|
|
386 0);
|
|
387
|
|
388 if (!(type & N_STAB))
|
|
389 {
|
|
390 if (!found_edata &&
|
|
391 (type == (N_EXT | N_DATA)) &&
|
|
392 tmp->n_un.n_strx &&
|
|
393 !strcmp ("_edata", strtab + tmp->n_un.n_strx))
|
|
394 {
|
|
395 tmp->n_value = edata_value;
|
|
396 found_edata = 1;
|
|
397 }
|
|
398
|
|
399
|
|
400 if ((type & N_TYPE) == N_BSS)
|
|
401 {
|
|
402 if (!found_end &&
|
|
403 (type == (N_EXT | N_BSS)) &&
|
|
404 tmp->n_un.n_strx &&
|
|
405 !strcmp ("_end", strtab + tmp->n_un.n_strx))
|
|
406 {
|
|
407 tmp->n_value = end_value;
|
|
408 found_end = 1;
|
|
409 }
|
|
410 else if (type & N_EXT)
|
|
411 tmp->n_type = N_DATA | N_EXT;
|
|
412 else
|
|
413 tmp->n_type = N_DATA;
|
|
414 }
|
|
415
|
|
416 /* the way things are being handled here, having sbrk() in the
|
|
417 image is fatal for an image linked with shared lib's (although
|
|
418 the code could be modified to support it), but this should
|
|
419 never happen anyway */
|
|
420 if (shlib_image &&
|
|
421 (type == (N_EXT | N_TEXT)) &&
|
|
422 tmp->n_un.n_strx &&
|
|
423 !strcmp ("_sbrk", strtab + tmp->n_un.n_strx))
|
|
424 unexec_error ("unexec'd shlib image has sbrk() in it -- we quit!",
|
|
425 0);
|
|
426 }
|
|
427
|
|
428 tmp++;
|
|
429 }
|
|
430 }
|
|
431
|
|
432 static void
|
|
433 reset_ldso_symtab (struct nzlist *start, struct nzlist *end, char *strtab,
|
|
434 long edata_value, long end_value, int shlib_image)
|
|
435 {
|
|
436 struct nzlist *tmp = start;
|
|
437 int found_edata = 0;
|
|
438 int found_end = 0;
|
|
439
|
|
440 while (tmp < end) {
|
|
441 int type = tmp->nz_type;
|
|
442 /*
|
|
443 * the following code breaks under FreeBSD-1.1-BETA, but everything
|
|
444 * seems to work perfectly if it's commented out. This did not break
|
|
445 * anything until the changes to ld.so were made.
|
|
446 */
|
|
447 /*
|
|
448 if ((type == (N_UNDF | N_EXT)) && (tmp->nz_value != 0))
|
|
449 unexec_error("unexec'ing image has COMMON symbols in rel -- we quit!",0);
|
|
450 */
|
|
451 if (!(type & N_STAB)) {
|
|
452 if (!found_edata &&
|
|
453 (type == (N_EXT | N_DATA)) &&
|
|
454 !strcmp ("_edata", strtab + tmp->nz_strx)) {
|
|
455 tmp->nz_value = edata_value;
|
|
456 found_edata = 1;
|
|
457 }
|
|
458
|
|
459 if ((type & N_TYPE) == N_BSS) {
|
|
460 if (!found_end &&
|
|
461 (type == (N_EXT | N_BSS)) &&
|
|
462 !strcmp ("_end", strtab + tmp->nz_strx)) {
|
|
463 tmp->nz_value = end_value;
|
|
464 found_end = 1;
|
|
465 } else if (type & N_EXT)
|
|
466 tmp->nz_type = N_DATA | N_EXT;
|
|
467 else
|
|
468 tmp->nz_type = N_DATA;
|
|
469 }
|
|
470
|
|
471 /* the way things are being handled here, having sbrk() in the
|
|
472 image is fatal for an image linked with shared lib's (although
|
|
473 the code could be modified to support it), but this should
|
|
474 never happen anyway */
|
|
475 if (shlib_image &&
|
|
476 (type == (N_EXT | N_TEXT)) &&
|
|
477 !strcmp ("_sbrk", strtab + tmp->nz_strx))
|
|
478 unexec_error("unexec'd shlib image has sbrk() ref -- we quit!", 0);
|
|
479 }
|
|
480 tmp++;
|
|
481 }
|
|
482 }
|
|
483
|
|
484 extern int getpagesize (void);
|
|
485
|
|
486 /*
|
|
487 * EXPORTED FUNCTIONS
|
|
488 */
|
|
489
|
|
490 /* this has to be a global variable to prevent the optimizers from
|
|
491 * assuming that it can not be 0.
|
|
492 */
|
|
493 static void *dynamic_addr = (void *) &_DYNAMIC;
|
|
494
|
|
495 int
|
2286
|
496 unexec (char *new_name, char *old_name, unsigned int UNUSED (emacs_edata),
|
|
497 unsigned int UNUSED (dummy1), unsigned int UNUSED (dummy2))
|
428
|
498 {
|
|
499 /* ld.so data */
|
|
500 struct _dynamic *ld = 0;
|
|
501 struct section_dispatch_table *ld2 = 0;
|
|
502 /* old and new state */
|
|
503 int old_fd;
|
|
504 int new_fd;
|
|
505 caddr_t old_base_addr;
|
|
506 caddr_t new_base_addr;
|
|
507 struct exec old_hdr;
|
|
508 struct exec new_hdr;
|
|
509 struct stat old_buf;
|
|
510 struct stat new_buf;
|
|
511 /* some process specific "constants" */
|
|
512 unsigned long n_pagsiz, new_edata;
|
|
513 long page_size = getpagesize ();
|
|
514 caddr_t plt_end;
|
|
515 caddr_t current_break = (caddr_t) sbrk (0);
|
|
516
|
|
517 if (!page_size)
|
|
518 unexec_error ("unexec() failed because we can't get the size of a page!",
|
|
519 0);
|
|
520
|
|
521 /* see if this is a -Bdynamic image -- if so, find ld.so structures */
|
|
522 if (dynamic_addr)
|
|
523 {
|
|
524 ld = (struct _dynamic *) dynamic_addr;
|
|
525 ld2 = ld->d_un.d_sdt;
|
|
526 if (ld->d_version < LD_VERSION_BSD)
|
|
527 unexec_error ("%s linked with obsolete version of ld -- we quit!",
|
|
528 0, old_name);
|
|
529 }
|
|
530
|
|
531 /* open the old and new files, figuring out how big the old one is
|
|
532 so that we can map it in */
|
|
533 old_fd = unexec_open (old_name, O_RDONLY, 0);
|
|
534 new_fd = unexec_open (new_name, O_RDWR | O_CREAT | O_TRUNC, 0666);
|
|
535
|
|
536 /* setup the header and the statbuf for old_fd */
|
|
537 unexec_read (old_fd, 0, (char *) &old_hdr, sizeof (old_hdr));
|
|
538 unexec_fstat (old_fd, &old_buf);
|
|
539
|
|
540
|
|
541 /* set up some important constants */
|
|
542 n_pagsiz = __LDPGSZ;
|
|
543 if (dynamic_addr)
|
|
544 plt_end = (caddr_t) MASK_UP (ld2->sdt_plt + ld2->sdt_plt_sz, sizeof (double));
|
|
545 else
|
|
546 plt_end = (caddr_t) N_DATADDR (old_hdr);
|
|
547
|
|
548 #if 0
|
|
549 /* never write protect the variable "environ", defined in /lib/crt0.o, and
|
853
|
550 set in process.c */
|
428
|
551 mprotect_bottom_addr = ((unsigned long) &environ) + sizeof (char **);
|
|
552 /* never protect ABOVE the end of data emacs_edata specified */
|
|
553 mprotect_top_addr = MIN (emacs_edata, N_DATADDR (old_hdr) + old_hdr.a_data);
|
|
554 #endif
|
|
555
|
|
556 /* Set up the image of the old file */
|
|
557 old_base_addr = unexec_mmap (old_fd, old_buf.st_size, PROT_READ,
|
|
558 MAP_FILE | MAP_PRIVATE);
|
|
559 close (old_fd);
|
|
560
|
|
561 /* set up the new exec */
|
|
562 new_hdr = old_hdr;
|
|
563 new_edata = (unsigned long) MASK_UP (current_break, n_pagsiz);
|
|
564 new_hdr.a_data = new_edata - ((unsigned long) N_DATADDR (old_hdr));
|
|
565 new_hdr.a_bss = 0;
|
|
566
|
|
567 /* set up this variable, in case we want to reset "the break"
|
|
568 when restarting */
|
|
569 sbrk_of_0_at_unexec = ((unsigned long) MASK_UP (current_break, n_pagsiz));
|
|
570
|
|
571 /* Write out the first approximation to the new file. The sizes of
|
|
572 each section will be correct, but there will be a number of
|
|
573 corrections that will need to be made. */
|
|
574 {
|
|
575 long old_datoff = N_DATOFF (old_hdr);
|
|
576 long old_dataddr = N_DATADDR (old_hdr);
|
|
577 long new_treloff = N_RELOFF (new_hdr);
|
|
578 long old_treloff = N_RELOFF (old_hdr);
|
|
579 long ld_so_size = ((unsigned long) plt_end) - old_dataddr;
|
|
580 long real_data_size = current_break - plt_end;
|
|
581 long pad_size =
|
|
582 MASK_UP (current_break, n_pagsiz) - ((unsigned long) current_break);
|
|
583
|
|
584
|
|
585 /* First, write the text segment with new header -- copy everything until
|
|
586 the start of the data segment from the old file, and then go back and
|
|
587 write the new header. */
|
|
588 unexec_write (new_fd, 0, old_base_addr, old_datoff + ld_so_size);
|
|
589 unexec_write (new_fd, 0, (char *) &new_hdr, sizeof (new_hdr));
|
|
590
|
|
591 /* Copy the rest of the data segment from the running image. */
|
|
592 unexec_write (new_fd, old_datoff + ld_so_size,
|
|
593 plt_end, real_data_size);
|
|
594
|
|
595 /* pad out the data segment */
|
|
596 unexec_pad (new_fd, pad_size);
|
|
597
|
|
598 /* Finally, copy the symbol table information from the old file. */
|
|
599 unexec_write (new_fd, new_treloff,
|
|
600 old_base_addr + old_treloff,
|
|
601 old_buf.st_size - old_treloff);
|
|
602 }
|
|
603
|
|
604
|
|
605 /* Next, map in the output file so that we can jump around fixing it
|
|
606 up. We retain the old file so that we can refer to it. */
|
|
607 unexec_fstat (new_fd, &new_buf);
|
|
608 new_base_addr = unexec_mmap (new_fd,
|
|
609 MASK_UP (new_buf.st_size, page_size),
|
|
610 PROT_READ | PROT_WRITE,
|
|
611 MAP_FILE | MAP_SHARED);
|
|
612
|
|
613
|
|
614
|
|
615 /* We need to do 2 things. First, make sure that _edata and _end (and
|
|
616 hence, curbrk) are set to the correct values. At the same time, for
|
|
617 neatness and to help with debugging, mark all the types of all ld.so
|
|
618 and nm BSS symbols in the new file to be DATA, and make sure that
|
|
619 there are no COMMON symbols in the output file, as any references to
|
|
620 these can lose really big. Second, reset all of the ld.so "relocation
|
|
621 sites" in the new file to have the values that appear in the old file
|
|
622 -- the failure to do this was the biggest loser in the old version of
|
|
623 this code. */
|
|
624
|
|
625 /* STEP 1 */
|
|
626 /* Reset the regular symbol table first. */
|
|
627 reset_symtab ((struct nlist *) (new_base_addr + N_SYMOFF(new_hdr)),
|
|
628 (struct nlist *) (new_base_addr + N_SYMOFF(new_hdr) +
|
|
629 new_hdr.a_syms),
|
|
630 (char *) (new_base_addr + N_STROFF(new_hdr)),
|
|
631 new_edata, new_edata,
|
|
632 !!dynamic_addr);
|
|
633
|
|
634 /* Now reset the ld.so symbol table. */
|
|
635 if (dynamic_addr)
|
|
636 {
|
|
637 struct translation_struct ts;
|
|
638 struct relocation_info *tmp, *end;
|
|
639 caddr_t syms, strings;
|
|
640
|
|
641 /* set up the structure that we use to translate addresses in the
|
|
642 old file into file offsets */
|
|
643 ts.txtaddr = N_TXTADDR (old_hdr);
|
|
644 ts.txtoff = N_TXTOFF (old_hdr);
|
|
645 ts.dataddr = N_DATADDR (old_hdr);
|
|
646 ts.datoff = N_DATOFF (old_hdr);
|
|
647 ts.bssaddr = N_DATADDR (old_hdr) + old_hdr.a_data;
|
|
648 ts.endaddr = ts.bssaddr + old_hdr.a_bss;
|
|
649
|
|
650 syms = new_base_addr + unexec_addr_to_offset(ld2->sdt_nzlist, &ts);
|
|
651 strings = new_base_addr + unexec_addr_to_offset(ld2->sdt_strings, &ts);
|
|
652 reset_ldso_symtab ((struct nzlist *) syms, (struct nzlist *) strings,
|
|
653 (char *) strings,
|
|
654 new_edata, new_edata,
|
|
655 !!dynamic_addr);
|
|
656
|
|
657 /* STEP 2 */
|
|
658 tmp = (struct relocation_info *)
|
|
659 (old_base_addr + unexec_addr_to_offset(ld2->sdt_rel, &ts));
|
|
660 end = (struct relocation_info *)
|
|
661 (old_base_addr + unexec_addr_to_offset(ld2->sdt_hash, &ts));
|
|
662 while (tmp < end)
|
|
663 {
|
|
664 copy_relocation_site (tmp, old_base_addr, new_base_addr, &ts);
|
|
665 tmp++;
|
|
666 }
|
|
667 }
|
|
668
|
|
669 /* get rid of the mmap-ed file space and make the output file
|
|
670 executable -- then quit */
|
|
671 munmap (new_base_addr, MASK_UP (new_buf.st_size, page_size));
|
|
672 munmap (old_base_addr, MASK_UP (old_buf.st_size, page_size));
|
|
673 unexec_fchmod (new_fd, 0755);
|
|
674 close (new_fd);
|
|
675 return 0;
|
|
676 }
|
|
677
|
|
678
|
|
679 int
|
2286
|
680 run_time_remap (char *UNUSED (dummy))
|
428
|
681 {
|
|
682 unsigned long current_sbrk = (unsigned long) sbrk (0);
|
|
683
|
|
684 #if __FreeBSD_version < 300000 /* 2.x can work with this code */
|
|
685 if (sbrk_of_0_at_unexec < current_sbrk)
|
|
686 {
|
|
687 if (sbrk_of_0_at_unexec != 0)
|
|
688 fprintf (stderr, "Absurd new brk addr = %lx (current = %lx)\n",
|
|
689 sbrk_of_0_at_unexec, current_sbrk);
|
|
690 }
|
|
691 else
|
|
692 #endif
|
|
693 if (sbrk_of_0_at_unexec > current_sbrk)
|
|
694 {
|
|
695 errno = 0;
|
|
696 if (brk ((caddr_t) sbrk_of_0_at_unexec))
|
|
697 fprintf (stderr, "failed to change brk addr to %lx: %s\n",
|
|
698 sbrk_of_0_at_unexec, SYS_ERR);
|
|
699 }
|
|
700
|
|
701 #if 0
|
|
702 /* with proper COW, i don't think we really need to do this... */
|
|
703 {
|
|
704 long page_size = getpagesize();
|
|
705 unsigned long base_addr = MASK_UP (mprotect_bottom_addr, page_size);
|
|
706 unsigned long top_addr = MASK_DOWN (mprotect_top_addr, page_size);
|
|
707 long len = top_addr - base_addr;
|
|
708
|
|
709 if (len > 0)
|
|
710 {
|
|
711 errno = 0;
|
|
712 if (mprotect ((caddr_t) base_addr, len, PROT_READ | PROT_EXEC))
|
|
713 fprintf (stderr, "failed to change protection on data pages: %s\n",
|
|
714 SYS_ERR);
|
|
715 }
|
|
716 }
|
|
717 #endif
|
|
718
|
|
719 return 0;
|
|
720 }
|