428
+ − 1 /*
+ − 2 * Code to do an unexec for HPUX 8.0 on an HP9000/[34]00 for a
+ − 3 * dynamically linked temacs.
+ − 4
+ − 5 Copyright (C) 1992-1993 Free Software Foundation, Inc.
+ − 6
+ − 7 This file is part of XEmacs.
+ − 8
+ − 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
+ − 12 later version.
+ − 13
+ − 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
+ − 17 for more details.
+ − 18
+ − 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. */
+ − 23
+ − 24 /* Synched up with: Not in FSF. */
+ − 25
+ − 26 /*
+ − 27 Created 29-Oct-92 by Harlan Sexton for SunOS
+ − 28
+ − 29 Modified Jan 93 by Hamish Macdonald for HPUX
+ − 30 */
+ − 31
+ − 32 /********************** Included .h Files **************************/
+ − 33
+ − 34 #include <config.h>
+ − 35
+ − 36 #include <stdarg.h>
+ − 37 #include <sys/param.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>
978
+ − 44 #include "syssignal.h"
428
+ − 45 #ifdef __hp9000s300
+ − 46 # include </usr/include/debug.h>
+ − 47 #endif
+ − 48 #include <a.out.h>
+ − 49 #include <unistd.h>
+ − 50 #include <ctype.h>
+ − 51 #include <sys/dir.h>
+ − 52
+ − 53 #include "sysdep.h"
2286
+ − 54 #include "compiler.h"
428
+ − 55
+ − 56 /* XEmacs: Richard Cognot <cognot@ensg.u-nancy.fr> says we need these */
+ − 57 extern void perror(const char*);
+ − 58 extern int sys_nerr;
+ − 59 extern char *sys_errlist[];
+ − 60 extern char *strerror (int);
+ − 61
+ − 62
+ − 63 /********************** Macros *************************************/
+ − 64
+ − 65 #define SYS_ERR \
+ − 66 ((errno > 0)?((errno < sys_nerr)?(sys_errlist[errno]):\
+ − 67 "unknown system error"): "unknown error")
+ − 68
+ − 69 #define MASK_UP(x,p_of_two) \
+ − 70 ((((unsigned long) (x)) + ((p_of_two) - 1)) & (~((p_of_two) - 1)))
+ − 71
+ − 72 #define MASK_DOWN(x,p_of_two) (((unsigned long) (x)) & (~((p_of_two) - 1)))
+ − 73
+ − 74 /********************** Function Prototypes/Declarations ***********/
+ − 75
442
+ − 76 static void unexec_error (const char *fmt, int use_errno, ...);
428
+ − 77 static int unexec_open (char *filename, int flag, int mode);
+ − 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_copy (int new_fd, int old_fd, long old_pos, long new_pos,
+ − 82 int bytes);
+ − 83 static void unexec_pad (int fd, int bytes);
+ − 84 static void unexec_fstat (int fd, struct stat *statptr);
+ − 85 static void unexec_fchmod (int fd, int mode);
+ − 86 int run_time_remap (char *dummy);
+ − 87
+ − 88 /********************** Variables **********************************/
+ − 89
+ − 90 /* for reporting error messages from system calls */
+ − 91 extern int sys_nerr;
+ − 92 extern int _DYNAMIC;
+ − 93 extern char **environ;
+ − 94
+ − 95 static unsigned long sbrk_of_0_at_unexec;
+ − 96
+ − 97 /*******************************************************************/
+ − 98
+ − 99 static void
442
+ − 100 unexec_error (const char *fmt, int use_errno, ...)
428
+ − 101 {
442
+ − 102 const char *err_msg = SYS_ERR;
428
+ − 103 va_list args;
+ − 104
+ − 105 fprintf (stderr, "unexec - ");
+ − 106 va_start (args, use_errno);
+ − 107 vfprintf (stderr, fmt, args);
+ − 108 va_end (args);
+ − 109
+ − 110 if (use_errno)
+ − 111 fprintf (stderr, ": %s", err_msg);
+ − 112 fprintf (stderr, "\n");
+ − 113 exit (1);
+ − 114 return;
+ − 115 }
+ − 116
+ − 117 static int
+ − 118 unexec_open (char *filename, int flag, int mode)
+ − 119 {
+ − 120 int fd;
+ − 121
+ − 122 errno = 0;
+ − 123
+ − 124 fd = open (filename, flag, mode);
+ − 125
+ − 126 if (fd < 0)
+ − 127 {
+ − 128 unexec_error ("Failure opening file %s", 1, (void *) filename, 0, 0);
+ − 129 return -1;
+ − 130 }
+ − 131 else
+ − 132 return fd;
+ − 133 }
+ − 134
+ − 135 static long
+ − 136 unexec_seek (int fd, long position)
+ − 137 {
+ − 138 long seek_value;
+ − 139
+ − 140 if (fd <= 0)
+ − 141 unexec_error ("No file open in which to seek", 0, 0, 0, 0);
+ − 142
+ − 143 errno = 0;
+ − 144
+ − 145 if (position < 0)
+ − 146 seek_value = (long) lseek (fd, 0, L_INCR);
+ − 147 else
+ − 148 seek_value = (long) lseek (fd, position, L_SET);
+ − 149
+ − 150 if (seek_value < 0)
+ − 151 unexec_error ("Failed to do a seek to 0x%x in %s", 1,
+ − 152 (char *) position, "unexec() output file", 0);
+ − 153
+ − 154 return seek_value;
+ − 155 }
+ − 156
+ − 157 static void
+ − 158 unexec_read (int fd, long position, char *buf, int bytes)
+ − 159 {
+ − 160 int n_read;
+ − 161 int remains = bytes;
+ − 162 position = unexec_seek (fd, position);
+ − 163
+ − 164 if (bytes < 0)
+ − 165 unexec_error ("Attempted read of %d bytes", 0, (char *) bytes, 0, 0);
+ − 166
+ − 167 errno = 0;
+ − 168
+ − 169 while (remains > 0)
+ − 170 {
+ − 171 n_read = read (fd, buf, remains);
+ − 172 if (n_read <= 0)
+ − 173 unexec_error ("Read failed for 0x%x bytes at offset 0x%x in %s",
+ − 174 1, (char *) bytes, (char *) position,
+ − 175 "unexec() output file");
+ − 176 buf += n_read;
+ − 177 remains -= n_read;
+ − 178 }
+ − 179
+ − 180 return;
+ − 181 }
+ − 182
+ − 183 static void
+ − 184 unexec_write (int fd, long position, char *buf, int bytes)
+ − 185 {
+ − 186 int n_written;
+ − 187 int remains = bytes;
+ − 188 position = unexec_seek (fd, position);
+ − 189
+ − 190 if (bytes < 0)
+ − 191 unexec_error ("Attempted write of %d bytes in %s",
+ − 192 0, (char *) bytes, "unexec() output file", 0);
+ − 193
+ − 194 errno = 0;
+ − 195
+ − 196 while (remains > 0)
+ − 197 {
+ − 198 n_written = write (fd, buf, remains);
+ − 199 if (n_written <= 0)
+ − 200 unexec_error ("Write failed for 0x%x bytes at offset 0x%x in %s",
+ − 201 1, (char *) bytes, (char *) position,
+ − 202 "unexec() output file");
+ − 203 buf += n_written;
+ − 204 remains -= n_written;
+ − 205 }
+ − 206
+ − 207 return;
+ − 208 }
+ − 209
+ − 210 static void
+ − 211 unexec_copy (int new_fd, int old_fd, long old_pos, long new_pos, int bytes)
+ − 212 {
+ − 213 int remains = bytes;
+ − 214 char buf[128];
+ − 215
+ − 216 while (remains > 0)
+ − 217 {
+ − 218 int n_to_copy = remains > sizeof(buf) ? sizeof(buf) : remains;
+ − 219
+ − 220 unexec_read (old_fd, old_pos, buf, n_to_copy);
+ − 221 unexec_write (new_fd, new_pos, buf, n_to_copy);
+ − 222
+ − 223 old_pos += n_to_copy;
+ − 224 new_pos += n_to_copy;
+ − 225 remains -= n_to_copy;
+ − 226 }
+ − 227
+ − 228 return;
+ − 229 }
+ − 230
+ − 231 static void
+ − 232 unexec_pad (int fd, int bytes)
+ − 233 {
+ − 234 if (bytes > 0)
+ − 235 {
+ − 236 char buf[1024];
+ − 237 int remaining = bytes;
+ − 238
+ − 239 memset (buf, 0, sizeof(buf));
+ − 240
+ − 241 while (remaining > 0)
+ − 242 {
+ − 243 int this_write = (remaining > sizeof(buf))?sizeof(buf):remaining;
+ − 244 unexec_write (fd, -1, buf, this_write);
+ − 245 remaining -= this_write;
+ − 246 }
+ − 247 }
+ − 248 }
+ − 249
+ − 250 static void
+ − 251 unexec_fstat (int fd, struct stat *statptr)
+ − 252 {
+ − 253 errno = 0;
+ − 254 if (-1 == fstat (fd, statptr))
+ − 255 unexec_error ("fstat() failed for descriptor %d", 1, (char *) fd, 0, 0);
+ − 256 return;
+ − 257 }
+ − 258
+ − 259 static void
+ − 260 unexec_fchmod (int fd, int mode)
+ − 261 {
+ − 262 errno = 0;
+ − 263 if (-1 == fchmod (fd, mode))
+ − 264 unexec_error ("fchmod() failed for descriptor %d", 1, (char *) fd, 0, 0);
+ − 265 return;
+ − 266 }
+ − 267
+ − 268 /*
+ − 269 * EXPORTED FUNCTIONS
+ − 270 */
+ − 271
+ − 272 /* this has to be a global variable to prevent the optimizers from
+ − 273 * assuming that it can not be 0.
+ − 274 */
+ − 275 static void *dynamic_addr = (void *) &_DYNAMIC;
+ − 276
+ − 277 int
2286
+ − 278 unexec (char *new_name, char *old_name, unsigned int emacs_edata,
+ − 279 unsigned int UNUSED (dummy1), unsigned int UNUSED (dummy2))
428
+ − 280 {
+ − 281 /* /dld.sl data */
+ − 282 struct dynamic *ld = 0;
+ − 283 /* old and new state */
+ − 284 int old_fd;
+ − 285 int new_fd;
+ − 286 struct exec old_hdr;
+ − 287 struct exec new_hdr;
+ − 288 struct stat old_buf;
+ − 289 /* some process specific "constants" */
+ − 290 unsigned long n_pagsiz;
+ − 291 caddr_t dynamic_beg;
+ − 292 caddr_t current_break = (caddr_t) sbrk (0);
+ − 293
+ − 294 /* dynamically linked image? -- if so, find dld.sl structures */
+ − 295 if (dynamic_addr)
+ − 296 {
+ − 297 ld = (struct dynamic *) dynamic_addr;
+ − 298 #ifdef DEBUG
+ − 299 printf ("dl_text = %#x\n", ld->text);
+ − 300 printf ("dl_data = %#x\n", ld->data);
+ − 301 printf ("dl_bss = %#x\n", ld->bss);
+ − 302 printf ("dl_end = %#x\n", ld->end);
+ − 303 printf ("dl_dmodule = %#x\n", ld->dmodule);
+ − 304 printf ("dl_dlt = %#x\n", ld->dlt);
+ − 305 printf ("dl_plt = %#x\n", ld->plt);
+ − 306 #endif
+ − 307 }
+ − 308
+ − 309 /* open the old and new files, figuring out how big the old one is
+ − 310 so that we can map it in */
+ − 311 old_fd = unexec_open (old_name, O_RDONLY, 0);
+ − 312 new_fd = unexec_open (new_name, O_RDWR | O_CREAT | O_TRUNC, 0666);
+ − 313
+ − 314 /* setup the header and the statbuf for old_fd */
+ − 315 unexec_read (old_fd, 0, (char *) &old_hdr, sizeof (old_hdr));
+ − 316 unexec_fstat (old_fd, &old_buf);
+ − 317
+ − 318 /* set up some important constants */
+ − 319 n_pagsiz = EXEC_PAGESIZE;
+ − 320
+ − 321 /* setup beginning of data to copy from executable */
+ − 322 if (ld)
+ − 323 dynamic_beg = ld->dmodule;
+ − 324 else
+ − 325 dynamic_beg = (caddr_t)EXEC_ALIGN (old_hdr.a_text) + old_hdr.a_data;
+ − 326
+ − 327 /* set up the new exec */
+ − 328 new_hdr = old_hdr;
+ − 329 new_hdr.a_text = MASK_DOWN (emacs_edata, n_pagsiz);
+ − 330 new_hdr.a_data = MASK_UP (current_break, n_pagsiz)
+ − 331 - EXEC_ALIGN(new_hdr.a_text);
+ − 332 new_hdr.a_bss = 0;
+ − 333
+ − 334 #ifdef DEBUG
+ − 335 printf ("old text %#x\n", old_hdr.a_text);
+ − 336 printf ("new text %#x\n", new_hdr.a_text);
+ − 337 printf ("old data %#x\n", old_hdr.a_data);
+ − 338 printf ("new data %#x\n", new_hdr.a_data);
+ − 339 printf ("old bss %#x\n", old_hdr.a_bss);
+ − 340 printf ("new bss %#x\n", new_hdr.a_bss);
+ − 341 #endif
+ − 342
+ − 343 /* set up this variable, in case we want to reset "the break"
+ − 344 when restarting */
+ − 345 sbrk_of_0_at_unexec = ((unsigned long) MASK_UP (current_break, n_pagsiz));
+ − 346
+ − 347 /* Write out the first approximation to the new file. The sizes of
+ − 348 each section will be correct, but there will be a number of
+ − 349 corrections that will need to be made. */
+ − 350 {
+ − 351 long old_datoff = DATA_OFFSET (old_hdr);
+ − 352 long new_datoff = DATA_OFFSET (new_hdr);
+ − 353 long old_dataddr = EXEC_ALIGN (old_hdr.a_text);
+ − 354 long new_dataddr = EXEC_ALIGN (new_hdr.a_text);
+ − 355 long new_mcaloff = MODCAL_OFFSET (new_hdr);
+ − 356 long old_mcaloff = MODCAL_OFFSET (old_hdr);
+ − 357 long newtext_size = new_hdr.a_text - old_dataddr;
+ − 358 long newdata1_size = (unsigned long)dynamic_beg - new_dataddr;
+ − 359 long dyn_size = (EXEC_ALIGN (old_hdr.a_text) + old_hdr.a_data)
+ − 360 - (unsigned long)dynamic_beg;
+ − 361 long newdata2_size = (unsigned long)current_break
+ − 362 - ((unsigned long)dynamic_beg + dyn_size);
+ − 363 long pad_size =
+ − 364 MASK_UP (current_break, n_pagsiz) - ((unsigned long) current_break);
+ − 365
+ − 366 #ifdef DEBUG
+ − 367 printf ("current break is %#lx\n", current_break);
+ − 368
+ − 369 printf ("old_dataddr = %#lx, dynamic_beg = %#lx\n",
+ − 370 old_dataddr, dynamic_beg);
+ − 371 #endif
+ − 372
+ − 373 /*
+ − 374 * First, write the text segment with new header -- copy
+ − 375 * everything until the start of the data segment from the old
+ − 376 * file
+ − 377 */
+ − 378 #ifdef DEBUG
+ − 379 printf ("copying %#lx bytes of text from 0\n", old_datoff);
+ − 380 #endif
+ − 381 unexec_copy (new_fd, old_fd, 0, 0, old_datoff);
+ − 382 /* pad out the text segment */
+ − 383 #ifdef DEBUG
+ − 384 printf ( "text pad size is %#x\n", old_dataddr - old_hdr.a_text);
+ − 385 #endif
+ − 386 unexec_pad (new_fd, old_dataddr - old_hdr.a_text);
+ − 387
+ − 388 /*
+ − 389 * Update debug header spoo
+ − 390 */
+ − 391 if (new_hdr.a_extension > 0)
+ − 392 {
+ − 393 new_hdr.a_extension += LESYM_OFFSET(new_hdr) - LESYM_OFFSET(old_hdr);
+ − 394 }
+ − 395
+ − 396 /*
+ − 397 * go back and write the new header.
+ − 398 */
+ − 399 unexec_write (new_fd, 0, (char *) &new_hdr, sizeof (new_hdr));
+ − 400
+ − 401
+ − 402 /*
+ − 403 * Copy the part of the data segment which becomes text from the
+ − 404 * running image.
+ − 405 */
+ − 406 #ifdef DEBUG
+ − 407 printf ("copying %#lx bytes of new text from %#lx to position %#lx\n",
+ − 408 newtext_size, old_dataddr, TEXT_OFFSET(new_hdr) + old_dataddr);
+ − 409 #endif
+ − 410 unexec_write (new_fd, TEXT_OFFSET(new_hdr) + old_dataddr,
+ − 411 (caddr_t)old_dataddr, newtext_size);
+ − 412
+ − 413 #ifdef DEBUG
+ − 414 printf ("new DATA_OFFSET is %#lx\n", new_datoff);
+ − 415 #endif
+ − 416
+ − 417 /*
+ − 418 * Copy the part of the old data segment which will be data
+ − 419 * in the new executable (before the dynamic stuff)
+ − 420 * from the running image.
+ − 421 */
+ − 422 #ifdef DEBUG
+ − 423 printf ("copying %#lx bytes of data from %#lx to position %#lx\n",
+ − 424 newdata1_size, new_dataddr, new_datoff);
+ − 425 #endif
+ − 426 unexec_write (new_fd, new_datoff, (caddr_t)new_dataddr, newdata1_size);
+ − 427
+ − 428 /* copy the dynamic part of the data segment from the old executable */
+ − 429 if (dyn_size)
+ − 430 {
+ − 431 #ifdef DEBUG
+ − 432 printf ("copying %#lx bytes of dyn data from executable"
+ − 433 " at address %#lx to position %#lx\n",
+ − 434 dyn_size, dynamic_beg, new_datoff + newdata1_size);
+ − 435 #endif
+ − 436 unexec_copy (new_fd, old_fd, old_datoff + newtext_size + newdata1_size,
+ − 437 new_datoff + newdata1_size, dyn_size);
+ − 438 }
+ − 439
+ − 440 /* copy remaining data (old bss) from the running image */
+ − 441 #ifdef DEBUG
+ − 442 printf ("copying %#lx bytes of data from %#lx to position %#lx\n",
+ − 443 newdata2_size, new_dataddr + newdata1_size + dyn_size,
+ − 444 new_datoff + newdata1_size + dyn_size);
+ − 445 #endif
+ − 446 unexec_write (new_fd, new_datoff + newdata1_size + dyn_size,
+ − 447 (caddr_t)(new_dataddr + newdata1_size + dyn_size),
+ − 448 newdata2_size);
+ − 449
+ − 450 /* pad out the data segment */
+ − 451 #ifdef DEBUG
+ − 452 printf ( "pad size is %#x\n", pad_size);
+ − 453 #endif
+ − 454 unexec_pad (new_fd, pad_size);
+ − 455
+ − 456 /* Finally, copy the rest of the junk from the old file. */
+ − 457 #ifdef DEBUG
+ − 458 printf ("Copying %#lx bytes of junk from %#lx (old) to %#lx (new)\n",
+ − 459 old_buf.st_size - old_mcaloff, old_mcaloff, new_mcaloff);
+ − 460 #endif
+ − 461 unexec_copy (new_fd, old_fd, old_mcaloff, new_mcaloff,
+ − 462 old_buf.st_size - old_mcaloff);
+ − 463
+ − 464 {
+ − 465 long curpos, offset;
+ − 466 struct _debug_header dhdr;
+ − 467 int new_header_delta;
+ − 468
+ − 469 new_header_delta = LESYM_OFFSET(new_hdr) - LESYM_OFFSET(old_hdr);
+ − 470 if ((new_header_delta > 0) &&
+ − 471 ((offset = EXT_OFFSET(old_hdr)) > 0))
+ − 472 {
+ − 473 curpos = lseek(new_fd, 0, SEEK_CUR);
+ − 474 lseek(old_fd, offset, 0);
+ − 475 if (read(old_fd, &dhdr, sizeof(dhdr)) == sizeof(dhdr))
+ − 476 {
+ − 477 dhdr.header_offset += new_header_delta;
+ − 478 dhdr.gntt_offset += new_header_delta;
+ − 479 dhdr.lntt_offset += new_header_delta;
+ − 480 dhdr.slt_offset += new_header_delta;
+ − 481 dhdr.vt_offset += new_header_delta;
+ − 482 dhdr.xt_offset += new_header_delta;
+ − 483 lseek(new_fd, EXT_OFFSET(new_hdr), SEEK_SET);
+ − 484 if (write(new_fd, &dhdr, sizeof(dhdr)) != sizeof(dhdr))
+ − 485 {
+ − 486 unexec_error("Unable to write debug information to \"%s\"\n",
+ − 487 1, new_name);
+ − 488 }
+ − 489 lseek(new_fd, curpos, SEEK_SET);
+ − 490 }
+ − 491 else
+ − 492 {
+ − 493 unexec_error("Unable to read debug information from \"%s\"\n",
+ − 494 1, old_name);
+ − 495 }
+ − 496 }
+ − 497 }
+ − 498 }
+ − 499
+ − 500
+ − 501 /* make the output file executable -- then quit */
+ − 502 unexec_fchmod (new_fd, 0755);
+ − 503 close (old_fd);
+ − 504 close (new_fd);
+ − 505 return 0;
+ − 506 }
+ − 507
+ − 508
+ − 509 int
2286
+ − 510 run_time_remap (char *UNUSED (dummy))
428
+ − 511 {
+ − 512 unsigned long current_sbrk = (unsigned long) sbrk (0);
+ − 513
+ − 514 if (sbrk_of_0_at_unexec < current_sbrk)
+ − 515 fprintf (stderr, "Absurd new brk addr = 0x%x (current = 0x%x)\n",
+ − 516 sbrk_of_0_at_unexec, current_sbrk);
+ − 517 else
+ − 518 {
+ − 519 errno = 0;
+ − 520 if (brk ((caddr_t) sbrk_of_0_at_unexec))
+ − 521 fprintf (stderr, "failed to change brk addr to 0x%x: %s\n",
+ − 522 sbrk_of_0_at_unexec, SYS_ERR);
+ − 523 }
+ − 524
+ − 525 return 0;
+ − 526 }