2 * Code to do an unexec for HPUX 8.0 on an HP9000/[34]00 for a
3 * dynamically linked temacs.
5 Copyright (C) 1992-1993 Free Software Foundation, Inc.
7 This file is part of XEmacs.
9 XEmacs is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 2, or (at your option) any
14 XEmacs is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with XEmacs; see the file COPYING. If not, write to
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
24 /* Synched up with: Not in FSF. */
27 Created 29-Oct-92 by Harlan Sexton for SunOS
29 Modified Jan 93 by Hamish Macdonald for HPUX
32 /********************** Included .h Files **************************/
37 #include <sys/param.h>
40 #include <sys/types.h>
46 # include </usr/include/debug.h>
55 /* XEmacs: Richard Cognot <cognot@ensg.u-nancy.fr> says we need these */
56 extern void perror(const char*);
58 extern char *sys_errlist[];
59 extern char *strerror (int);
62 /********************** Macros *************************************/
65 ((errno > 0)?((errno < sys_nerr)?(sys_errlist[errno]):\
66 "unknown system error"): "unknown error")
68 #define MASK_UP(x,p_of_two) \
69 ((((unsigned long) (x)) + ((p_of_two) - 1)) & (~((p_of_two) - 1)))
71 #define MASK_DOWN(x,p_of_two) (((unsigned long) (x)) & (~((p_of_two) - 1)))
73 /********************** Function Prototypes/Declarations ***********/
75 static void unexec_error (const char *fmt, int use_errno, ...);
76 static int unexec_open (char *filename, int flag, int mode);
77 static long unexec_seek (int fd, long position);
78 static void unexec_read (int fd, long position, char *buf, int bytes);
79 static void unexec_write (int fd, long position, char *buf, int bytes);
80 static void unexec_copy (int new_fd, int old_fd, long old_pos, long new_pos,
82 static void unexec_pad (int fd, int bytes);
83 static void unexec_fstat (int fd, struct stat *statptr);
84 static void unexec_fchmod (int fd, int mode);
85 int run_time_remap (char *dummy);
87 /********************** Variables **********************************/
89 /* for reporting error messages from system calls */
92 extern char **environ;
94 static unsigned long sbrk_of_0_at_unexec;
96 /*******************************************************************/
99 unexec_error (const char *fmt, int use_errno, ...)
101 const char *err_msg = SYS_ERR;
104 fprintf (stderr, "unexec - ");
105 va_start (args, use_errno);
106 vfprintf (stderr, fmt, args);
110 fprintf (stderr, ": %s", err_msg);
111 fprintf (stderr, "\n");
117 unexec_open (char *filename, int flag, int mode)
123 fd = open (filename, flag, mode);
127 unexec_error ("Failure opening file %s", 1, (void *) filename, 0, 0);
135 unexec_seek (int fd, long position)
140 unexec_error ("No file open in which to seek", 0, 0, 0, 0);
145 seek_value = (long) lseek (fd, 0, L_INCR);
147 seek_value = (long) lseek (fd, position, L_SET);
150 unexec_error ("Failed to do a seek to 0x%x in %s", 1,
151 (char *) position, "unexec() output file", 0);
157 unexec_read (int fd, long position, char *buf, int bytes)
161 position = unexec_seek (fd, position);
164 unexec_error ("Attempted read of %d bytes", 0, (char *) bytes, 0, 0);
170 n_read = read (fd, buf, remains);
172 unexec_error ("Read failed for 0x%x bytes at offset 0x%x in %s",
173 1, (char *) bytes, (char *) position,
174 "unexec() output file");
183 unexec_write (int fd, long position, char *buf, int bytes)
187 position = unexec_seek (fd, position);
190 unexec_error ("Attempted write of %d bytes in %s",
191 0, (char *) bytes, "unexec() output file", 0);
197 n_written = write (fd, buf, remains);
199 unexec_error ("Write failed for 0x%x bytes at offset 0x%x in %s",
200 1, (char *) bytes, (char *) position,
201 "unexec() output file");
203 remains -= n_written;
210 unexec_copy (int new_fd, int old_fd, long old_pos, long new_pos, int bytes)
217 int n_to_copy = remains > sizeof(buf) ? sizeof(buf) : remains;
219 unexec_read (old_fd, old_pos, buf, n_to_copy);
220 unexec_write (new_fd, new_pos, buf, n_to_copy);
222 old_pos += n_to_copy;
223 new_pos += n_to_copy;
224 remains -= n_to_copy;
231 unexec_pad (int fd, int bytes)
236 int remaining = bytes;
238 memset (buf, 0, sizeof(buf));
240 while (remaining > 0)
242 int this_write = (remaining > sizeof(buf))?sizeof(buf):remaining;
243 unexec_write (fd, -1, buf, this_write);
244 remaining -= this_write;
250 unexec_fstat (int fd, struct stat *statptr)
253 if (-1 == fstat (fd, statptr))
254 unexec_error ("fstat() failed for descriptor %d", 1, (char *) fd, 0, 0);
259 unexec_fchmod (int fd, int mode)
262 if (-1 == fchmod (fd, mode))
263 unexec_error ("fchmod() failed for descriptor %d", 1, (char *) fd, 0, 0);
271 /* this has to be a global variable to prevent the optimizers from
272 * assuming that it can not be 0.
274 static void *dynamic_addr = (void *) &_DYNAMIC;
277 unexec (char *new_name, char *old_name,
278 unsigned int emacs_edata, unsigned int dummy1, unsigned int dummy2)
281 struct dynamic *ld = 0;
282 /* old and new state */
288 /* some process specific "constants" */
289 unsigned long n_pagsiz;
291 caddr_t current_break = (caddr_t) sbrk (0);
293 /* dynamically linked image? -- if so, find dld.sl structures */
296 ld = (struct dynamic *) dynamic_addr;
298 printf ("dl_text = %#x\n", ld->text);
299 printf ("dl_data = %#x\n", ld->data);
300 printf ("dl_bss = %#x\n", ld->bss);
301 printf ("dl_end = %#x\n", ld->end);
302 printf ("dl_dmodule = %#x\n", ld->dmodule);
303 printf ("dl_dlt = %#x\n", ld->dlt);
304 printf ("dl_plt = %#x\n", ld->plt);
308 /* open the old and new files, figuring out how big the old one is
309 so that we can map it in */
310 old_fd = unexec_open (old_name, O_RDONLY, 0);
311 new_fd = unexec_open (new_name, O_RDWR | O_CREAT | O_TRUNC, 0666);
313 /* setup the header and the statbuf for old_fd */
314 unexec_read (old_fd, 0, (char *) &old_hdr, sizeof (old_hdr));
315 unexec_fstat (old_fd, &old_buf);
317 /* set up some important constants */
318 n_pagsiz = EXEC_PAGESIZE;
320 /* setup beginning of data to copy from executable */
322 dynamic_beg = ld->dmodule;
324 dynamic_beg = (caddr_t)EXEC_ALIGN (old_hdr.a_text) + old_hdr.a_data;
326 /* set up the new exec */
328 new_hdr.a_text = MASK_DOWN (emacs_edata, n_pagsiz);
329 new_hdr.a_data = MASK_UP (current_break, n_pagsiz)
330 - EXEC_ALIGN(new_hdr.a_text);
334 printf ("old text %#x\n", old_hdr.a_text);
335 printf ("new text %#x\n", new_hdr.a_text);
336 printf ("old data %#x\n", old_hdr.a_data);
337 printf ("new data %#x\n", new_hdr.a_data);
338 printf ("old bss %#x\n", old_hdr.a_bss);
339 printf ("new bss %#x\n", new_hdr.a_bss);
342 /* set up this variable, in case we want to reset "the break"
344 sbrk_of_0_at_unexec = ((unsigned long) MASK_UP (current_break, n_pagsiz));
346 /* Write out the first approximation to the new file. The sizes of
347 each section will be correct, but there will be a number of
348 corrections that will need to be made. */
350 long old_datoff = DATA_OFFSET (old_hdr);
351 long new_datoff = DATA_OFFSET (new_hdr);
352 long old_dataddr = EXEC_ALIGN (old_hdr.a_text);
353 long new_dataddr = EXEC_ALIGN (new_hdr.a_text);
354 long new_mcaloff = MODCAL_OFFSET (new_hdr);
355 long old_mcaloff = MODCAL_OFFSET (old_hdr);
356 long newtext_size = new_hdr.a_text - old_dataddr;
357 long newdata1_size = (unsigned long)dynamic_beg - new_dataddr;
358 long dyn_size = (EXEC_ALIGN (old_hdr.a_text) + old_hdr.a_data)
359 - (unsigned long)dynamic_beg;
360 long newdata2_size = (unsigned long)current_break
361 - ((unsigned long)dynamic_beg + dyn_size);
363 MASK_UP (current_break, n_pagsiz) - ((unsigned long) current_break);
366 printf ("current break is %#lx\n", current_break);
368 printf ("old_dataddr = %#lx, dynamic_beg = %#lx\n",
369 old_dataddr, dynamic_beg);
373 * First, write the text segment with new header -- copy
374 * everything until the start of the data segment from the old
378 printf ("copying %#lx bytes of text from 0\n", old_datoff);
380 unexec_copy (new_fd, old_fd, 0, 0, old_datoff);
381 /* pad out the text segment */
383 printf ( "text pad size is %#x\n", old_dataddr - old_hdr.a_text);
385 unexec_pad (new_fd, old_dataddr - old_hdr.a_text);
388 * Update debug header spoo
390 if (new_hdr.a_extension > 0)
392 new_hdr.a_extension += LESYM_OFFSET(new_hdr) - LESYM_OFFSET(old_hdr);
396 * go back and write the new header.
398 unexec_write (new_fd, 0, (char *) &new_hdr, sizeof (new_hdr));
402 * Copy the part of the data segment which becomes text from the
406 printf ("copying %#lx bytes of new text from %#lx to position %#lx\n",
407 newtext_size, old_dataddr, TEXT_OFFSET(new_hdr) + old_dataddr);
409 unexec_write (new_fd, TEXT_OFFSET(new_hdr) + old_dataddr,
410 (caddr_t)old_dataddr, newtext_size);
413 printf ("new DATA_OFFSET is %#lx\n", new_datoff);
417 * Copy the part of the old data segment which will be data
418 * in the new executable (before the dynamic stuff)
419 * from the running image.
422 printf ("copying %#lx bytes of data from %#lx to position %#lx\n",
423 newdata1_size, new_dataddr, new_datoff);
425 unexec_write (new_fd, new_datoff, (caddr_t)new_dataddr, newdata1_size);
427 /* copy the dynamic part of the data segment from the old executable */
431 printf ("copying %#lx bytes of dyn data from executable"
432 " at address %#lx to position %#lx\n",
433 dyn_size, dynamic_beg, new_datoff + newdata1_size);
435 unexec_copy (new_fd, old_fd, old_datoff + newtext_size + newdata1_size,
436 new_datoff + newdata1_size, dyn_size);
439 /* copy remaining data (old bss) from the running image */
441 printf ("copying %#lx bytes of data from %#lx to position %#lx\n",
442 newdata2_size, new_dataddr + newdata1_size + dyn_size,
443 new_datoff + newdata1_size + dyn_size);
445 unexec_write (new_fd, new_datoff + newdata1_size + dyn_size,
446 (caddr_t)(new_dataddr + newdata1_size + dyn_size),
449 /* pad out the data segment */
451 printf ( "pad size is %#x\n", pad_size);
453 unexec_pad (new_fd, pad_size);
455 /* Finally, copy the rest of the junk from the old file. */
457 printf ("Copying %#lx bytes of junk from %#lx (old) to %#lx (new)\n",
458 old_buf.st_size - old_mcaloff, old_mcaloff, new_mcaloff);
460 unexec_copy (new_fd, old_fd, old_mcaloff, new_mcaloff,
461 old_buf.st_size - old_mcaloff);
465 struct _debug_header dhdr;
466 int new_header_delta;
468 new_header_delta = LESYM_OFFSET(new_hdr) - LESYM_OFFSET(old_hdr);
469 if ((new_header_delta > 0) &&
470 ((offset = EXT_OFFSET(old_hdr)) > 0))
472 curpos = lseek(new_fd, 0, SEEK_CUR);
473 lseek(old_fd, offset, 0);
474 if (read(old_fd, &dhdr, sizeof(dhdr)) == sizeof(dhdr))
476 dhdr.header_offset += new_header_delta;
477 dhdr.gntt_offset += new_header_delta;
478 dhdr.lntt_offset += new_header_delta;
479 dhdr.slt_offset += new_header_delta;
480 dhdr.vt_offset += new_header_delta;
481 dhdr.xt_offset += new_header_delta;
482 lseek(new_fd, EXT_OFFSET(new_hdr), SEEK_SET);
483 if (write(new_fd, &dhdr, sizeof(dhdr)) != sizeof(dhdr))
485 unexec_error("Unable to write debug information to \"%s\"\n",
488 lseek(new_fd, curpos, SEEK_SET);
492 unexec_error("Unable to read debug information from \"%s\"\n",
500 /* make the output file executable -- then quit */
501 unexec_fchmod (new_fd, 0755);
509 run_time_remap (char *dummy)
511 unsigned long current_sbrk = (unsigned long) sbrk (0);
513 if (sbrk_of_0_at_unexec < current_sbrk)
514 fprintf (stderr, "Absurd new brk addr = 0x%x (current = 0x%x)\n",
515 sbrk_of_0_at_unexec, current_sbrk);
519 if (brk ((caddr_t) sbrk_of_0_at_unexec))
520 fprintf (stderr, "failed to change brk addr to 0x%x: %s\n",
521 sbrk_of_0_at_unexec, SYS_ERR);