1 /* Copyright (C) 1985, 1986, 1987, 1988, 1990, 1992, 1999, 2000, 01, 02
2 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; 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.
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding! */
27 * unexec.c - Convert a running program into an a.out file.
29 * Author: Spencer W. Thomas
30 * Computer Science Dept.
32 * Date: Tue Mar 2 1982
33 * Modified heavily since then.
36 * unexec (new_name, old_name, data_start, bss_start, entry_address)
37 * char *new_name, *old_name;
38 * unsigned data_start, bss_start, entry_address;
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.
42 * If old_name is non-NULL, the symbol table will be taken from the given file.
43 * On some machines, an existing old_name file is required.
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.
48 * Data_start gives the boundary between the text segment and the data
49 * segment of the program. The text segment can contain shared, read-only
50 * program code and literal data, while the data segment is always unshared
51 * and unprotected. Data_start gives the lowest unprotected address.
52 * The value you specify may be rounded down to a suitable boundary
53 * as required by the machine you are using.
55 * Bss_start indicates how much of the data segment is to be saved in the
56 * a.out file and restored when the program is executed. It gives the lowest
57 * unsaved address, and is rounded up to a page boundary. The default when 0
58 * is given assumes that the entire data segment is to be stored, including
59 * the previous data and bss as well as any additional storage allocated with
62 * The new file is set up to start at entry_address.
66 /* Even more heavily modified by james@bigtex.cactus.org of Dell Computer Co.
69 * Basic theory: the data space of the running process needs to be
70 * dumped to the output file. Normally we would just enlarge the size
71 * of .data, scooting everything down. But we can't do that in ELF,
72 * because there is often something between the .data space and the
75 * In the temacs dump below, notice that the Global Offset Table
76 * (.got) and the Dynamic link data (.dynamic) come between .data1 and
77 * .bss. It does not work to overlap .data with these fields.
79 * The solution is to create a new .data segment. This segment is
80 * filled with data from the current process. Since the contents of
81 * various sections refer to sections by index, the new .data segment
82 * is made the last in the table to avoid changing any existing index.
84 * This is an example of how the section headers are changed. "Addr"
85 * is a process virtual address. "Offset" is a file offset.
87 raid:/nfs/raid/src/dist-18.56/src> dump -h temacs
91 **** SECTION HEADER TABLE ****
92 [No] Type Flags Addr Offset Size Name
93 Link Info Adralgn Entsize
95 [1] 1 2 0x80480d4 0xd4 0x13 .interp
98 [2] 5 2 0x80480e8 0xe8 0x388 .hash
101 [3] 11 2 0x8048470 0x470 0x7f0 .dynsym
104 [4] 3 2 0x8048c60 0xc60 0x3ad .dynstr
107 [5] 9 2 0x8049010 0x1010 0x338 .rel.plt
110 [6] 1 6 0x8049348 0x1348 0x3 .init
113 [7] 1 6 0x804934c 0x134c 0x680 .plt
116 [8] 1 6 0x80499cc 0x19cc 0x3c56f .text
119 [9] 1 6 0x8085f3c 0x3df3c 0x3 .fini
122 [10] 1 2 0x8085f40 0x3df40 0x69c .rodata
125 [11] 1 2 0x80865dc 0x3e5dc 0xd51 .rodata1
128 [12] 1 3 0x8088330 0x3f330 0x20afc .data
131 [13] 1 3 0x80a8e2c 0x5fe2c 0x89d .data1
134 [14] 1 3 0x80a96cc 0x606cc 0x1a8 .got
137 [15] 6 3 0x80a9874 0x60874 0x80 .dynamic
140 [16] 8 3 0x80a98f4 0x608f4 0x449c .bss
143 [17] 2 0 0 0x608f4 0x9b90 .symtab
146 [18] 3 0 0 0x6a484 0x8526 .strtab
149 [19] 3 0 0 0x729aa 0x93 .shstrtab
152 [20] 1 0 0 0x72a3d 0x68b7 .comment
155 raid:/nfs/raid/src/dist-18.56/src> dump -h xemacs
159 **** SECTION HEADER TABLE ****
160 [No] Type Flags Addr Offset Size Name
161 Link Info Adralgn Entsize
163 [1] 1 2 0x80480d4 0xd4 0x13 .interp
166 [2] 5 2 0x80480e8 0xe8 0x388 .hash
169 [3] 11 2 0x8048470 0x470 0x7f0 .dynsym
172 [4] 3 2 0x8048c60 0xc60 0x3ad .dynstr
175 [5] 9 2 0x8049010 0x1010 0x338 .rel.plt
178 [6] 1 6 0x8049348 0x1348 0x3 .init
181 [7] 1 6 0x804934c 0x134c 0x680 .plt
184 [8] 1 6 0x80499cc 0x19cc 0x3c56f .text
187 [9] 1 6 0x8085f3c 0x3df3c 0x3 .fini
190 [10] 1 2 0x8085f40 0x3df40 0x69c .rodata
193 [11] 1 2 0x80865dc 0x3e5dc 0xd51 .rodata1
196 [12] 1 3 0x8088330 0x3f330 0x20afc .data
199 [13] 1 3 0x80a8e2c 0x5fe2c 0x89d .data1
202 [14] 1 3 0x80a96cc 0x606cc 0x1a8 .got
205 [15] 6 3 0x80a9874 0x60874 0x80 .dynamic
208 [16] 8 3 0x80c6800 0x7d800 0 .bss
211 [17] 2 0 0 0x7d800 0x9b90 .symtab
214 [18] 3 0 0 0x87390 0x8526 .strtab
217 [19] 3 0 0 0x8f8b6 0x93 .shstrtab
220 [20] 1 0 0 0x8f949 0x68b7 .comment
223 [21] 1 3 0x80a98f4 0x608f4 0x1cf0c .data
226 * This is an example of how the file header is changed. "Shoff" is
227 * the section header offset within the file. Since that table is
228 * after the new .data section, it is moved. "Shnum" is the number of
229 * sections, which we increment.
231 * "Phoff" is the file offset to the program header. "Phentsize" and
232 * "Shentsz" are the program and section header entries sizes respectively.
233 * These can be larger than the apparent struct sizes.
235 raid:/nfs/raid/src/dist-18.56/src> dump -f temacs
240 Class Data Type Machine Version
241 Entry Phoff Shoff Flags Ehsize
242 Phentsize Phnum Shentsz Shnum Shstrndx
245 0x80499cc 0x34 0x792f4 0 0x34
248 raid:/nfs/raid/src/dist-18.56/src> dump -f xemacs
253 Class Data Type Machine Version
254 Entry Phoff Shoff Flags Ehsize
255 Phentsize Phnum Shentsz Shnum Shstrndx
258 0x80499cc 0x34 0x96200 0 0x34
261 * These are the program headers. "Offset" is the file offset to the
262 * segment. "Vaddr" is the memory load address. "Filesz" is the
263 * segment size as it appears in the file, and "Memsz" is the size in
264 * memory. Below, the third segment is the code and the fourth is the
265 * data: the difference between Filesz and Memsz is .bss
267 raid:/nfs/raid/src/dist-18.56/src> dump -o temacs
270 ***** PROGRAM EXECUTION HEADER *****
271 Type Offset Vaddr Paddr
272 Filesz Memsz Flags Align
281 0x3f2f9 0x3f2f9 5 0x1000
283 1 0x3f330 0x8088330 0
284 0x215c4 0x25a60 7 0x1000
286 2 0x60874 0x80a9874 0
289 raid:/nfs/raid/src/dist-18.56/src> dump -o xemacs
292 ***** PROGRAM EXECUTION HEADER *****
293 Type Offset Vaddr Paddr
294 Filesz Memsz Flags Align
303 0x3f2f9 0x3f2f9 5 0x1000
305 1 0x3f330 0x8088330 0
306 0x3e4d0 0x3e4d0 7 0x1000
308 2 0x60874 0x80a9874 0
314 /* Modified by wtien@urbana.mcd.mot.com of Motorola Inc.
316 * The above mechanism does not work if the unexeced ELF file is being
317 * re-layout by other applications (such as `strip'). All the applications
318 * that re-layout the internal of ELF will layout all sections in ascending
319 * order of their file offsets. After the re-layout, the data2 section will
320 * still be the LAST section in the section header vector, but its file offset
321 * is now being pushed far away down, and causes part of it not to be mapped
322 * in (ie. not covered by the load segment entry in PHDR vector), therefore
323 * causes the new binary to fail.
325 * The solution is to modify the unexec algorithm to insert the new data2
326 * section header right before the new bss section header, so their file
327 * offsets will be in the ascending order. Since some of the section's (all
328 * sections AFTER the bss section) indexes are now changed, we also need to
329 * modify some fields to make them point to the right sections. This is done
330 * by macro PATCH_INDEX. All the fields that need to be patched are:
332 * 1. ELF header e_shstrndx field.
333 * 2. section header sh_link and sh_info field.
334 * 3. symbol table entry st_shndx field.
336 * The above example now should look like:
338 **** SECTION HEADER TABLE ****
339 [No] Type Flags Addr Offset Size Name
340 Link Info Adralgn Entsize
342 [1] 1 2 0x80480d4 0xd4 0x13 .interp
345 [2] 5 2 0x80480e8 0xe8 0x388 .hash
348 [3] 11 2 0x8048470 0x470 0x7f0 .dynsym
351 [4] 3 2 0x8048c60 0xc60 0x3ad .dynstr
354 [5] 9 2 0x8049010 0x1010 0x338 .rel.plt
357 [6] 1 6 0x8049348 0x1348 0x3 .init
360 [7] 1 6 0x804934c 0x134c 0x680 .plt
363 [8] 1 6 0x80499cc 0x19cc 0x3c56f .text
366 [9] 1 6 0x8085f3c 0x3df3c 0x3 .fini
369 [10] 1 2 0x8085f40 0x3df40 0x69c .rodata
372 [11] 1 2 0x80865dc 0x3e5dc 0xd51 .rodata1
375 [12] 1 3 0x8088330 0x3f330 0x20afc .data
378 [13] 1 3 0x80a8e2c 0x5fe2c 0x89d .data1
381 [14] 1 3 0x80a96cc 0x606cc 0x1a8 .got
384 [15] 6 3 0x80a9874 0x60874 0x80 .dynamic
387 [16] 1 3 0x80a98f4 0x608f4 0x1cf0c .data
390 [17] 8 3 0x80c6800 0x7d800 0 .bss
393 [18] 2 0 0 0x7d800 0x9b90 .symtab
396 [19] 3 0 0 0x87390 0x8526 .strtab
399 [20] 3 0 0 0x8f8b6 0x93 .shstrtab
402 [21] 1 0 0 0x8f949 0x68b7 .comment
408 * Modified by rdh@yottayotta.com of Yotta Yotta Incorporated.
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.
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.
425 #define fatal(a, b, c) fprintf (stderr, a, b, c), exit (1)
429 extern void fatal (char *, ...);
432 #include <sys/types.h>
434 #include <sys/stat.h>
439 #if !defined (__NetBSD__) && !defined (__OpenBSD__)
442 #include <sys/mman.h>
443 #if defined (__sony_news) && defined (_SYSTYPE_SYSV)
444 #include <sys/elf_mips.h>
446 #endif /* __sony_news && _SYSTYPE_SYSV */
448 #include <syms.h> /* for HDRR declaration */
453 #define MAP_ANON MAP_ANONYMOUS
460 #define MAP_FAILED ((void *) -1)
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. */
494 #define cbHDRR sizeof(HDRR)
495 #define hdrNil ((pHDRR)0)
500 * NetBSD does not have normal-looking user-land ELF support.
502 # if defined __alpha__ || defined __sparc_v9__
507 # include <sys/exec_elf.h>
510 # define PT_LOAD Elf_pt_load
511 # if 0 /* was in pkgsrc patches for 20.7 */
512 # define SHT_PROGBITS Elf_sht_progbits
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
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 */
527 # include <sys/exec_ecoff.h>
528 # define HDRR struct ecoff_symhdr
529 # define pHDRR HDRR *
530 # endif /* __alpha__ */
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__ */
539 # include <sys/exec_elf.h>
542 #if __GNU_LIBRARY__ - 0 >= 6
543 # include <link.h> /* get ElfW etc */
548 # define ElfBitsW(bits, type) Elf##bits##_##type
550 # define ElfBitsW(bits, type) Elf/**/bits/**/_/**/type
557 /* This macro expands `bits' before invoking ElfBitsW. */
558 # define ElfExpandBitsW(bits, type) ElfBitsW (bits, type)
559 # define ElfW(type) ElfExpandBitsW (ELFSIZE, type)
562 #ifndef ELF_BSS_SECTION_NAME
563 #define ELF_BSS_SECTION_NAME ".bss"
566 /* Get the address of a particular section or program header entry,
567 * accounting for the size of the entries.
570 On PPC Reference Platform running Solaris 2.5.1
571 the plt section is also of type NOBI like the bss section.
572 (not really stored) and therefore sections after the bss
573 section start at the plt offset. The plt section is always
574 the one just before the bss section.
575 Thus, we modify the test from
576 if (NEW_SECTION_H (nn).sh_offset >= new_data2_offset)
578 if (NEW_SECTION_H (nn).sh_offset >=
579 OLD_SECTION_H (old_bss_index-1).sh_offset)
580 This is just a hack. We should put the new data section
581 before the .plt section.
582 And we should not have this routine at all but use
583 the libelf library to read the old file and create the new
585 The changed code is minimal and depends on prep set in m/prep.h
587 Quantum Theory Project
588 University of Florida
593 #define OLD_SECTION_H(n) \
594 (*(ElfW(Shdr) *) ((byte *) old_section_h + old_file_h->e_shentsize * (n)))
595 #define NEW_SECTION_H(n) \
596 (*(ElfW(Shdr) *) ((byte *) new_section_h + new_file_h->e_shentsize * (n)))
597 #define OLD_PROGRAM_H(n) \
598 (*(ElfW(Phdr) *) ((byte *) old_program_h + old_file_h->e_phentsize * (n)))
599 #define NEW_PROGRAM_H(n) \
600 (*(ElfW(Phdr) *) ((byte *) new_program_h + new_file_h->e_phentsize * (n)))
602 #define PATCH_INDEX(n) \
604 if ((int) (n) >= old_bss_index) \
606 typedef unsigned char byte
;
608 /* Round X up to a multiple of Y. */
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.
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. */
628 find_section (name
, section_names
, file_name
, old_file_h
, old_section_h
, noerror
)
632 ElfW(Ehdr
) *old_file_h
;
633 ElfW(Shdr
) *old_section_h
;
638 for (idx
= 1; idx
< old_file_h
->e_shnum
; idx
++)
641 fprintf (stderr
, "Looking for %s - found %s\n", name
,
642 section_names
+ OLD_SECTION_H (idx
).sh_name
);
644 if (!strcmp (section_names
+ OLD_SECTION_H (idx
).sh_name
,
648 if (idx
== old_file_h
->e_shnum
)
653 fatal ("Can't find %s in %s.\n", name
, file_name
);
659 /* ****************************************************************
664 * In ELF, this works by replacing the old .bss section with a new
665 * .data section, and inserting an empty .bss immediately afterwards.
669 unexec (new_name
, old_name
, data_start
, bss_start
, entry_address
)
670 char *new_name
, *old_name
;
671 unsigned data_start
, bss_start
, entry_address
;
673 int new_file
, old_file
, new_file_size
;
675 /* Pointers to the base of the image of the two files. */
676 caddr_t old_base
, new_base
;
684 /* Pointers to the file, program and section headers for the old and new
687 ElfW(Ehdr
) *old_file_h
, *new_file_h
;
688 ElfW(Phdr
) *old_program_h
, *new_program_h
;
689 ElfW(Shdr
) *old_section_h
, *new_section_h
;
691 /* Point to the section name table in the old file */
692 char *old_section_names
;
694 ElfW(Addr
) old_bss_addr
, new_bss_addr
;
695 ElfW(Word
) old_bss_size
, new_data2_size
;
696 ElfW(Off
) new_data2_offset
;
697 ElfW(Addr
) new_data2_addr
;
700 int old_bss_index
, old_sbss_index
;
701 int old_data_index
, new_data2_index
;
702 int old_mdebug_index
;
703 struct stat stat_buf
;
706 /* Open the old file, allocate a buffer of the right size, and read
707 * in the file contents. */
709 old_file
= open (old_name
, O_RDONLY
);
712 fatal ("Can't open %s for reading: errno %d\n", old_name
, errno
);
714 if (fstat (old_file
, &stat_buf
) == -1)
715 fatal ("Can't fstat (%s): errno %d\n", old_name
, errno
);
718 mmap_fd
= open ("/dev/zero", O_RDONLY
);
720 fatal ("Can't open /dev/zero for reading: errno %d\n", errno
);
723 /* We cannot use malloc here because that may use sbrk. If it does,
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
);
733 if (read (old_file
, old_base
, stat_buf
.st_size
) != stat_buf
.st_size
)
734 fatal ("Didn't read all of %s: errno %d\n", old_name
, errno
);
736 /* Get pointers to headers & section names */
738 old_file_h
= (ElfW(Ehdr
) *) old_base
;
739 old_program_h
= (ElfW(Phdr
) *) ((byte
*) old_base
+ old_file_h
->e_phoff
);
740 old_section_h
= (ElfW(Shdr
) *) ((byte
*) old_base
+ old_file_h
->e_shoff
);
741 old_section_names
= (char *) old_base
742 + OLD_SECTION_H (old_file_h
->e_shstrndx
).sh_offset
;
744 /* Find the mdebug section, if any. */
746 old_mdebug_index
= find_section (".mdebug", old_section_names
,
747 old_name
, old_file_h
, old_section_h
, 1);
749 /* Find the old .bss section. Figure out parameters of the new
750 * data2 and bss sections.
753 old_bss_index
= find_section (".bss", old_section_names
,
754 old_name
, old_file_h
, old_section_h
, 0);
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
)
762 if (old_sbss_index
== -1)
764 old_bss_addr
= OLD_SECTION_H (old_bss_index
).sh_addr
;
765 old_bss_size
= OLD_SECTION_H (old_bss_index
).sh_size
;
766 new_data2_index
= old_bss_index
;
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
;
776 /* Find the old .data section. Figure out parameters of
777 the new data2 and bss sections. */
779 old_data_index
= find_section (".data", old_section_names
,
780 old_name
, old_file_h
, old_section_h
, 0);
782 #if defined (emacs) || !defined (DEBUG)
783 new_bss_addr
= (ElfW(Addr
)) sbrk (0);
785 new_bss_addr
= old_bss_addr
+ old_bss_size
+ 0x1234;
787 new_data2_addr
= old_bss_addr
;
788 new_data2_size
= new_bss_addr
- old_bss_addr
;
789 new_data2_offset
= OLD_SECTION_H (old_data_index
).sh_offset
+
790 (new_data2_addr
- OLD_SECTION_H (old_data_index
).sh_addr
);
793 fprintf (stderr
, "old_bss_index %d\n", old_bss_index
);
794 fprintf (stderr
, "old_bss_addr %x\n", old_bss_addr
);
795 fprintf (stderr
, "old_bss_size %x\n", old_bss_size
);
796 fprintf (stderr
, "new_bss_addr %x\n", new_bss_addr
);
797 fprintf (stderr
, "new_data2_addr %x\n", new_data2_addr
);
798 fprintf (stderr
, "new_data2_size %x\n", new_data2_size
);
799 fprintf (stderr
, "new_data2_offset %x\n", new_data2_offset
);
802 if ((unsigned) new_bss_addr
< (unsigned) old_bss_addr
+ old_bss_size
)
803 fatal (".bss shrank when undumping???\n", 0, 0);
805 /* Set the output file to the right size. Allocate a buffer to hold
806 * the image of the new file. Set pointers to various interesting
807 * objects. stat_buf still has old_file data.
810 new_file
= open (new_name
, O_RDWR
| O_CREAT
, 0666);
812 fatal ("Can't creat (%s): errno %d\n", new_name
, errno
);
814 new_file_size
= stat_buf
.st_size
+ old_file_h
->e_shentsize
+ new_data2_size
;
816 if (ftruncate (new_file
, new_file_size
))
817 fatal ("Can't ftruncate (%s): errno %d\n", new_name
, errno
);
819 new_base
= mmap (NULL
, new_file_size
, PROT_READ
| PROT_WRITE
,
820 MAP_ANON
| MAP_PRIVATE
, mmap_fd
, 0);
821 if (new_base
== MAP_FAILED
)
822 fatal ("Can't allocate buffer for %s\n", old_name
);
824 new_file_h
= (ElfW(Ehdr
) *) new_base
;
825 new_program_h
= (ElfW(Phdr
) *) ((byte
*) new_base
+ old_file_h
->e_phoff
);
826 new_section_h
= (ElfW(Shdr
) *)
827 ((byte
*) new_base
+ old_file_h
->e_shoff
+ new_data2_size
);
829 /* Make our new file, program and section headers as copies of the
833 memcpy (new_file_h
, old_file_h
, old_file_h
->e_ehsize
);
834 memcpy (new_program_h
, old_program_h
,
835 old_file_h
->e_phnum
* old_file_h
->e_phentsize
);
837 /* Modify the e_shstrndx if necessary. */
838 PATCH_INDEX (new_file_h
->e_shstrndx
);
840 /* Fix up file header. We'll add one section. Section header is
844 new_file_h
->e_shoff
+= new_data2_size
;
845 new_file_h
->e_shnum
+= 1;
848 fprintf (stderr
, "Old section offset %x\n", old_file_h
->e_shoff
);
849 fprintf (stderr
, "Old section count %d\n", old_file_h
->e_shnum
);
850 fprintf (stderr
, "New section offset %x\n", new_file_h
->e_shoff
);
851 fprintf (stderr
, "New section count %d\n", new_file_h
->e_shnum
);
854 /* Fix up a new program header. Extend the writable data segment so
855 * that the bss area is covered too. Find that segment by looking
856 * for a segment that ends just before the .bss area. Make sure
857 * that no segments are above the new .data2. Put a loop at the end
858 * to adjust the offset and address of any segment that is above
859 * data2, just in case we decide to allow this later.
862 for (n
= new_file_h
->e_phnum
- 1; n
>= 0; n
--)
864 /* Compute maximum of all requirements for alignment of section. */
865 ElfW(Word
) alignment
= (NEW_PROGRAM_H (n
)).p_align
;
866 if ((OLD_SECTION_H (old_bss_index
)).sh_addralign
> alignment
)
867 alignment
= OLD_SECTION_H (old_bss_index
).sh_addralign
;
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)
876 if (NEW_PROGRAM_H (n
).p_vaddr
+ NEW_PROGRAM_H (n
).p_filesz
877 > (old_sbss_index
== -1
879 : round_up (old_bss_addr
, alignment
)))
880 fatal ("Program segment above .bss in %s\n", old_name
, 0);
882 if (NEW_PROGRAM_H (n
).p_type
== PT_LOAD
883 && (round_up ((NEW_PROGRAM_H (n
)).p_vaddr
884 + (NEW_PROGRAM_H (n
)).p_filesz
,
886 == round_up (old_bss_addr
, alignment
)))
890 fatal ("Couldn't find segment next to .bss in %s\n", old_name
, 0);
892 /* Make sure that the size includes any padding before the old .bss
894 NEW_PROGRAM_H (n
).p_filesz
= new_bss_addr
- NEW_PROGRAM_H (n
).p_vaddr
;
895 NEW_PROGRAM_H (n
).p_memsz
= NEW_PROGRAM_H (n
).p_filesz
;
897 #if 0 /* Maybe allow section after data2 - does this ever happen? */
898 for (n
= new_file_h
->e_phnum
- 1; n
>= 0; n
--)
900 if (NEW_PROGRAM_H (n
).p_vaddr
901 && NEW_PROGRAM_H (n
).p_vaddr
>= new_data2_addr
)
902 NEW_PROGRAM_H (n
).p_vaddr
+= new_data2_size
- old_bss_size
;
904 if (NEW_PROGRAM_H (n
).p_offset
>= new_data2_offset
)
905 NEW_PROGRAM_H (n
).p_offset
+= new_data2_size
;
909 /* Fix up section headers based on new .data2 section. Any section
910 * whose offset or virtual address is after the new .data2 section
911 * gets its value adjusted. .bss size becomes zero and new address
912 * is set. data2 section header gets added by copying the existing
913 * .data header and modifying the offset, address and size.
915 for (old_data_index
= 1; old_data_index
< (int) old_file_h
->e_shnum
;
917 if (!strcmp (old_section_names
+ OLD_SECTION_H (old_data_index
).sh_name
,
920 if (old_data_index
== old_file_h
->e_shnum
)
921 fatal ("Can't find .data in %s.\n", old_name
, 0);
923 /* Walk through all section headers, insert the new data2 section right
924 before the new bss section. */
925 for (n
= 1, nn
= 1; n
< (int) old_file_h
->e_shnum
; n
++, nn
++)
928 /* If it is (s)bss section, insert the new data2 section before it. */
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
)
933 /* Steal the data section header for this data2 section. */
934 memcpy (&NEW_SECTION_H (nn
), &OLD_SECTION_H (old_data_index
),
935 new_file_h
->e_shentsize
);
937 NEW_SECTION_H (nn
).sh_addr
= new_data2_addr
;
938 NEW_SECTION_H (nn
).sh_offset
= new_data2_offset
;
939 NEW_SECTION_H (nn
).sh_size
= new_data2_size
;
940 /* Use the bss section's alignment. This will assure that the
941 new data2 section always be placed in the same spot as the old
942 bss section by any other application. */
943 NEW_SECTION_H (nn
).sh_addralign
= OLD_SECTION_H (n
).sh_addralign
;
945 /* Now copy over what we have in the memory now. */
946 memcpy (NEW_SECTION_H (nn
).sh_offset
+ new_base
,
947 (caddr_t
) OLD_SECTION_H (n
).sh_addr
,
952 memcpy (&NEW_SECTION_H (nn
), &OLD_SECTION_H (n
),
953 old_file_h
->e_shentsize
);
955 if (n
== old_bss_index
956 /* The new bss and sbss section's size is zero, and its file offset
957 and virtual address should be off by NEW_DATA2_SIZE. */
958 || n
== old_sbss_index
961 /* NN should be `old_s?bss_index + 1' at this point. */
962 NEW_SECTION_H (nn
).sh_offset
=
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
;
966 /* Let the new bss section address alignment be the same as the
967 section address alignment followed the old bss section, so
968 this section will be placed in exactly the same place. */
969 NEW_SECTION_H (nn
).sh_addralign
= OLD_SECTION_H (nn
).sh_addralign
;
970 NEW_SECTION_H (nn
).sh_size
= 0;
974 /* Any section that was original placed AFTER the bss
975 section should now be off by NEW_DATA2_SIZE. */
976 #ifdef SOLARIS_POWERPC
977 /* On PPC Reference Platform running Solaris 2.5.1
978 the plt section is also of type NOBI like the bss section.
979 (not really stored) and therefore sections after the bss
980 section start at the plt offset. The plt section is always
981 the one just before the bss section.
982 It would be better to put the new data section before
983 the .plt section, or use libelf instead.
984 Erik Deumens, deumens@qtp.ufl.edu. */
985 if (NEW_SECTION_H (nn
).sh_offset
986 >= OLD_SECTION_H (old_bss_index
-1).sh_offset
)
987 NEW_SECTION_H (nn
).sh_offset
+= new_data2_size
;
989 if (round_up (NEW_SECTION_H (nn
).sh_offset
,
990 OLD_SECTION_H (old_bss_index
).sh_addralign
)
992 NEW_SECTION_H (nn
).sh_offset
+= new_data2_size
;
994 /* Any section that was originally placed after the section
995 header table should now be off by the size of one section
996 header table entry. */
997 if (NEW_SECTION_H (nn
).sh_offset
> new_file_h
->e_shoff
)
998 NEW_SECTION_H (nn
).sh_offset
+= new_file_h
->e_shentsize
;
1001 /* If any section hdr refers to the section after the new .data
1002 section, make it refer to next one because we have inserted
1003 a new section in between. */
1005 PATCH_INDEX (NEW_SECTION_H (nn
).sh_link
);
1006 /* For symbol tables, info is a symbol table index,
1007 so don't change it. */
1008 if (NEW_SECTION_H (nn
).sh_type
!= SHT_SYMTAB
1009 && NEW_SECTION_H (nn
).sh_type
!= SHT_DYNSYM
)
1010 PATCH_INDEX (NEW_SECTION_H (nn
).sh_info
);
1012 if (old_sbss_index
!= -1)
1013 if (!strcmp (old_section_names
+ NEW_SECTION_H (nn
).sh_name
, ".sbss"))
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
;
1021 /* Now, start to copy the content of sections. */
1022 if (NEW_SECTION_H (nn
).sh_type
== SHT_NULL
1023 || NEW_SECTION_H (nn
).sh_type
== SHT_NOBITS
)
1026 /* Write out the sections. .data and .data1 (and data2, called
1027 ".data" in the strings table) get copied from the current process
1028 instead of the old file. */
1029 if (!strcmp (old_section_names
+ NEW_SECTION_H (n
).sh_name
, ".data")
1030 || !strcmp ((old_section_names
+ NEW_SECTION_H (n
).sh_name
),
1032 || !strcmp ((old_section_names
+ NEW_SECTION_H (n
).sh_name
),
1034 || !strcmp ((old_section_names
+ NEW_SECTION_H (n
).sh_name
),
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 */
1050 || !strcmp ((old_section_names
+ NEW_SECTION_H (n
).sh_name
),
1053 || !strcmp ((old_section_names
+ NEW_SECTION_H (n
).sh_name
),
1055 || !strcmp ((old_section_names
+ NEW_SECTION_H (n
).sh_name
),
1057 || !strcmp ((old_section_names
+ NEW_SECTION_H (n
).sh_name
),
1059 src
= (caddr_t
) OLD_SECTION_H (n
).sh_addr
;
1061 src
= old_base
+ OLD_SECTION_H (n
).sh_offset
;
1063 memcpy (NEW_SECTION_H (nn
).sh_offset
+ new_base
, src
,
1064 NEW_SECTION_H (nn
).sh_size
);
1067 /* Update Alpha COFF symbol table: */
1068 if (strcmp (old_section_names
+ OLD_SECTION_H (n
).sh_name
, ".mdebug")
1071 pHDRR symhdr
= (pHDRR
) (NEW_SECTION_H (nn
).sh_offset
+ new_base
);
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
;
1085 #endif /* __alpha__ */
1087 #if defined (__sony_news) && defined (_SYSTYPE_SYSV)
1088 if (NEW_SECTION_H (nn
).sh_type
== SHT_MIPS_DEBUG
1089 && old_mdebug_index
!= -1)
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
);
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
;
1110 #endif /* __sony_news && _SYSTYPE_SYSV */
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
)
1123 #define MDEBUGADJUST(__ct,__fileaddr) \
1124 if (n_phdrr->__ct > 0) \
1126 n_phdrr->__fileaddr += movement; \
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
;
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)
1147 if (o_phdrr
->cbLineOffset
> (OLD_SECTION_H (n
).sh_offset
1148 + OLD_SECTION_H (n
).sh_size
))
1150 /* line data is in a hole in elf. do special copy and adjust
1151 for this ld mistake.
1153 n_phdrr
->cbLineOffset
+= movement
;
1155 memcpy (n_phdrr
->cbLineOffset
+ new_base
,
1156 o_phdrr
->cbLineOffset
+ old_base
, n_phdrr
->cbLine
);
1160 /* somehow line data is in .mdebug as it is supposed to be. */
1161 MDEBUGADJUST (cbLine
, cbLineOffset
);
1167 /* If it is the symbol table, its st_shndx field needs to be patched. */
1168 if (NEW_SECTION_H (nn
).sh_type
== SHT_SYMTAB
1169 || NEW_SECTION_H (nn
).sh_type
== SHT_DYNSYM
)
1171 ElfW(Shdr
) *spt
= &NEW_SECTION_H (nn
);
1172 unsigned int num
= spt
->sh_size
/ spt
->sh_entsize
;
1173 ElfW(Sym
) * sym
= (ElfW(Sym
) *) (NEW_SECTION_H (nn
).sh_offset
+
1175 for (; num
--; sym
++)
1177 if ((sym
->st_shndx
== SHN_UNDEF
)
1178 || (sym
->st_shndx
== SHN_ABS
)
1179 || (sym
->st_shndx
== SHN_COMMON
))
1182 PATCH_INDEX (sym
->st_shndx
);
1187 /* Update the symbol values of _edata and _end. */
1188 for (n
= new_file_h
->e_shnum
- 1; n
; n
--)
1191 ElfW(Sym
) *symp
, *symendp
;
1193 if (NEW_SECTION_H (n
).sh_type
!= SHT_DYNSYM
1194 && NEW_SECTION_H (n
).sh_type
!= SHT_SYMTAB
)
1197 symnames
= ((byte
*) new_base
1198 + NEW_SECTION_H (NEW_SECTION_H (n
).sh_link
).sh_offset
);
1199 symp
= (ElfW(Sym
) *) (NEW_SECTION_H (n
).sh_offset
+ new_base
);
1200 symendp
= (ElfW(Sym
) *) ((byte
*)symp
+ NEW_SECTION_H (n
).sh_size
);
1202 for (; symp
< symendp
; symp
++)
1203 if (strcmp ((char *) (symnames
+ symp
->st_name
), "_end") == 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)
1207 memcpy (&symp
->st_value
, &new_bss_addr
, sizeof (new_bss_addr
));
1210 /* This loop seeks out relocation sections for the data section, so
1211 that it can undo relocations performed by the runtime linker. */
1212 for (n
= new_file_h
->e_shnum
- 1; n
; n
--)
1214 ElfW(Shdr
) section
= NEW_SECTION_H (n
);
1215 switch (section
.sh_type
) {
1220 /* This code handles two different size structs, but there should
1221 be no harm in that provided that r_offset is always the first
1223 nn
= section
.sh_info
;
1224 if (!strcmp (old_section_names
+ NEW_SECTION_H (nn
).sh_name
, ".data")
1225 || !strcmp ((old_section_names
+ NEW_SECTION_H (nn
).sh_name
),
1227 || !strcmp ((old_section_names
+ NEW_SECTION_H (nn
).sh_name
),
1229 || !strcmp ((old_section_names
+ NEW_SECTION_H (nn
).sh_name
),
1231 #ifdef IRIX6_5 /* see above */
1232 || !strcmp ((old_section_names
+ NEW_SECTION_H (nn
).sh_name
),
1235 || !strcmp ((old_section_names
+ NEW_SECTION_H (nn
).sh_name
),
1237 || !strcmp ((old_section_names
+ NEW_SECTION_H (nn
).sh_name
),
1240 ElfW(Addr
) offset
= NEW_SECTION_H (nn
).sh_addr
-
1241 NEW_SECTION_H (nn
).sh_offset
;
1242 caddr_t reloc
= old_base
+ section
.sh_offset
, end
;
1243 for (end
= reloc
+ section
.sh_size
; reloc
< end
;
1244 reloc
+= section
.sh_entsize
)
1246 ElfW(Addr
) addr
= ((ElfW(Rel
) *) reloc
)->r_offset
- offset
;
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)
1254 memcpy (new_base
+ addr
, old_base
+ addr
, sizeof(ElfW(Addr
)));
1261 /* Write out new_file, close it, and free the buffer containing its
1264 if (write (new_file
, new_base
, new_file_size
) != new_file_size
)
1265 fatal ("Didn't write %d bytes to %s: errno %d\n",
1266 new_file_size
, new_base
, errno
);
1268 if (close (new_file
))
1269 fatal ("Can't close (%s): errno %d\n", new_name
, errno
);
1271 munmap (new_base
, new_file_size
);
1273 /* Close old_file, and free the corresponding buffer */
1279 if (close (old_file
))
1280 fatal ("Can't close (%s): errno %d\n", old_name
, errno
);
1282 munmap (old_base
, old_file_size
);
1284 /* Make the new file executable */
1286 if (stat (new_name
, &stat_buf
) == -1)
1287 fatal ("Can't stat (%s): errno %d\n", new_name
, errno
);
1291 stat_buf
.st_mode
|= 0111 & ~n
;
1292 if (chmod (new_name
, stat_buf
.st_mode
) == -1)
1293 fatal ("Can't chmod (%s): errno %d\n", new_name
, errno
);