1 /* Copyright (C) 1985-1988, 1990, 1992, 1999-2017 Free Software
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 3 of the License, or (at
9 your option) any later version.
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. If not, see <https://www.gnu.org/licenses/>. */
20 In other words, you are welcome to use, share and improve this program.
21 You are forbidden to forbid anyone else to use, share and improve
22 what you give them. Help stamp out software-hoarding! */
26 * unexec.c - Convert a running program into an a.out file.
28 * Author: Spencer W. Thomas
29 * Computer Science Dept.
31 * Date: Tue Mar 2 1982
32 * Modified heavily since then.
35 * unexec (const char *new_name, const char *old_name);
37 * Takes a snapshot of the program and makes an a.out format file in the
38 * file named by the string argument new_name.
39 * If old_name is non-NULL, the symbol table will be taken from the given file.
40 * On some machines, an existing old_name file is required.
44 /* We do not use mmap because that fails with NFS.
45 Instead we read the whole file, modify it, and write it out. */
58 #include <sys/types.h>
63 #elif !defined __NetBSD__ && !defined __OpenBSD__
67 #if defined (_SYSTYPE_SYSV)
68 #include <sys/elf_mips.h>
70 #endif /* _SYSTYPE_SYSV */
74 #define MAP_ANON MAP_ANONYMOUS
81 #define MAP_FAILED ((void *) -1)
84 #if defined (__alpha__) && !defined (__NetBSD__) && !defined (__OpenBSD__)
85 /* Declare COFF debugging symbol table. This used to be in
86 /usr/include/sym.h, but this file is no longer included in Red Hat
87 5.0 and presumably in any other glibc 2.x based distribution. */
115 #define cbHDRR sizeof (HDRR)
116 #define hdrNil ((pHDRR)0)
121 * NetBSD does not have normal-looking user-land ELF support.
123 # if defined __alpha__ || defined __sparc_v9__ || defined _LP64
128 # include <sys/exec_elf.h>
131 # define PT_LOAD Elf_pt_load
132 # if 0 /* was in pkgsrc patches for 20.7 */
133 # define SHT_PROGBITS Elf_sht_progbits
135 # define SHT_SYMTAB Elf_sht_symtab
136 # define SHT_DYNSYM Elf_sht_dynsym
137 # define SHT_NULL Elf_sht_null
138 # define SHT_NOBITS Elf_sht_nobits
139 # define SHT_REL Elf_sht_rel
140 # define SHT_RELA Elf_sht_rela
142 # define SHN_UNDEF Elf_eshn_undefined
143 # define SHN_ABS Elf_eshn_absolute
144 # define SHN_COMMON Elf_eshn_common
145 # endif /* !PT_LOAD */
148 # include <sys/exec_ecoff.h>
149 # define HDRR struct ecoff_symhdr
150 # define pHDRR HDRR *
151 # endif /* __alpha__ */
153 #ifdef __mips__ /* was in pkgsrc patches for 20.7 */
154 # define SHT_MIPS_DEBUG DT_MIPS_FLAGS
155 # define HDRR struct Elf_Shdr
156 #endif /* __mips__ */
157 #endif /* __NetBSD__ */
160 # include <sys/exec_elf.h>
163 #if __GNU_LIBRARY__ - 0 >= 6
164 # include <link.h> /* get ElfW etc */
168 # define ElfBitsW(bits, type) Elf##bits##_##type
176 /* This macro expands `bits' before invoking ElfBitsW. */
177 # define ElfExpandBitsW(bits, type) ElfBitsW (bits, type)
178 # define ElfW(type) ElfExpandBitsW (ELFSIZE, type)
181 /* The code often converts ElfW (Half) values like e_shentsize to ptrdiff_t;
182 check that this doesn't lose information. */
183 #include <intprops.h>
185 verify ((! TYPE_SIGNED (ElfW (Half
))
186 || PTRDIFF_MIN
<= TYPE_MINIMUM (ElfW (Half
)))
187 && TYPE_MAXIMUM (ElfW (Half
)) <= PTRDIFF_MAX
);
190 # define DEBUG_LOG(expr) fprintf (stderr, #expr " 0x%jx\n", (uintmax_t) (expr))
193 /* Get the address of a particular section or program header entry,
194 * accounting for the size of the entries.
198 entry_address (void *section_h
, ptrdiff_t idx
, ptrdiff_t entsize
)
201 return h
+ idx
* entsize
;
204 #define OLD_SECTION_H(n) \
205 (*(ElfW (Shdr) *) entry_address (old_section_h, n, old_file_h->e_shentsize))
206 #define NEW_SECTION_H(n) \
207 (*(ElfW (Shdr) *) entry_address (new_section_h, n, new_file_h->e_shentsize))
208 #define OLD_PROGRAM_H(n) \
209 (*(ElfW (Phdr) *) entry_address (old_program_h, n, old_file_h->e_phentsize))
211 typedef unsigned char byte
;
213 /* ****************************************************************
218 * In ELF, this works by replacing the old bss SHT_NOBITS section with
219 * a new, larger, SHT_PROGBITS section.
223 unexec (const char *new_name
, const char *old_name
)
225 int new_file
, old_file
;
228 /* Pointers to the base of the image of the two files. */
229 caddr_t old_base
, new_base
;
237 /* Pointers to the file, program and section headers for the old and
239 ElfW (Ehdr
) *old_file_h
, *new_file_h
;
240 ElfW (Phdr
) *old_program_h
, *new_program_h
;
241 ElfW (Shdr
) *old_section_h
, *new_section_h
;
243 /* Point to the section name table. */
244 char *old_section_names
, *new_section_names
;
246 ElfW (Phdr
) *old_bss_seg
, *new_bss_seg
;
247 ElfW (Addr
) old_bss_addr
, new_bss_addr
;
248 ElfW (Word
) old_bss_size
, bss_size_growth
, new_data2_size
;
249 ElfW (Off
) old_bss_offset
, new_data2_offset
;
252 ptrdiff_t old_bss_index
;
253 struct stat stat_buf
;
256 /* Open the old file, allocate a buffer of the right size, and read
257 in the file contents. */
259 old_file
= emacs_open (old_name
, O_RDONLY
, 0);
262 fatal ("Can't open %s for reading: %s", old_name
, strerror (errno
));
264 if (fstat (old_file
, &stat_buf
) != 0)
265 fatal ("Can't fstat (%s): %s", old_name
, strerror (errno
));
268 mmap_fd
= emacs_open ("/dev/zero", O_RDONLY
, 0);
270 fatal ("Can't open /dev/zero for reading: %s", strerror (errno
));
273 /* We cannot use malloc here because that may use sbrk. If it does,
274 we'd dump our temporary buffers with Emacs, and we'd have to be
275 extra careful to use the correct value of sbrk(0) after
276 allocating all buffers in the code below, which we aren't. */
277 old_file_size
= stat_buf
.st_size
;
278 if (! (0 <= old_file_size
&& old_file_size
<= SIZE_MAX
))
279 fatal ("File size out of range");
280 old_base
= mmap (NULL
, old_file_size
, PROT_READ
| PROT_WRITE
,
281 MAP_ANON
| MAP_PRIVATE
, mmap_fd
, 0);
282 if (old_base
== MAP_FAILED
)
283 fatal ("Can't allocate buffer for %s: %s", old_name
, strerror (errno
));
285 if (read (old_file
, old_base
, old_file_size
) != old_file_size
)
286 fatal ("Didn't read all of %s: %s", old_name
, strerror (errno
));
288 /* Get pointers to headers & section names */
290 old_file_h
= (ElfW (Ehdr
) *) old_base
;
291 old_program_h
= (ElfW (Phdr
) *) ((byte
*) old_base
+ old_file_h
->e_phoff
);
292 old_section_h
= (ElfW (Shdr
) *) ((byte
*) old_base
+ old_file_h
->e_shoff
);
293 old_section_names
= (char *) old_base
294 + OLD_SECTION_H (old_file_h
->e_shstrndx
).sh_offset
;
296 /* Find the PT_LOAD header covering the highest address. This
297 segment will be where bss sections are located, past p_filesz. */
299 for (n
= old_file_h
->e_phnum
; --n
>= 0; )
301 ElfW (Phdr
) *seg
= &OLD_PROGRAM_H (n
);
302 if (seg
->p_type
== PT_LOAD
304 || seg
->p_vaddr
> old_bss_seg
->p_vaddr
))
308 /* Note that old_bss_addr may be lower than the first bss section
309 address, since the section may need aligning. */
310 old_bss_addr
= old_bss_seg
->p_vaddr
+ old_bss_seg
->p_filesz
;
311 old_bss_offset
= old_bss_seg
->p_offset
+ old_bss_seg
->p_filesz
;
312 old_bss_size
= old_bss_seg
->p_memsz
- old_bss_seg
->p_filesz
;
314 /* Find the last bss style section in the bss segment range. */
316 for (n
= old_file_h
->e_shnum
; --n
> 0; )
318 ElfW (Shdr
) *shdr
= &OLD_SECTION_H (n
);
319 if (shdr
->sh_type
== SHT_NOBITS
320 && shdr
->sh_addr
>= old_bss_addr
321 && shdr
->sh_addr
+ shdr
->sh_size
<= old_bss_addr
+ old_bss_size
322 && (old_bss_index
== -1
323 || OLD_SECTION_H (old_bss_index
).sh_addr
< shdr
->sh_addr
))
327 if (old_bss_index
== -1)
328 fatal ("no bss section found");
330 void *no_break
= (void *) (intptr_t) -1;
331 void *new_break
= no_break
;
333 new_break
= sbrk (0);
335 if (new_break
== no_break
)
336 new_break
= (byte
*) old_bss_addr
+ old_bss_size
;
337 new_bss_addr
= (ElfW (Addr
)) new_break
;
338 bss_size_growth
= new_bss_addr
- old_bss_addr
;
339 new_data2_size
= bss_size_growth
;
340 new_data2_size
+= alignof (ElfW (Shdr
)) - 1;
341 new_data2_size
-= new_data2_size
% alignof (ElfW (Shdr
));
343 new_data2_offset
= old_bss_offset
;
346 fprintf (stderr
, "old_bss_index %td\n", old_bss_index
);
347 DEBUG_LOG (old_bss_addr
);
348 DEBUG_LOG (old_bss_size
);
349 DEBUG_LOG (old_bss_offset
);
350 DEBUG_LOG (new_bss_addr
);
351 DEBUG_LOG (new_data2_size
);
352 DEBUG_LOG (new_data2_offset
);
355 if (new_bss_addr
< old_bss_addr
+ old_bss_size
)
356 fatal (".bss shrank when undumping");
358 /* Set the output file to the right size. Allocate a buffer to hold
359 the image of the new file. Set pointers to various interesting
362 new_file
= emacs_open (new_name
, O_RDWR
| O_CREAT
, 0777);
364 fatal ("Can't creat (%s): %s", new_name
, strerror (errno
));
366 new_file_size
= old_file_size
+ new_data2_size
;
368 if (ftruncate (new_file
, new_file_size
))
369 fatal ("Can't ftruncate (%s): %s", new_name
, strerror (errno
));
371 new_base
= mmap (NULL
, new_file_size
, PROT_READ
| PROT_WRITE
,
372 MAP_ANON
| MAP_PRIVATE
, mmap_fd
, 0);
373 if (new_base
== MAP_FAILED
)
374 fatal ("Can't allocate buffer for %s: %s", old_name
, strerror (errno
));
376 /* Make our new file, program and section headers as copies of the
379 new_file_h
= (ElfW (Ehdr
) *) new_base
;
380 memcpy (new_file_h
, old_file_h
, old_file_h
->e_ehsize
);
382 /* Fix up file header. Section header is further away now. */
384 if (new_file_h
->e_shoff
>= old_bss_offset
)
385 new_file_h
->e_shoff
+= new_data2_size
;
387 new_program_h
= (ElfW (Phdr
) *) ((byte
*) new_base
+ new_file_h
->e_phoff
);
388 new_section_h
= (ElfW (Shdr
) *) ((byte
*) new_base
+ new_file_h
->e_shoff
);
390 memcpy (new_program_h
, old_program_h
,
391 old_file_h
->e_phnum
* old_file_h
->e_phentsize
);
392 memcpy (new_section_h
, old_section_h
,
393 old_file_h
->e_shnum
* old_file_h
->e_shentsize
);
396 DEBUG_LOG (old_file_h
->e_shoff
);
397 fprintf (stderr
, "Old section count %td\n", (ptrdiff_t) old_file_h
->e_shnum
);
398 DEBUG_LOG (new_file_h
->e_shoff
);
399 fprintf (stderr
, "New section count %td\n", (ptrdiff_t) new_file_h
->e_shnum
);
402 /* Fix up program header. Extend the writable data segment so
403 that the bss area is covered too. */
405 new_bss_seg
= new_program_h
+ (old_bss_seg
- old_program_h
);
406 new_bss_seg
->p_filesz
= new_bss_addr
- new_bss_seg
->p_vaddr
;
407 new_bss_seg
->p_memsz
= new_bss_seg
->p_filesz
;
409 /* Copy over what we have in memory now for the bss area. */
410 memcpy (new_base
+ new_data2_offset
, (caddr_t
) old_bss_addr
,
413 /* Walk through all section headers, copying data and updating. */
414 for (n
= 1; n
< old_file_h
->e_shnum
; n
++)
417 ElfW (Shdr
) *old_shdr
= &OLD_SECTION_H (n
);
418 ElfW (Shdr
) *new_shdr
= &NEW_SECTION_H (n
);
420 if (new_shdr
->sh_type
== SHT_NOBITS
421 && new_shdr
->sh_addr
>= old_bss_addr
422 && (new_shdr
->sh_addr
+ new_shdr
->sh_size
423 <= old_bss_addr
+ old_bss_size
))
425 /* This section now has file backing. */
426 new_shdr
->sh_type
= SHT_PROGBITS
;
428 /* SHT_NOBITS sections do not need a valid sh_offset, so it
429 might be incorrect. Write the correct value. */
430 new_shdr
->sh_offset
= (new_shdr
->sh_addr
- new_bss_seg
->p_vaddr
431 + new_bss_seg
->p_offset
);
433 /* If this is was a SHT_NOBITS .plt section, then it is
434 probably a PowerPC PLT. If it is PowerPC64 ELFv1 then
435 glibc ld.so doesn't initialize the toc pointer word. A
436 non-zero toc pointer word can defeat Power7 thread safety
437 during lazy update of a PLT entry. This only matters if
438 emacs becomes multi-threaded. */
439 if (strcmp (old_section_names
+ new_shdr
->sh_name
, ".plt") == 0)
440 memset (new_shdr
->sh_offset
+ new_base
, 0, new_shdr
->sh_size
);
442 /* Extend the size of the last bss section to cover dumped
444 if (n
== old_bss_index
)
445 new_shdr
->sh_size
= new_bss_addr
- new_shdr
->sh_addr
;
447 /* We have already copied this section from the current
452 /* Any section that was originally placed after the .bss
453 section should now be offset by NEW_DATA2_SIZE. */
454 if (new_shdr
->sh_offset
>= old_bss_offset
)
455 new_shdr
->sh_offset
+= new_data2_size
;
457 /* Now, start to copy the content of sections. */
458 if (new_shdr
->sh_type
== SHT_NULL
459 || new_shdr
->sh_type
== SHT_NOBITS
)
462 /* Some sections are copied from the current process instead of
464 if (!strcmp (old_section_names
+ new_shdr
->sh_name
, ".data")
465 || !strcmp (old_section_names
+ new_shdr
->sh_name
, ".sdata")
466 || !strcmp (old_section_names
+ new_shdr
->sh_name
, ".lit4")
467 || !strcmp (old_section_names
+ new_shdr
->sh_name
, ".lit8")
468 || !strcmp (old_section_names
+ new_shdr
->sh_name
, ".sdata1")
469 || !strcmp (old_section_names
+ new_shdr
->sh_name
, ".data1"))
470 src
= (caddr_t
) old_shdr
->sh_addr
;
472 src
= old_base
+ old_shdr
->sh_offset
;
474 memcpy (new_shdr
->sh_offset
+ new_base
, src
, new_shdr
->sh_size
);
476 #if (defined __alpha__ && !defined __OpenBSD__) || defined _SYSTYPE_SYSV
477 /* Update Alpha and MIPS COFF debug symbol table. */
478 if (strcmp (old_section_names
+ new_shdr
->sh_name
, ".mdebug") == 0
479 && new_shdr
->sh_offset
- old_shdr
->sh_offset
!= 0
480 #if defined _SYSTYPE_SYSV
481 && new_shdr
->sh_type
== SHT_MIPS_DEBUG
485 ptrdiff_t diff
= new_shdr
->sh_offset
- old_shdr
->sh_offset
;
486 HDRR
*phdr
= (HDRR
*) (new_shdr
->sh_offset
+ new_base
);
488 phdr
->cbLineOffset
+= diff
;
489 phdr
->cbDnOffset
+= diff
;
490 phdr
->cbPdOffset
+= diff
;
491 phdr
->cbSymOffset
+= diff
;
492 phdr
->cbOptOffset
+= diff
;
493 phdr
->cbAuxOffset
+= diff
;
494 phdr
->cbSsOffset
+= diff
;
495 phdr
->cbSsExtOffset
+= diff
;
496 phdr
->cbFdOffset
+= diff
;
497 phdr
->cbRfdOffset
+= diff
;
498 phdr
->cbExtOffset
+= diff
;
500 #endif /* __alpha__ || _SYSTYPE_SYSV */
503 /* Update the symbol values of _edata and _end. */
504 for (n
= new_file_h
->e_shnum
; 0 < --n
; )
507 ElfW (Sym
) *symp
, *symendp
;
508 ElfW (Shdr
) *sym_shdr
= &NEW_SECTION_H (n
);
510 if (sym_shdr
->sh_type
!= SHT_DYNSYM
511 && sym_shdr
->sh_type
!= SHT_SYMTAB
)
514 symnames
= ((byte
*) new_base
515 + NEW_SECTION_H (sym_shdr
->sh_link
).sh_offset
);
516 symp
= (ElfW (Sym
) *) (sym_shdr
->sh_offset
+ new_base
);
517 symendp
= (ElfW (Sym
) *) ((byte
*) symp
+ sym_shdr
->sh_size
);
519 for (; symp
< symendp
; symp
++)
521 if (strcmp ((char *) (symnames
+ symp
->st_name
), "_end") == 0
522 || strcmp ((char *) (symnames
+ symp
->st_name
), "end") == 0
523 || strcmp ((char *) (symnames
+ symp
->st_name
), "_edata") == 0
524 || strcmp ((char *) (symnames
+ symp
->st_name
), "edata") == 0)
525 memcpy (&symp
->st_value
, &new_bss_addr
, sizeof (new_bss_addr
));
527 /* Strictly speaking, #ifdef below is not necessary. But we
528 keep it to indicate that this kind of change may also be
529 necessary for other unexecs to support GNUstep. */
530 #ifdef NS_IMPL_GNUSTEP
531 /* ObjC runtime modifies the values of some data structures
532 such as classes and selectors in the .data section after
533 loading. As the dump process copies the .data section
534 from the current process, that causes problems when the
535 modified classes are reinitialized in the dumped
536 executable. We copy such data from the old file, not
537 from the current process. */
538 if (strncmp ((char *) (symnames
+ symp
->st_name
),
539 "_OBJC_", sizeof ("_OBJC_") - 1) == 0)
541 ElfW (Shdr
) *new_shdr
= &NEW_SECTION_H (symp
->st_shndx
);
542 if (new_shdr
->sh_type
!= SHT_NOBITS
)
544 ElfW (Shdr
) *old_shdr
= &OLD_SECTION_H (symp
->st_shndx
);
545 ptrdiff_t reladdr
= symp
->st_value
- new_shdr
->sh_addr
;
546 ptrdiff_t newoff
= reladdr
+ new_shdr
->sh_offset
;
548 if (old_shdr
->sh_type
== SHT_NOBITS
)
549 memset (new_base
+ newoff
, 0, symp
->st_size
);
552 ptrdiff_t oldoff
= reladdr
+ old_shdr
->sh_offset
;
553 memcpy (new_base
+ newoff
, old_base
+ oldoff
,
562 /* Modify the names of sections we changed from SHT_NOBITS to
563 SHT_PROGBITS. This is really just cosmetic, but some tools that
564 (wrongly) operate on section names rather than types might be
565 confused by a SHT_PROGBITS .bss section. */
566 new_section_names
= ((char *) new_base
567 + NEW_SECTION_H (new_file_h
->e_shstrndx
).sh_offset
);
568 for (n
= new_file_h
->e_shnum
; 0 < --n
; )
570 ElfW (Shdr
) *old_shdr
= &OLD_SECTION_H (n
);
571 ElfW (Shdr
) *new_shdr
= &NEW_SECTION_H (n
);
573 /* Replace the leading '.' with ','. When .shstrtab is string
574 merged this will rename both .bss and .rela.bss to ,bss and
576 if (old_shdr
->sh_type
== SHT_NOBITS
577 && new_shdr
->sh_type
== SHT_PROGBITS
)
578 *(new_section_names
+ new_shdr
->sh_name
) = ',';
581 /* This loop seeks out relocation sections for the data section, so
582 that it can undo relocations performed by the runtime loader.
584 The following approach does not work on x86 platforms that use
585 the GNU Gold linker, which can generate .rel.dyn relocation
586 sections containing R_386_32 entries that the following code does
587 not grok. Emacs works around this problem by avoiding C
588 constructs that generate such entries, which is horrible hack.
590 FIXME: Presumably more problems like this will crop up as linkers
591 get fancier. We really need to stop assuming that Emacs can grok
592 arbitrary linker output. See Bug#27248. */
593 for (n
= new_file_h
->e_shnum
; 0 < --n
; )
595 ElfW (Shdr
) *rel_shdr
= &NEW_SECTION_H (n
);
598 switch (rel_shdr
->sh_type
)
604 /* This code handles two different size structs, but there should
605 be no harm in that provided that r_offset is always the first
607 shdr
= &NEW_SECTION_H (rel_shdr
->sh_info
);
608 if (!strcmp (old_section_names
+ shdr
->sh_name
, ".data")
609 || !strcmp (old_section_names
+ shdr
->sh_name
, ".sdata")
610 || !strcmp (old_section_names
+ shdr
->sh_name
, ".lit4")
611 || !strcmp (old_section_names
+ shdr
->sh_name
, ".lit8")
612 || !strcmp (old_section_names
+ shdr
->sh_name
, ".sdata1")
613 || !strcmp (old_section_names
+ shdr
->sh_name
, ".data1"))
615 ElfW (Addr
) offset
= shdr
->sh_addr
- shdr
->sh_offset
;
616 caddr_t reloc
= old_base
+ rel_shdr
->sh_offset
, end
;
617 for (end
= reloc
+ rel_shdr
->sh_size
;
619 reloc
+= rel_shdr
->sh_entsize
)
621 ElfW (Addr
) addr
= ((ElfW (Rel
) *) reloc
)->r_offset
- offset
;
622 /* Ignore R_*_NONE relocs. */
623 if (((ElfW (Rel
) *) reloc
)->r_offset
== 0)
625 /* Assume reloc applies to a word.
626 ??? This is not always true, eg. TLS module/index
627 pair in .got which occupies two words. */
628 memcpy (new_base
+ addr
, old_base
+ addr
,
629 sizeof (ElfW (Addr
)));
636 /* Write out new_file, and free the buffers. */
638 if (write (new_file
, new_base
, new_file_size
) != new_file_size
)
639 fatal ("Didn't write %lu bytes to %s: %s",
640 (unsigned long) new_file_size
, new_name
, strerror (errno
));
641 munmap (old_base
, old_file_size
);
642 munmap (new_base
, new_file_size
);
644 /* Close the files and make the new file executable. */
647 emacs_close (mmap_fd
);
650 if (emacs_close (old_file
) != 0)
651 fatal ("Can't close (%s): %s", old_name
, strerror (errno
));
653 if (emacs_close (new_file
) != 0)
654 fatal ("Can't close (%s): %s", new_name
, strerror (errno
));