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