Lines 1-4
Link Here
|
1 |
/* Copyright (C) 1985, 1986, 1987, 1988, 1990, 1992 |
1 |
/* Copyright (C) 1985, 1986, 1987, 1988, 1990, 1992, 1999, 2000, 01, 02 |
2 |
Free Software Foundation, Inc. |
2 |
Free Software Foundation, Inc. |
3 |
|
3 |
|
4 |
This file is part of GNU Emacs. |
4 |
This file is part of GNU Emacs. |
Lines 33-46
Link Here
|
33 |
* Modified heavily since then. |
33 |
* Modified heavily since then. |
34 |
* |
34 |
* |
35 |
* Synopsis: |
35 |
* Synopsis: |
36 |
* unexec (new_name, a_name, data_start, bss_start, entry_address) |
36 |
* unexec (new_name, old_name, data_start, bss_start, entry_address) |
37 |
* char *new_name, *a_name; |
37 |
* char *new_name, *old_name; |
38 |
* unsigned data_start, bss_start, entry_address; |
38 |
* unsigned data_start, bss_start, entry_address; |
39 |
* |
39 |
* |
40 |
* Takes a snapshot of the program and makes an a.out format file in the |
40 |
* Takes a snapshot of the program and makes an a.out format file in the |
41 |
* file named by the string argument new_name. |
41 |
* file named by the string argument new_name. |
42 |
* If a_name is non-NULL, the symbol table will be taken from the given file. |
42 |
* If old_name is non-NULL, the symbol table will be taken from the given file. |
43 |
* On some machines, an existing a_name file is required. |
43 |
* On some machines, an existing old_name file is required. |
44 |
* |
44 |
* |
45 |
* The boundaries within the a.out file may be adjusted with the data_start |
45 |
* The boundaries within the a.out file may be adjusted with the data_start |
46 |
* and bss_start arguments. Either or both may be given as 0 for defaults. |
46 |
* and bss_start arguments. Either or both may be given as 0 for defaults. |
Lines 52-62
Link Here
|
52 |
* The value you specify may be rounded down to a suitable boundary |
52 |
* The value you specify may be rounded down to a suitable boundary |
53 |
* as required by the machine you are using. |
53 |
* as required by the machine you are using. |
54 |
* |
54 |
* |
55 |
* Specifying zero for data_start means the boundary between text and data |
|
|
56 |
* should not be the same as when the program was loaded. |
57 |
* If NO_REMAP is defined, the argument data_start is ignored and the |
58 |
* segment boundaries are never changed. |
59 |
* |
60 |
* Bss_start indicates how much of the data segment is to be saved in the |
55 |
* Bss_start indicates how much of the data segment is to be saved in the |
61 |
* a.out file and restored when the program is executed. It gives the lowest |
56 |
* a.out file and restored when the program is executed. It gives the lowest |
62 |
* unsaved address, and is rounded up to a page boundary. The default when 0 |
57 |
* unsaved address, and is rounded up to a page boundary. The default when 0 |
Lines 66-74
Link Here
|
66 |
* |
61 |
* |
67 |
* The new file is set up to start at entry_address. |
62 |
* The new file is set up to start at entry_address. |
68 |
* |
63 |
* |
69 |
* If you make improvements I'd like to get them too. |
|
|
70 |
* harpo!utah-cs!thomas, thomas@Utah-20 |
71 |
* |
72 |
*/ |
64 |
*/ |
73 |
|
65 |
|
74 |
/* Even more heavily modified by james@bigtex.cactus.org of Dell Computer Co. |
66 |
/* Even more heavily modified by james@bigtex.cactus.org of Dell Computer Co. |
Lines 412-433
Link Here
|
412 |
|
404 |
|
413 |
*/ |
405 |
*/ |
414 |
|
406 |
|
|
|
407 |
/* |
408 |
* Modified by rdh@yottayotta.com of Yotta Yotta Incorporated. |
409 |
* |
410 |
* The code originally used mmap() to create a memory image of the new |
411 |
* and old object files. This had a few handy features: (1) you get |
412 |
* to use a cool system call like mmap, (2) no need to explicitly |
413 |
* write out the new file before the close, and (3) no swap space |
414 |
* requirements. Unfortunately, mmap() often fails to work with |
415 |
* nfs-mounted file systems. |
416 |
* |
417 |
* So, instead of relying on the vm subsystem to do the file i/o for |
418 |
* us, it's now done explicitly. A buffer of the right size for the |
419 |
* file is dynamically allocated, and either the old_name is read into |
420 |
* it, or it is initialized with the correct new executable contents, |
421 |
* and then written to new_name. |
422 |
*/ |
423 |
|
424 |
#ifndef emacs |
425 |
#define fatal(a, b, c) fprintf (stderr, a, b, c), exit (1) |
426 |
#include <string.h> |
427 |
#else |
428 |
#include "config.h" |
429 |
extern void fatal (char *, ...); |
430 |
#endif |
431 |
|
415 |
#include <sys/types.h> |
432 |
#include <sys/types.h> |
416 |
#include <stdio.h> |
433 |
#include <stdio.h> |
417 |
#include <sys/stat.h> |
434 |
#include <sys/stat.h> |
418 |
#include <memory.h> |
435 |
#include <memory.h> |
419 |
#include <string.h> |
|
|
420 |
#include <errno.h> |
436 |
#include <errno.h> |
421 |
#include <unistd.h> |
437 |
#include <unistd.h> |
422 |
#include <fcntl.h> |
438 |
#include <fcntl.h> |
|
|
439 |
#if !defined (__NetBSD__) && !defined (__OpenBSD__) |
423 |
#include <elf.h> |
440 |
#include <elf.h> |
|
|
441 |
#endif |
424 |
#include <sys/mman.h> |
442 |
#include <sys/mman.h> |
425 |
|
443 |
#if defined (__sony_news) && defined (_SYSTYPE_SYSV) |
426 |
#ifndef emacs |
444 |
#include <sys/elf_mips.h> |
427 |
#define fatal(a, b, c) fprintf (stderr, a, b, c), exit (1) |
445 |
#include <sym.h> |
|
|
446 |
#endif /* __sony_news && _SYSTYPE_SYSV */ |
447 |
#if __sgi |
448 |
#include <syms.h> /* for HDRR declaration */ |
449 |
#endif /* __sgi */ |
450 |
|
451 |
#ifndef MAP_ANON |
452 |
#ifdef MAP_ANONYMOUS |
453 |
#define MAP_ANON MAP_ANONYMOUS |
428 |
#else |
454 |
#else |
429 |
#include "config.h" |
455 |
#define MAP_ANON 0 |
430 |
extern void fatal (char *, ...); |
456 |
#endif |
|
|
457 |
#endif |
458 |
|
459 |
#ifndef MAP_FAILED |
460 |
#define MAP_FAILED ((void *) -1) |
461 |
#endif |
462 |
|
463 |
#if defined (__alpha__) && !defined (__NetBSD__) && !defined (__OpenBSD__) |
464 |
/* Declare COFF debugging symbol table. This used to be in |
465 |
/usr/include/sym.h, but this file is no longer included in Red Hat |
466 |
5.0 and presumably in any other glibc 2.x based distribution. */ |
467 |
typedef struct { |
468 |
short magic; |
469 |
short vstamp; |
470 |
int ilineMax; |
471 |
int idnMax; |
472 |
int ipdMax; |
473 |
int isymMax; |
474 |
int ioptMax; |
475 |
int iauxMax; |
476 |
int issMax; |
477 |
int issExtMax; |
478 |
int ifdMax; |
479 |
int crfd; |
480 |
int iextMax; |
481 |
long cbLine; |
482 |
long cbLineOffset; |
483 |
long cbDnOffset; |
484 |
long cbPdOffset; |
485 |
long cbSymOffset; |
486 |
long cbOptOffset; |
487 |
long cbAuxOffset; |
488 |
long cbSsOffset; |
489 |
long cbSsExtOffset; |
490 |
long cbFdOffset; |
491 |
long cbRfdOffset; |
492 |
long cbExtOffset; |
493 |
} HDRR, *pHDRR; |
494 |
#define cbHDRR sizeof(HDRR) |
495 |
#define hdrNil ((pHDRR)0) |
496 |
#endif |
497 |
|
498 |
#ifdef __NetBSD__ |
499 |
/* |
500 |
* NetBSD does not have normal-looking user-land ELF support. |
501 |
*/ |
502 |
# if defined __alpha__ || defined __sparc_v9__ |
503 |
# define ELFSIZE 64 |
504 |
# else |
505 |
# define ELFSIZE 32 |
506 |
# endif |
507 |
# include <sys/exec_elf.h> |
508 |
|
509 |
# ifndef PT_LOAD |
510 |
# define PT_LOAD Elf_pt_load |
511 |
# if 0 /* was in pkgsrc patches for 20.7 */ |
512 |
# define SHT_PROGBITS Elf_sht_progbits |
513 |
# endif |
514 |
# define SHT_SYMTAB Elf_sht_symtab |
515 |
# define SHT_DYNSYM Elf_sht_dynsym |
516 |
# define SHT_NULL Elf_sht_null |
517 |
# define SHT_NOBITS Elf_sht_nobits |
518 |
# define SHT_REL Elf_sht_rel |
519 |
# define SHT_RELA Elf_sht_rela |
520 |
|
521 |
# define SHN_UNDEF Elf_eshn_undefined |
522 |
# define SHN_ABS Elf_eshn_absolute |
523 |
# define SHN_COMMON Elf_eshn_common |
524 |
# endif /* !PT_LOAD */ |
525 |
|
526 |
# ifdef __alpha__ |
527 |
# include <sys/exec_ecoff.h> |
528 |
# define HDRR struct ecoff_symhdr |
529 |
# define pHDRR HDRR * |
530 |
# endif /* __alpha__ */ |
531 |
|
532 |
#ifdef __mips__ /* was in pkgsrc patches for 20.7 */ |
533 |
# define SHT_MIPS_DEBUG DT_MIPS_FLAGS |
534 |
# define HDRR struct Elf_Shdr |
535 |
#endif /* __mips__ */ |
536 |
#endif /* __NetBSD__ */ |
537 |
|
538 |
#ifdef __OpenBSD__ |
539 |
# include <sys/exec_elf.h> |
540 |
#endif |
541 |
|
542 |
#if __GNU_LIBRARY__ - 0 >= 6 |
543 |
# include <link.h> /* get ElfW etc */ |
544 |
#endif |
545 |
|
546 |
#ifndef ElfW |
547 |
# ifdef __STDC__ |
548 |
# define ElfBitsW(bits, type) Elf##bits##_##type |
549 |
# else |
550 |
# define ElfBitsW(bits, type) Elf/**/bits/**/_/**/type |
551 |
# endif |
552 |
# ifdef _LP64 |
553 |
# define ELFSIZE 64 |
554 |
# else |
555 |
# define ELFSIZE 32 |
556 |
# endif |
557 |
/* This macro expands `bits' before invoking ElfBitsW. */ |
558 |
# define ElfExpandBitsW(bits, type) ElfBitsW (bits, type) |
559 |
# define ElfW(type) ElfExpandBitsW (ELFSIZE, type) |
431 |
#endif |
560 |
#endif |
432 |
|
561 |
|
433 |
#ifndef ELF_BSS_SECTION_NAME |
562 |
#ifndef ELF_BSS_SECTION_NAME |
Lines 462-474
Link Here
|
462 |
*/ |
591 |
*/ |
463 |
|
592 |
|
464 |
#define OLD_SECTION_H(n) \ |
593 |
#define OLD_SECTION_H(n) \ |
465 |
(*(Elf32_Shdr *) ((byte *) old_section_h + old_file_h->e_shentsize * (n))) |
594 |
(*(ElfW(Shdr) *) ((byte *) old_section_h + old_file_h->e_shentsize * (n))) |
466 |
#define NEW_SECTION_H(n) \ |
595 |
#define NEW_SECTION_H(n) \ |
467 |
(*(Elf32_Shdr *) ((byte *) new_section_h + new_file_h->e_shentsize * (n))) |
596 |
(*(ElfW(Shdr) *) ((byte *) new_section_h + new_file_h->e_shentsize * (n))) |
468 |
#define OLD_PROGRAM_H(n) \ |
597 |
#define OLD_PROGRAM_H(n) \ |
469 |
(*(Elf32_Phdr *) ((byte *) old_program_h + old_file_h->e_phentsize * (n))) |
598 |
(*(ElfW(Phdr) *) ((byte *) old_program_h + old_file_h->e_phentsize * (n))) |
470 |
#define NEW_PROGRAM_H(n) \ |
599 |
#define NEW_PROGRAM_H(n) \ |
471 |
(*(Elf32_Phdr *) ((byte *) new_program_h + new_file_h->e_phentsize * (n))) |
600 |
(*(ElfW(Phdr) *) ((byte *) new_program_h + new_file_h->e_phentsize * (n))) |
472 |
|
601 |
|
473 |
#define PATCH_INDEX(n) \ |
602 |
#define PATCH_INDEX(n) \ |
474 |
do { \ |
603 |
do { \ |
Lines 478-486
Link Here
|
478 |
|
607 |
|
479 |
/* Round X up to a multiple of Y. */ |
608 |
/* Round X up to a multiple of Y. */ |
480 |
|
609 |
|
481 |
int |
610 |
static ElfW(Addr) |
482 |
round_up (x, y) |
611 |
round_up (x, y) |
483 |
int x, y; |
612 |
ElfW(Addr) x, y; |
484 |
{ |
613 |
{ |
485 |
int rem = x % y; |
614 |
int rem = x % y; |
486 |
if (rem == 0) |
615 |
if (rem == 0) |
Lines 488-493
Link Here
|
488 |
return x - rem + y; |
617 |
return x - rem + y; |
489 |
} |
618 |
} |
490 |
|
619 |
|
|
|
620 |
/* Return the index of the section named NAME. |
621 |
SECTION_NAMES, FILE_NAME and FILE_H give information |
622 |
about the file we are looking in. |
623 |
|
624 |
If we don't find the section NAME, that is a fatal error |
625 |
if NOERROR is 0; we return -1 if NOERROR is nonzero. */ |
626 |
|
627 |
static int |
628 |
find_section (name, section_names, file_name, old_file_h, old_section_h, noerror) |
629 |
char *name; |
630 |
char *section_names; |
631 |
char *file_name; |
632 |
ElfW(Ehdr) *old_file_h; |
633 |
ElfW(Shdr) *old_section_h; |
634 |
int noerror; |
635 |
{ |
636 |
int idx; |
637 |
|
638 |
for (idx = 1; idx < old_file_h->e_shnum; idx++) |
639 |
{ |
640 |
#ifdef DEBUG |
641 |
fprintf (stderr, "Looking for %s - found %s\n", name, |
642 |
section_names + OLD_SECTION_H (idx).sh_name); |
643 |
#endif |
644 |
if (!strcmp (section_names + OLD_SECTION_H (idx).sh_name, |
645 |
name)) |
646 |
break; |
647 |
} |
648 |
if (idx == old_file_h->e_shnum) |
649 |
{ |
650 |
if (noerror) |
651 |
return -1; |
652 |
else |
653 |
fatal ("Can't find %s in %s.\n", name, file_name); |
654 |
} |
655 |
|
656 |
return idx; |
657 |
} |
658 |
|
491 |
/* **************************************************************** |
659 |
/* **************************************************************** |
492 |
* unexec |
660 |
* unexec |
493 |
* |
661 |
* |
Lines 507-531
Link Here
|
507 |
/* Pointers to the base of the image of the two files. */ |
675 |
/* Pointers to the base of the image of the two files. */ |
508 |
caddr_t old_base, new_base; |
676 |
caddr_t old_base, new_base; |
509 |
|
677 |
|
|
|
678 |
#if MAP_ANON == 0 |
679 |
int mmap_fd; |
680 |
#else |
681 |
# define mmap_fd -1 |
682 |
#endif |
683 |
|
510 |
/* Pointers to the file, program and section headers for the old and new |
684 |
/* Pointers to the file, program and section headers for the old and new |
511 |
* files. |
685 |
* files. |
512 |
*/ |
686 |
*/ |
513 |
Elf32_Ehdr *old_file_h, *new_file_h; |
687 |
ElfW(Ehdr) *old_file_h, *new_file_h; |
514 |
Elf32_Phdr *old_program_h, *new_program_h; |
688 |
ElfW(Phdr) *old_program_h, *new_program_h; |
515 |
Elf32_Shdr *old_section_h, *new_section_h; |
689 |
ElfW(Shdr) *old_section_h, *new_section_h; |
516 |
|
690 |
|
517 |
/* Point to the section name table in the old file */ |
691 |
/* Point to the section name table in the old file */ |
518 |
char *old_section_names; |
692 |
char *old_section_names; |
519 |
|
693 |
|
520 |
Elf32_Addr old_bss_addr, new_bss_addr; |
694 |
ElfW(Addr) old_bss_addr, new_bss_addr; |
521 |
Elf32_Word old_bss_size, new_data2_size; |
695 |
ElfW(Word) old_bss_size, new_data2_size; |
522 |
Elf32_Off new_data2_offset; |
696 |
ElfW(Off) new_data2_offset; |
523 |
Elf32_Addr new_data2_addr; |
697 |
ElfW(Addr) new_data2_addr; |
524 |
|
698 |
|
525 |
int n, nn, old_bss_index, old_data_index, new_data2_index; |
699 |
int n, nn; |
|
|
700 |
int old_bss_index, old_sbss_index; |
701 |
int old_data_index, new_data2_index; |
702 |
int old_mdebug_index; |
526 |
struct stat stat_buf; |
703 |
struct stat stat_buf; |
|
|
704 |
int old_file_size; |
527 |
|
705 |
|
528 |
/* Open the old file & map it into the address space. */ |
706 |
/* Open the old file, allocate a buffer of the right size, and read |
|
|
707 |
* in the file contents. */ |
529 |
|
708 |
|
530 |
old_file = open (old_name, O_RDONLY); |
709 |
old_file = open (old_name, O_RDONLY); |
531 |
|
710 |
|
Lines 535-586
Link Here
|
535 |
if (fstat (old_file, &stat_buf) == -1) |
714 |
if (fstat (old_file, &stat_buf) == -1) |
536 |
fatal ("Can't fstat (%s): errno %d\n", old_name, errno); |
715 |
fatal ("Can't fstat (%s): errno %d\n", old_name, errno); |
537 |
|
716 |
|
538 |
old_base = mmap (0, stat_buf.st_size, PROT_READ, MAP_SHARED, old_file, 0); |
717 |
#if MAP_ANON == 0 |
|
|
718 |
mmap_fd = open ("/dev/zero", O_RDONLY); |
719 |
if (mmap_fd < 0) |
720 |
fatal ("Can't open /dev/zero for reading: errno %d\n", errno); |
721 |
#endif |
539 |
|
722 |
|
540 |
if (old_base == (caddr_t) -1) |
723 |
/* We cannot use malloc here because that may use sbrk. If it does, |
541 |
fatal ("Can't mmap (%s): errno %d\n", old_name, errno); |
724 |
we'd dump our temporary buffers with Emacs, and we'd have to be |
|
|
725 |
extra careful to use the correct value of sbrk(0) after |
726 |
allocating all buffers in the code below, which we aren't. */ |
727 |
old_file_size = stat_buf.st_size; |
728 |
old_base = mmap (NULL, old_file_size, PROT_READ | PROT_WRITE, |
729 |
MAP_ANON | MAP_PRIVATE, mmap_fd, 0); |
730 |
if (old_base == MAP_FAILED) |
731 |
fatal ("Can't allocate buffer for %s\n", old_name); |
542 |
|
732 |
|
543 |
#ifdef DEBUG |
733 |
if (read (old_file, old_base, stat_buf.st_size) != stat_buf.st_size) |
544 |
fprintf (stderr, "mmap (%s, %x) -> %x\n", old_name, stat_buf.st_size, |
734 |
fatal ("Didn't read all of %s: errno %d\n", old_name, errno); |
545 |
old_base); |
|
|
546 |
#endif |
547 |
|
735 |
|
548 |
/* Get pointers to headers & section names */ |
736 |
/* Get pointers to headers & section names */ |
549 |
|
737 |
|
550 |
old_file_h = (Elf32_Ehdr *) old_base; |
738 |
old_file_h = (ElfW(Ehdr) *) old_base; |
551 |
old_program_h = (Elf32_Phdr *) ((byte *) old_base + old_file_h->e_phoff); |
739 |
old_program_h = (ElfW(Phdr) *) ((byte *) old_base + old_file_h->e_phoff); |
552 |
old_section_h = (Elf32_Shdr *) ((byte *) old_base + old_file_h->e_shoff); |
740 |
old_section_h = (ElfW(Shdr) *) ((byte *) old_base + old_file_h->e_shoff); |
553 |
old_section_names = (char *) old_base |
741 |
old_section_names = (char *) old_base |
554 |
+ OLD_SECTION_H (old_file_h->e_shstrndx).sh_offset; |
742 |
+ OLD_SECTION_H (old_file_h->e_shstrndx).sh_offset; |
555 |
|
743 |
|
|
|
744 |
/* Find the mdebug section, if any. */ |
745 |
|
746 |
old_mdebug_index = find_section (".mdebug", old_section_names, |
747 |
old_name, old_file_h, old_section_h, 1); |
748 |
|
556 |
/* Find the old .bss section. Figure out parameters of the new |
749 |
/* Find the old .bss section. Figure out parameters of the new |
557 |
* data2 and bss sections. |
750 |
* data2 and bss sections. |
558 |
*/ |
751 |
*/ |
559 |
|
752 |
|
560 |
for (old_bss_index = 1; old_bss_index < (int) old_file_h->e_shnum; |
753 |
old_bss_index = find_section (".bss", old_section_names, |
561 |
old_bss_index++) |
754 |
old_name, old_file_h, old_section_h, 0); |
|
|
755 |
|
756 |
old_sbss_index = find_section (".sbss", old_section_names, |
757 |
old_name, old_file_h, old_section_h, 1); |
758 |
if (old_sbss_index != -1) |
759 |
if (OLD_SECTION_H (old_sbss_index).sh_type == SHT_PROGBITS) |
760 |
old_sbss_index = -1; |
761 |
|
762 |
if (old_sbss_index == -1) |
562 |
{ |
763 |
{ |
563 |
#ifdef DEBUG |
764 |
old_bss_addr = OLD_SECTION_H (old_bss_index).sh_addr; |
564 |
fprintf (stderr, "Looking for .bss - found %s\n", |
765 |
old_bss_size = OLD_SECTION_H (old_bss_index).sh_size; |
565 |
old_section_names + OLD_SECTION_H (old_bss_index).sh_name); |
766 |
new_data2_index = old_bss_index; |
566 |
#endif |
767 |
} |
567 |
if (!strcmp (old_section_names + OLD_SECTION_H (old_bss_index).sh_name, |
768 |
else |
568 |
ELF_BSS_SECTION_NAME)) |
769 |
{ |
569 |
break; |
770 |
old_bss_addr = OLD_SECTION_H (old_sbss_index).sh_addr; |
|
|
771 |
old_bss_size = OLD_SECTION_H (old_bss_index).sh_size |
772 |
+ OLD_SECTION_H (old_sbss_index).sh_size; |
773 |
new_data2_index = old_sbss_index; |
570 |
} |
774 |
} |
571 |
if (old_bss_index == old_file_h->e_shnum) |
|
|
572 |
fatal ("Can't find .bss in %s.\n", old_name, 0); |
573 |
|
775 |
|
574 |
old_bss_addr = OLD_SECTION_H (old_bss_index).sh_addr; |
776 |
/* Find the old .data section. Figure out parameters of |
575 |
old_bss_size = OLD_SECTION_H (old_bss_index).sh_size; |
777 |
the new data2 and bss sections. */ |
576 |
#if defined(emacs) || !defined(DEBUG) |
778 |
|
577 |
new_bss_addr = (Elf32_Addr) sbrk (0); |
779 |
old_data_index = find_section (".data", old_section_names, |
|
|
780 |
old_name, old_file_h, old_section_h, 0); |
781 |
|
782 |
#if defined (emacs) || !defined (DEBUG) |
783 |
new_bss_addr = (ElfW(Addr)) sbrk (0); |
578 |
#else |
784 |
#else |
579 |
new_bss_addr = old_bss_addr + old_bss_size + 0x1234; |
785 |
new_bss_addr = old_bss_addr + old_bss_size + 0x1234; |
580 |
#endif |
786 |
#endif |
581 |
new_data2_addr = old_bss_addr; |
787 |
new_data2_addr = old_bss_addr; |
582 |
new_data2_size = new_bss_addr - old_bss_addr; |
788 |
new_data2_size = new_bss_addr - old_bss_addr; |
583 |
new_data2_offset = OLD_SECTION_H (old_bss_index).sh_offset; |
789 |
new_data2_offset = OLD_SECTION_H (old_data_index).sh_offset + |
|
|
790 |
(new_data2_addr - OLD_SECTION_H (old_data_index).sh_addr); |
584 |
|
791 |
|
585 |
#ifdef DEBUG |
792 |
#ifdef DEBUG |
586 |
fprintf (stderr, "old_bss_index %d\n", old_bss_index); |
793 |
fprintf (stderr, "old_bss_index %d\n", old_bss_index); |
Lines 595-603
Link Here
|
595 |
if ((unsigned) new_bss_addr < (unsigned) old_bss_addr + old_bss_size) |
802 |
if ((unsigned) new_bss_addr < (unsigned) old_bss_addr + old_bss_size) |
596 |
fatal (".bss shrank when undumping???\n", 0, 0); |
803 |
fatal (".bss shrank when undumping???\n", 0, 0); |
597 |
|
804 |
|
598 |
/* Set the output file to the right size and mmap it. Set |
805 |
/* Set the output file to the right size. Allocate a buffer to hold |
599 |
* pointers to various interesting objects. stat_buf still has |
806 |
* the image of the new file. Set pointers to various interesting |
600 |
* old_file data. |
807 |
* objects. stat_buf still has old_file data. |
601 |
*/ |
808 |
*/ |
602 |
|
809 |
|
603 |
new_file = open (new_name, O_RDWR | O_CREAT, 0666); |
810 |
new_file = open (new_name, O_RDWR | O_CREAT, 0666); |
Lines 609-628
Link Here
|
609 |
if (ftruncate (new_file, new_file_size)) |
816 |
if (ftruncate (new_file, new_file_size)) |
610 |
fatal ("Can't ftruncate (%s): errno %d\n", new_name, errno); |
817 |
fatal ("Can't ftruncate (%s): errno %d\n", new_name, errno); |
611 |
|
818 |
|
612 |
#ifdef UNEXEC_USE_MAP_PRIVATE |
819 |
new_base = mmap (NULL, new_file_size, PROT_READ | PROT_WRITE, |
613 |
new_base = mmap (0, new_file_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, |
820 |
MAP_ANON | MAP_PRIVATE, mmap_fd, 0); |
614 |
new_file, 0); |
821 |
if (new_base == MAP_FAILED) |
615 |
#else |
822 |
fatal ("Can't allocate buffer for %s\n", old_name); |
616 |
new_base = mmap (0, new_file_size, PROT_READ | PROT_WRITE, MAP_SHARED, |
823 |
|
617 |
new_file, 0); |
824 |
new_file_h = (ElfW(Ehdr) *) new_base; |
618 |
#endif |
825 |
new_program_h = (ElfW(Phdr) *) ((byte *) new_base + old_file_h->e_phoff); |
619 |
|
826 |
new_section_h = (ElfW(Shdr) *) |
620 |
if (new_base == (caddr_t) -1) |
|
|
621 |
fatal ("Can't mmap (%s): errno %d\n", new_name, errno); |
622 |
|
623 |
new_file_h = (Elf32_Ehdr *) new_base; |
624 |
new_program_h = (Elf32_Phdr *) ((byte *) new_base + old_file_h->e_phoff); |
625 |
new_section_h = (Elf32_Shdr *) |
626 |
((byte *) new_base + old_file_h->e_shoff + new_data2_size); |
827 |
((byte *) new_base + old_file_h->e_shoff + new_data2_size); |
627 |
|
828 |
|
628 |
/* Make our new file, program and section headers as copies of the |
829 |
/* Make our new file, program and section headers as copies of the |
Lines 661-672
Link Here
|
661 |
for (n = new_file_h->e_phnum - 1; n >= 0; n--) |
862 |
for (n = new_file_h->e_phnum - 1; n >= 0; n--) |
662 |
{ |
863 |
{ |
663 |
/* Compute maximum of all requirements for alignment of section. */ |
864 |
/* Compute maximum of all requirements for alignment of section. */ |
664 |
int alignment = (NEW_PROGRAM_H (n)).p_align; |
865 |
ElfW(Word) alignment = (NEW_PROGRAM_H (n)).p_align; |
665 |
if ((OLD_SECTION_H (old_bss_index)).sh_addralign > alignment) |
866 |
if ((OLD_SECTION_H (old_bss_index)).sh_addralign > alignment) |
666 |
alignment = OLD_SECTION_H (old_bss_index).sh_addralign; |
867 |
alignment = OLD_SECTION_H (old_bss_index).sh_addralign; |
667 |
|
868 |
|
668 |
if (NEW_PROGRAM_H (n).p_vaddr + NEW_PROGRAM_H (n).p_filesz > old_bss_addr) |
869 |
#ifdef __sgi |
669 |
fatal ("Program segment above .bss in %s\n", old_name, 0); |
870 |
/* According to r02kar@x4u2.desy.de (Karsten Kuenne) |
|
|
871 |
and oliva@gnu.org (Alexandre Oliva), on IRIX 5.2, we |
872 |
always get "Program segment above .bss" when dumping |
873 |
when the executable doesn't have an sbss section. */ |
874 |
if (old_sbss_index != -1) |
875 |
#endif /* __sgi */ |
876 |
if (NEW_PROGRAM_H (n).p_vaddr + NEW_PROGRAM_H (n).p_filesz |
877 |
> (old_sbss_index == -1 |
878 |
? old_bss_addr |
879 |
: round_up (old_bss_addr, alignment))) |
880 |
fatal ("Program segment above .bss in %s\n", old_name, 0); |
670 |
|
881 |
|
671 |
if (NEW_PROGRAM_H (n).p_type == PT_LOAD |
882 |
if (NEW_PROGRAM_H (n).p_type == PT_LOAD |
672 |
&& (round_up ((NEW_PROGRAM_H (n)).p_vaddr |
883 |
&& (round_up ((NEW_PROGRAM_H (n)).p_vaddr |
Lines 678-684
Link Here
|
678 |
if (n < 0) |
889 |
if (n < 0) |
679 |
fatal ("Couldn't find segment next to .bss in %s\n", old_name, 0); |
890 |
fatal ("Couldn't find segment next to .bss in %s\n", old_name, 0); |
680 |
|
891 |
|
681 |
NEW_PROGRAM_H (n).p_filesz += new_data2_size; |
892 |
/* Make sure that the size includes any padding before the old .bss |
|
|
893 |
section. */ |
894 |
NEW_PROGRAM_H (n).p_filesz = new_bss_addr - NEW_PROGRAM_H (n).p_vaddr; |
682 |
NEW_PROGRAM_H (n).p_memsz = NEW_PROGRAM_H (n).p_filesz; |
895 |
NEW_PROGRAM_H (n).p_memsz = NEW_PROGRAM_H (n).p_filesz; |
683 |
|
896 |
|
684 |
#if 0 /* Maybe allow section after data2 - does this ever happen? */ |
897 |
#if 0 /* Maybe allow section after data2 - does this ever happen? */ |
Lines 712-723
Link Here
|
712 |
for (n = 1, nn = 1; n < (int) old_file_h->e_shnum; n++, nn++) |
925 |
for (n = 1, nn = 1; n < (int) old_file_h->e_shnum; n++, nn++) |
713 |
{ |
926 |
{ |
714 |
caddr_t src; |
927 |
caddr_t src; |
715 |
/* If it is bss section, insert the new data2 section before it. */ |
928 |
/* If it is (s)bss section, insert the new data2 section before it. */ |
716 |
if (n == old_bss_index) |
929 |
/* new_data2_index is the index of either old_sbss or old_bss, that was |
|
|
930 |
chosen as a section for new_data2. */ |
931 |
if (n == new_data2_index) |
717 |
{ |
932 |
{ |
718 |
/* Steal the data section header for this data2 section. */ |
933 |
/* Steal the data section header for this data2 section. */ |
719 |
memcpy (&NEW_SECTION_H (nn), &OLD_SECTION_H (old_data_index), |
934 |
memcpy (&NEW_SECTION_H (nn), &OLD_SECTION_H (old_data_index), |
720 |
new_file_h->e_shentsize); |
935 |
new_file_h->e_shentsize); |
721 |
|
936 |
|
722 |
NEW_SECTION_H (nn).sh_addr = new_data2_addr; |
937 |
NEW_SECTION_H (nn).sh_addr = new_data2_addr; |
723 |
NEW_SECTION_H (nn).sh_offset = new_data2_offset; |
938 |
NEW_SECTION_H (nn).sh_offset = new_data2_offset; |
Lines 737-749
Link Here
|
737 |
memcpy (&NEW_SECTION_H (nn), &OLD_SECTION_H (n), |
952 |
memcpy (&NEW_SECTION_H (nn), &OLD_SECTION_H (n), |
738 |
old_file_h->e_shentsize); |
953 |
old_file_h->e_shentsize); |
739 |
|
954 |
|
740 |
/* The new bss section's size is zero, and its file offset and virtual |
955 |
if (n == old_bss_index |
741 |
address should be off by NEW_DATA2_SIZE. */ |
956 |
/* The new bss and sbss section's size is zero, and its file offset |
742 |
if (n == old_bss_index) |
957 |
and virtual address should be off by NEW_DATA2_SIZE. */ |
|
|
958 |
|| n == old_sbss_index |
959 |
) |
743 |
{ |
960 |
{ |
744 |
/* NN should be `old_bss_index + 1' at this point. */ |
961 |
/* NN should be `old_s?bss_index + 1' at this point. */ |
745 |
NEW_SECTION_H (nn).sh_offset += new_data2_size; |
962 |
NEW_SECTION_H (nn).sh_offset = |
746 |
NEW_SECTION_H (nn).sh_addr += new_data2_size; |
963 |
NEW_SECTION_H (new_data2_index).sh_offset + new_data2_size; |
|
|
964 |
NEW_SECTION_H (nn).sh_addr = |
965 |
NEW_SECTION_H (new_data2_index).sh_addr + new_data2_size; |
747 |
/* Let the new bss section address alignment be the same as the |
966 |
/* Let the new bss section address alignment be the same as the |
748 |
section address alignment followed the old bss section, so |
967 |
section address alignment followed the old bss section, so |
749 |
this section will be placed in exactly the same place. */ |
968 |
this section will be placed in exactly the same place. */ |
Lines 767-773
Link Here
|
767 |
>= OLD_SECTION_H (old_bss_index-1).sh_offset) |
986 |
>= OLD_SECTION_H (old_bss_index-1).sh_offset) |
768 |
NEW_SECTION_H (nn).sh_offset += new_data2_size; |
987 |
NEW_SECTION_H (nn).sh_offset += new_data2_size; |
769 |
#else |
988 |
#else |
770 |
if (NEW_SECTION_H (nn).sh_offset >= new_data2_offset) |
989 |
if (round_up (NEW_SECTION_H (nn).sh_offset, |
|
|
990 |
OLD_SECTION_H (old_bss_index).sh_addralign) |
991 |
>= new_data2_offset) |
771 |
NEW_SECTION_H (nn).sh_offset += new_data2_size; |
992 |
NEW_SECTION_H (nn).sh_offset += new_data2_size; |
772 |
#endif |
993 |
#endif |
773 |
/* Any section that was originally placed after the section |
994 |
/* Any section that was originally placed after the section |
Lines 787-804
Link Here
|
787 |
if (NEW_SECTION_H (nn).sh_type != SHT_SYMTAB |
1008 |
if (NEW_SECTION_H (nn).sh_type != SHT_SYMTAB |
788 |
&& NEW_SECTION_H (nn).sh_type != SHT_DYNSYM) |
1009 |
&& NEW_SECTION_H (nn).sh_type != SHT_DYNSYM) |
789 |
PATCH_INDEX (NEW_SECTION_H (nn).sh_info); |
1010 |
PATCH_INDEX (NEW_SECTION_H (nn).sh_info); |
|
|
1011 |
|
1012 |
if (old_sbss_index != -1) |
1013 |
if (!strcmp (old_section_names + NEW_SECTION_H (nn).sh_name, ".sbss")) |
1014 |
{ |
1015 |
NEW_SECTION_H (nn).sh_offset = |
1016 |
round_up (NEW_SECTION_H (nn).sh_offset, |
1017 |
NEW_SECTION_H (nn).sh_addralign); |
1018 |
NEW_SECTION_H (nn).sh_type = SHT_PROGBITS; |
1019 |
} |
790 |
|
1020 |
|
791 |
/* Now, start to copy the content of sections. */ |
1021 |
/* Now, start to copy the content of sections. */ |
792 |
if (NEW_SECTION_H (nn).sh_type == SHT_NULL |
1022 |
if (NEW_SECTION_H (nn).sh_type == SHT_NULL |
793 |
|| NEW_SECTION_H (nn).sh_type == SHT_NOBITS) |
1023 |
|| NEW_SECTION_H (nn).sh_type == SHT_NOBITS) |
794 |
continue; |
1024 |
continue; |
795 |
|
1025 |
|
796 |
/* Write out the sections. .data and .data1 (and data2, called |
1026 |
/* Write out the sections. .data and .data1 (and data2, called |
797 |
".data" in the strings table) get copied from the current process |
1027 |
".data" in the strings table) get copied from the current process |
798 |
instead of the old file. */ |
1028 |
instead of the old file. */ |
799 |
if (!strcmp (old_section_names + NEW_SECTION_H (n).sh_name, ".data") |
1029 |
if (!strcmp (old_section_names + NEW_SECTION_H (n).sh_name, ".data") |
800 |
|| !strcmp ((old_section_names + NEW_SECTION_H (n).sh_name), |
1030 |
|| !strcmp ((old_section_names + NEW_SECTION_H (n).sh_name), |
801 |
".data1")) |
1031 |
".sdata") |
|
|
1032 |
|| !strcmp ((old_section_names + NEW_SECTION_H (n).sh_name), |
1033 |
".lit4") |
1034 |
|| !strcmp ((old_section_names + NEW_SECTION_H (n).sh_name), |
1035 |
".lit8") |
1036 |
/* The conditional bit below was in Oliva's original code |
1037 |
(1999-08-25) and seems to have been dropped by mistake |
1038 |
subsequently. It prevents a crash at startup under X in |
1039 |
`IRIX64 6.5 6.5.17m' with c_dev 7.3.1.3m. It causes no |
1040 |
trouble on the other ELF platforms I could test (Irix |
1041 |
6.5.15m, Solaris 8, Debian Potato x86, Debian Woody |
1042 |
SPARC); however, it's reported to cause crashes under |
1043 |
some version of GNU/Linux. It's not yet clear what's |
1044 |
changed in that Irix version to cause the problem, or why |
1045 |
the fix sometimes fails under GNU/Linux. There's |
1046 |
probably no good reason to have something Irix-specific |
1047 |
here, but this will have to do for now. IRIX6_5 is the |
1048 |
most specific macro we have to test. -- fx 2002-10-01 */ |
1049 |
#ifdef IRIX6_5 |
1050 |
|| !strcmp ((old_section_names + NEW_SECTION_H (n).sh_name), |
1051 |
".got") |
1052 |
#endif |
1053 |
|| !strcmp ((old_section_names + NEW_SECTION_H (n).sh_name), |
1054 |
".sdata1") |
1055 |
|| !strcmp ((old_section_names + NEW_SECTION_H (n).sh_name), |
1056 |
".data1") |
1057 |
|| !strcmp ((old_section_names + NEW_SECTION_H (n).sh_name), |
1058 |
".sbss")) |
802 |
src = (caddr_t) OLD_SECTION_H (n).sh_addr; |
1059 |
src = (caddr_t) OLD_SECTION_H (n).sh_addr; |
803 |
else |
1060 |
else |
804 |
src = old_base + OLD_SECTION_H (n).sh_offset; |
1061 |
src = old_base + OLD_SECTION_H (n).sh_offset; |
Lines 806-818
Link Here
|
806 |
memcpy (NEW_SECTION_H (nn).sh_offset + new_base, src, |
1063 |
memcpy (NEW_SECTION_H (nn).sh_offset + new_base, src, |
807 |
NEW_SECTION_H (nn).sh_size); |
1064 |
NEW_SECTION_H (nn).sh_size); |
808 |
|
1065 |
|
|
|
1066 |
#ifdef __alpha__ |
1067 |
/* Update Alpha COFF symbol table: */ |
1068 |
if (strcmp (old_section_names + OLD_SECTION_H (n).sh_name, ".mdebug") |
1069 |
== 0) |
1070 |
{ |
1071 |
pHDRR symhdr = (pHDRR) (NEW_SECTION_H (nn).sh_offset + new_base); |
1072 |
|
1073 |
symhdr->cbLineOffset += new_data2_size; |
1074 |
symhdr->cbDnOffset += new_data2_size; |
1075 |
symhdr->cbPdOffset += new_data2_size; |
1076 |
symhdr->cbSymOffset += new_data2_size; |
1077 |
symhdr->cbOptOffset += new_data2_size; |
1078 |
symhdr->cbAuxOffset += new_data2_size; |
1079 |
symhdr->cbSsOffset += new_data2_size; |
1080 |
symhdr->cbSsExtOffset += new_data2_size; |
1081 |
symhdr->cbFdOffset += new_data2_size; |
1082 |
symhdr->cbRfdOffset += new_data2_size; |
1083 |
symhdr->cbExtOffset += new_data2_size; |
1084 |
} |
1085 |
#endif /* __alpha__ */ |
1086 |
|
1087 |
#if defined (__sony_news) && defined (_SYSTYPE_SYSV) |
1088 |
if (NEW_SECTION_H (nn).sh_type == SHT_MIPS_DEBUG |
1089 |
&& old_mdebug_index != -1) |
1090 |
{ |
1091 |
int diff = NEW_SECTION_H(nn).sh_offset |
1092 |
- OLD_SECTION_H(old_mdebug_index).sh_offset; |
1093 |
HDRR *phdr = (HDRR *)(NEW_SECTION_H (nn).sh_offset + new_base); |
1094 |
|
1095 |
if (diff) |
1096 |
{ |
1097 |
phdr->cbLineOffset += diff; |
1098 |
phdr->cbDnOffset += diff; |
1099 |
phdr->cbPdOffset += diff; |
1100 |
phdr->cbSymOffset += diff; |
1101 |
phdr->cbOptOffset += diff; |
1102 |
phdr->cbAuxOffset += diff; |
1103 |
phdr->cbSsOffset += diff; |
1104 |
phdr->cbSsExtOffset += diff; |
1105 |
phdr->cbFdOffset += diff; |
1106 |
phdr->cbRfdOffset += diff; |
1107 |
phdr->cbExtOffset += diff; |
1108 |
} |
1109 |
} |
1110 |
#endif /* __sony_news && _SYSTYPE_SYSV */ |
1111 |
|
1112 |
#if __sgi |
1113 |
/* Adjust the HDRR offsets in .mdebug and copy the |
1114 |
line data if it's in its usual 'hole' in the object. |
1115 |
Makes the new file debuggable with dbx. |
1116 |
patches up two problems: the absolute file offsets |
1117 |
in the HDRR record of .mdebug (see /usr/include/syms.h), and |
1118 |
the ld bug that gets the line table in a hole in the |
1119 |
elf file rather than in the .mdebug section proper. |
1120 |
David Anderson. davea@sgi.com Jan 16,1994. */ |
1121 |
if (n == old_mdebug_index) |
1122 |
{ |
1123 |
#define MDEBUGADJUST(__ct,__fileaddr) \ |
1124 |
if (n_phdrr->__ct > 0) \ |
1125 |
{ \ |
1126 |
n_phdrr->__fileaddr += movement; \ |
1127 |
} |
1128 |
|
1129 |
HDRR * o_phdrr = (HDRR *)((byte *)old_base + OLD_SECTION_H (n).sh_offset); |
1130 |
HDRR * n_phdrr = (HDRR *)((byte *)new_base + NEW_SECTION_H (nn).sh_offset); |
1131 |
unsigned movement = new_data2_size; |
1132 |
|
1133 |
MDEBUGADJUST (idnMax, cbDnOffset); |
1134 |
MDEBUGADJUST (ipdMax, cbPdOffset); |
1135 |
MDEBUGADJUST (isymMax, cbSymOffset); |
1136 |
MDEBUGADJUST (ioptMax, cbOptOffset); |
1137 |
MDEBUGADJUST (iauxMax, cbAuxOffset); |
1138 |
MDEBUGADJUST (issMax, cbSsOffset); |
1139 |
MDEBUGADJUST (issExtMax, cbSsExtOffset); |
1140 |
MDEBUGADJUST (ifdMax, cbFdOffset); |
1141 |
MDEBUGADJUST (crfd, cbRfdOffset); |
1142 |
MDEBUGADJUST (iextMax, cbExtOffset); |
1143 |
/* The Line Section, being possible off in a hole of the object, |
1144 |
requires special handling. */ |
1145 |
if (n_phdrr->cbLine > 0) |
1146 |
{ |
1147 |
if (o_phdrr->cbLineOffset > (OLD_SECTION_H (n).sh_offset |
1148 |
+ OLD_SECTION_H (n).sh_size)) |
1149 |
{ |
1150 |
/* line data is in a hole in elf. do special copy and adjust |
1151 |
for this ld mistake. |
1152 |
*/ |
1153 |
n_phdrr->cbLineOffset += movement; |
1154 |
|
1155 |
memcpy (n_phdrr->cbLineOffset + new_base, |
1156 |
o_phdrr->cbLineOffset + old_base, n_phdrr->cbLine); |
1157 |
} |
1158 |
else |
1159 |
{ |
1160 |
/* somehow line data is in .mdebug as it is supposed to be. */ |
1161 |
MDEBUGADJUST (cbLine, cbLineOffset); |
1162 |
} |
1163 |
} |
1164 |
} |
1165 |
#endif /* __sgi */ |
1166 |
|
809 |
/* If it is the symbol table, its st_shndx field needs to be patched. */ |
1167 |
/* If it is the symbol table, its st_shndx field needs to be patched. */ |
810 |
if (NEW_SECTION_H (nn).sh_type == SHT_SYMTAB |
1168 |
if (NEW_SECTION_H (nn).sh_type == SHT_SYMTAB |
811 |
|| NEW_SECTION_H (nn).sh_type == SHT_DYNSYM) |
1169 |
|| NEW_SECTION_H (nn).sh_type == SHT_DYNSYM) |
812 |
{ |
1170 |
{ |
813 |
Elf32_Shdr *spt = &NEW_SECTION_H (nn); |
1171 |
ElfW(Shdr) *spt = &NEW_SECTION_H (nn); |
814 |
unsigned int num = spt->sh_size / spt->sh_entsize; |
1172 |
unsigned int num = spt->sh_size / spt->sh_entsize; |
815 |
Elf32_Sym * sym = (Elf32_Sym *) (NEW_SECTION_H (nn).sh_offset + |
1173 |
ElfW(Sym) * sym = (ElfW(Sym) *) (NEW_SECTION_H (nn).sh_offset + |
816 |
new_base); |
1174 |
new_base); |
817 |
for (; num--; sym++) |
1175 |
for (; num--; sym++) |
818 |
{ |
1176 |
{ |
Lines 830-836
Link Here
|
830 |
for (n = new_file_h->e_shnum - 1; n; n--) |
1188 |
for (n = new_file_h->e_shnum - 1; n; n--) |
831 |
{ |
1189 |
{ |
832 |
byte *symnames; |
1190 |
byte *symnames; |
833 |
Elf32_Sym *symp, *symendp; |
1191 |
ElfW(Sym) *symp, *symendp; |
834 |
|
1192 |
|
835 |
if (NEW_SECTION_H (n).sh_type != SHT_DYNSYM |
1193 |
if (NEW_SECTION_H (n).sh_type != SHT_DYNSYM |
836 |
&& NEW_SECTION_H (n).sh_type != SHT_SYMTAB) |
1194 |
&& NEW_SECTION_H (n).sh_type != SHT_SYMTAB) |
Lines 838-849
Link Here
|
838 |
|
1196 |
|
839 |
symnames = ((byte *) new_base |
1197 |
symnames = ((byte *) new_base |
840 |
+ NEW_SECTION_H (NEW_SECTION_H (n).sh_link).sh_offset); |
1198 |
+ NEW_SECTION_H (NEW_SECTION_H (n).sh_link).sh_offset); |
841 |
symp = (Elf32_Sym *) (NEW_SECTION_H (n).sh_offset + new_base); |
1199 |
symp = (ElfW(Sym) *) (NEW_SECTION_H (n).sh_offset + new_base); |
842 |
symendp = (Elf32_Sym *) ((byte *)symp + NEW_SECTION_H (n).sh_size); |
1200 |
symendp = (ElfW(Sym) *) ((byte *)symp + NEW_SECTION_H (n).sh_size); |
843 |
|
1201 |
|
844 |
for (; symp < symendp; symp ++) |
1202 |
for (; symp < symendp; symp ++) |
845 |
if (strcmp ((char *) (symnames + symp->st_name), "_end") == 0 |
1203 |
if (strcmp ((char *) (symnames + symp->st_name), "_end") == 0 |
846 |
|| strcmp ((char *) (symnames + symp->st_name), "_edata") == 0) |
1204 |
|| strcmp ((char *) (symnames + symp->st_name), "end") == 0 |
|
|
1205 |
|| strcmp ((char *) (symnames + symp->st_name), "_edata") == 0 |
1206 |
|| strcmp ((char *) (symnames + symp->st_name), "edata") == 0) |
847 |
memcpy (&symp->st_value, &new_bss_addr, sizeof (new_bss_addr)); |
1207 |
memcpy (&symp->st_value, &new_bss_addr, sizeof (new_bss_addr)); |
848 |
} |
1208 |
} |
849 |
|
1209 |
|
Lines 851-857
Link Here
|
851 |
that it can undo relocations performed by the runtime linker. */ |
1211 |
that it can undo relocations performed by the runtime linker. */ |
852 |
for (n = new_file_h->e_shnum - 1; n; n--) |
1212 |
for (n = new_file_h->e_shnum - 1; n; n--) |
853 |
{ |
1213 |
{ |
854 |
Elf32_Shdr section = NEW_SECTION_H (n); |
1214 |
ElfW(Shdr) section = NEW_SECTION_H (n); |
855 |
switch (section.sh_type) { |
1215 |
switch (section.sh_type) { |
856 |
default: |
1216 |
default: |
857 |
break; |
1217 |
break; |
Lines 863-899
Link Here
|
863 |
nn = section.sh_info; |
1223 |
nn = section.sh_info; |
864 |
if (!strcmp (old_section_names + NEW_SECTION_H (nn).sh_name, ".data") |
1224 |
if (!strcmp (old_section_names + NEW_SECTION_H (nn).sh_name, ".data") |
865 |
|| !strcmp ((old_section_names + NEW_SECTION_H (nn).sh_name), |
1225 |
|| !strcmp ((old_section_names + NEW_SECTION_H (nn).sh_name), |
|
|
1226 |
".sdata") |
1227 |
|| !strcmp ((old_section_names + NEW_SECTION_H (nn).sh_name), |
1228 |
".lit4") |
1229 |
|| !strcmp ((old_section_names + NEW_SECTION_H (nn).sh_name), |
1230 |
".lit8") |
1231 |
#ifdef IRIX6_5 /* see above */ |
1232 |
|| !strcmp ((old_section_names + NEW_SECTION_H (nn).sh_name), |
1233 |
".got") |
1234 |
#endif |
1235 |
|| !strcmp ((old_section_names + NEW_SECTION_H (nn).sh_name), |
1236 |
".sdata1") |
1237 |
|| !strcmp ((old_section_names + NEW_SECTION_H (nn).sh_name), |
866 |
".data1")) |
1238 |
".data1")) |
867 |
{ |
1239 |
{ |
868 |
Elf32_Addr offset = NEW_SECTION_H (nn).sh_addr - |
1240 |
ElfW(Addr) offset = NEW_SECTION_H (nn).sh_addr - |
869 |
NEW_SECTION_H (nn).sh_offset; |
1241 |
NEW_SECTION_H (nn).sh_offset; |
870 |
caddr_t reloc = old_base + section.sh_offset, end; |
1242 |
caddr_t reloc = old_base + section.sh_offset, end; |
871 |
for (end = reloc + section.sh_size; reloc < end; |
1243 |
for (end = reloc + section.sh_size; reloc < end; |
872 |
reloc += section.sh_entsize) |
1244 |
reloc += section.sh_entsize) |
873 |
{ |
1245 |
{ |
874 |
Elf32_Addr addr = ((Elf32_Rel *) reloc)->r_offset - offset; |
1246 |
ElfW(Addr) addr = ((ElfW(Rel) *) reloc)->r_offset - offset; |
875 |
memcpy (new_base + addr, old_base + addr, 4); |
1247 |
#ifdef __alpha__ |
876 |
} |
1248 |
/* The Alpha ELF binutils currently have a bug that |
|
|
1249 |
sometimes results in relocs that contain all |
1250 |
zeroes. Work around this for now... */ |
1251 |
if (((ElfW(Rel) *) reloc)->r_offset == 0) |
1252 |
continue; |
1253 |
#endif |
1254 |
memcpy (new_base + addr, old_base + addr, sizeof(ElfW(Addr))); |
1255 |
} |
877 |
} |
1256 |
} |
878 |
break; |
1257 |
break; |
879 |
} |
1258 |
} |
880 |
} |
1259 |
} |
881 |
|
1260 |
|
882 |
#ifdef UNEXEC_USE_MAP_PRIVATE |
1261 |
/* Write out new_file, close it, and free the buffer containing its |
883 |
if (lseek (new_file, 0, SEEK_SET) == -1) |
1262 |
* contents */ |
884 |
fatal ("Can't rewind (%s): errno %d\n", new_name, errno); |
|
|
885 |
|
1263 |
|
886 |
if (write (new_file, new_base, new_file_size) != new_file_size) |
1264 |
if (write (new_file, new_base, new_file_size) != new_file_size) |
887 |
fatal ("Can't write (%s): errno %d\n", new_name, errno); |
1265 |
fatal ("Didn't write %d bytes to %s: errno %d\n", |
888 |
#endif |
1266 |
new_file_size, new_base, errno); |
|
|
1267 |
|
1268 |
if (close (new_file)) |
1269 |
fatal ("Can't close (%s): errno %d\n", new_name, errno); |
1270 |
|
1271 |
munmap (new_base, new_file_size); |
889 |
|
1272 |
|
890 |
/* Close the files and make the new file executable. */ |
1273 |
/* Close old_file, and free the corresponding buffer */ |
|
|
1274 |
|
1275 |
#if MAP_ANON == 0 |
1276 |
close (mmap_fd); |
1277 |
#endif |
891 |
|
1278 |
|
892 |
if (close (old_file)) |
1279 |
if (close (old_file)) |
893 |
fatal ("Can't close (%s): errno %d\n", old_name, errno); |
1280 |
fatal ("Can't close (%s): errno %d\n", old_name, errno); |
894 |
|
1281 |
|
895 |
if (close (new_file)) |
1282 |
munmap (old_base, old_file_size); |
896 |
fatal ("Can't close (%s): errno %d\n", new_name, errno); |
1283 |
|
|
|
1284 |
/* Make the new file executable */ |
897 |
|
1285 |
|
898 |
if (stat (new_name, &stat_buf) == -1) |
1286 |
if (stat (new_name, &stat_buf) == -1) |
899 |
fatal ("Can't stat (%s): errno %d\n", new_name, errno); |
1287 |
fatal ("Can't stat (%s): errno %d\n", new_name, errno); |