]> code.delx.au - gnu-emacs/blob - src/unexelf.c
03e6dafe8ee1b6790e402d1d80687e90df11d972
[gnu-emacs] / src / unexelf.c
1 /* Copyright (C) 1985-1988, 1990, 1992, 1999-2015 Free Software
2 Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
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
9 (at your option) any later version.
10
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.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
18
19 /*
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! */
23
24
25 /*
26 * unexec.c - Convert a running program into an a.out file.
27 *
28 * Author: Spencer W. Thomas
29 * Computer Science Dept.
30 * University of Utah
31 * Date: Tue Mar 2 1982
32 * Modified heavily since then.
33 *
34 * Synopsis:
35 * unexec (const char *new_name, const char *old_name);
36 *
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.
41 *
42 */
43 \f
44 /* We do not use mmap because that fails with NFS.
45 Instead we read the whole file, modify it, and write it out. */
46
47 #include <config.h>
48 #include "unexec.h"
49 #include "lisp.h"
50
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <limits.h>
54 #include <memory.h>
55 #include <stdint.h>
56 #include <stdio.h>
57 #include <sys/stat.h>
58 #include <sys/types.h>
59 #include <unistd.h>
60
61 #if !defined (__NetBSD__) && !defined (__OpenBSD__)
62 #include <elf.h>
63 #endif /* not __NetBSD__ and not __OpenBSD__ */
64 #include <sys/mman.h>
65 #if defined (_SYSTYPE_SYSV)
66 #include <sys/elf_mips.h>
67 #include <sym.h>
68 #endif /* _SYSTYPE_SYSV */
69 #if __sgi
70 #include <syms.h> /* for HDRR declaration */
71 #endif /* __sgi */
72
73 #ifndef MAP_ANON
74 #ifdef MAP_ANONYMOUS
75 #define MAP_ANON MAP_ANONYMOUS
76 #else
77 #define MAP_ANON 0
78 #endif
79 #endif
80
81 #ifndef MAP_FAILED
82 #define MAP_FAILED ((void *) -1)
83 #endif
84
85 #if defined (__alpha__) && !defined (__NetBSD__) && !defined (__OpenBSD__)
86 /* Declare COFF debugging symbol table. This used to be in
87 /usr/include/sym.h, but this file is no longer included in Red Hat
88 5.0 and presumably in any other glibc 2.x based distribution. */
89 typedef struct {
90 short magic;
91 short vstamp;
92 int ilineMax;
93 int idnMax;
94 int ipdMax;
95 int isymMax;
96 int ioptMax;
97 int iauxMax;
98 int issMax;
99 int issExtMax;
100 int ifdMax;
101 int crfd;
102 int iextMax;
103 long cbLine;
104 long cbLineOffset;
105 long cbDnOffset;
106 long cbPdOffset;
107 long cbSymOffset;
108 long cbOptOffset;
109 long cbAuxOffset;
110 long cbSsOffset;
111 long cbSsExtOffset;
112 long cbFdOffset;
113 long cbRfdOffset;
114 long cbExtOffset;
115 } HDRR, *pHDRR;
116 #define cbHDRR sizeof (HDRR)
117 #define hdrNil ((pHDRR)0)
118 #endif
119
120 #ifdef __NetBSD__
121 /*
122 * NetBSD does not have normal-looking user-land ELF support.
123 */
124 # if defined __alpha__ || defined __sparc_v9__ || defined _LP64
125 # define ELFSIZE 64
126 # else
127 # define ELFSIZE 32
128 # endif
129 # include <sys/exec_elf.h>
130
131 # ifndef PT_LOAD
132 # define PT_LOAD Elf_pt_load
133 # if 0 /* was in pkgsrc patches for 20.7 */
134 # define SHT_PROGBITS Elf_sht_progbits
135 # endif
136 # define SHT_SYMTAB Elf_sht_symtab
137 # define SHT_DYNSYM Elf_sht_dynsym
138 # define SHT_NULL Elf_sht_null
139 # define SHT_NOBITS Elf_sht_nobits
140 # define SHT_REL Elf_sht_rel
141 # define SHT_RELA Elf_sht_rela
142
143 # define SHN_UNDEF Elf_eshn_undefined
144 # define SHN_ABS Elf_eshn_absolute
145 # define SHN_COMMON Elf_eshn_common
146 # endif /* !PT_LOAD */
147
148 # ifdef __alpha__
149 # include <sys/exec_ecoff.h>
150 # define HDRR struct ecoff_symhdr
151 # define pHDRR HDRR *
152 # endif /* __alpha__ */
153
154 #ifdef __mips__ /* was in pkgsrc patches for 20.7 */
155 # define SHT_MIPS_DEBUG DT_MIPS_FLAGS
156 # define HDRR struct Elf_Shdr
157 #endif /* __mips__ */
158 #endif /* __NetBSD__ */
159
160 #ifdef __OpenBSD__
161 # include <sys/exec_elf.h>
162 #endif
163
164 #if __GNU_LIBRARY__ - 0 >= 6
165 # include <link.h> /* get ElfW etc */
166 #endif
167
168 #ifndef ElfW
169 # define ElfBitsW(bits, type) Elf##bits##_##type
170 # ifndef ELFSIZE
171 # ifdef _LP64
172 # define ELFSIZE 64
173 # else
174 # define ELFSIZE 32
175 # endif
176 # endif
177 /* This macro expands `bits' before invoking ElfBitsW. */
178 # define ElfExpandBitsW(bits, type) ElfBitsW (bits, type)
179 # define ElfW(type) ElfExpandBitsW (ELFSIZE, type)
180 #endif
181
182 /* The code often converts ElfW (Half) values like e_shentsize to ptrdiff_t;
183 check that this doesn't lose information. */
184 #include <intprops.h>
185 #include <verify.h>
186 verify ((! TYPE_SIGNED (ElfW (Half))
187 || PTRDIFF_MIN <= TYPE_MINIMUM (ElfW (Half)))
188 && TYPE_MAXIMUM (ElfW (Half)) <= PTRDIFF_MAX);
189
190 #ifdef UNEXELF_DEBUG
191 # define DEBUG_LOG(expr) fprintf (stderr, #expr " 0x%jx\n", (uintmax_t) (expr))
192 #endif
193
194 /* Get the address of a particular section or program header entry,
195 * accounting for the size of the entries.
196 */
197
198 static void *
199 entry_address (void *section_h, ptrdiff_t idx, ptrdiff_t entsize)
200 {
201 char *h = section_h;
202 return h + idx * entsize;
203 }
204
205 #define OLD_SECTION_H(n) \
206 (*(ElfW (Shdr) *) entry_address (old_section_h, n, old_file_h->e_shentsize))
207 #define NEW_SECTION_H(n) \
208 (*(ElfW (Shdr) *) entry_address (new_section_h, n, new_file_h->e_shentsize))
209 #define OLD_PROGRAM_H(n) \
210 (*(ElfW (Phdr) *) entry_address (old_program_h, n, old_file_h->e_phentsize))
211
212 typedef unsigned char byte;
213
214 /* ****************************************************************
215 * unexec
216 *
217 * driving logic.
218 *
219 * In ELF, this works by replacing the old bss SHT_NOBITS section with
220 * a new, larger, SHT_PROGBITS section.
221 *
222 */
223 void
224 unexec (const char *new_name, const char *old_name)
225 {
226 int new_file, old_file;
227 off_t new_file_size;
228 void *new_break;
229
230 /* Pointers to the base of the image of the two files. */
231 caddr_t old_base, new_base;
232
233 #if MAP_ANON == 0
234 int mmap_fd;
235 #else
236 # define mmap_fd -1
237 #endif
238
239 /* Pointers to the file, program and section headers for the old and
240 new files. */
241 ElfW (Ehdr) *old_file_h, *new_file_h;
242 ElfW (Phdr) *old_program_h, *new_program_h;
243 ElfW (Shdr) *old_section_h, *new_section_h;
244
245 /* Point to the section name table. */
246 char *old_section_names, *new_section_names;
247
248 ElfW (Phdr) *old_bss_seg, *new_bss_seg;
249 ElfW (Addr) old_bss_addr, new_bss_addr;
250 ElfW (Word) old_bss_size, bss_size_growth, new_data2_size;
251 ElfW (Off) old_bss_offset, new_data2_offset;
252
253 ptrdiff_t n;
254 ptrdiff_t old_bss_index;
255 struct stat stat_buf;
256 off_t old_file_size;
257
258 /* Open the old file, allocate a buffer of the right size, and read
259 in the file contents. */
260
261 old_file = emacs_open (old_name, O_RDONLY, 0);
262
263 if (old_file < 0)
264 fatal ("Can't open %s for reading: %s", old_name, strerror (errno));
265
266 if (fstat (old_file, &stat_buf) != 0)
267 fatal ("Can't fstat (%s): %s", old_name, strerror (errno));
268
269 #if MAP_ANON == 0
270 mmap_fd = emacs_open ("/dev/zero", O_RDONLY, 0);
271 if (mmap_fd < 0)
272 fatal ("Can't open /dev/zero for reading: %s", strerror (errno));
273 #endif
274
275 /* We cannot use malloc here because that may use sbrk. If it does,
276 we'd dump our temporary buffers with Emacs, and we'd have to be
277 extra careful to use the correct value of sbrk(0) after
278 allocating all buffers in the code below, which we aren't. */
279 old_file_size = stat_buf.st_size;
280 if (! (0 <= old_file_size && old_file_size <= SIZE_MAX))
281 fatal ("File size out of range");
282 old_base = mmap (NULL, old_file_size, PROT_READ | PROT_WRITE,
283 MAP_ANON | MAP_PRIVATE, mmap_fd, 0);
284 if (old_base == MAP_FAILED)
285 fatal ("Can't allocate buffer for %s: %s", old_name, strerror (errno));
286
287 if (read (old_file, old_base, old_file_size) != old_file_size)
288 fatal ("Didn't read all of %s: %s", old_name, strerror (errno));
289
290 /* Get pointers to headers & section names */
291
292 old_file_h = (ElfW (Ehdr) *) old_base;
293 old_program_h = (ElfW (Phdr) *) ((byte *) old_base + old_file_h->e_phoff);
294 old_section_h = (ElfW (Shdr) *) ((byte *) old_base + old_file_h->e_shoff);
295 old_section_names = (char *) old_base
296 + OLD_SECTION_H (old_file_h->e_shstrndx).sh_offset;
297
298 /* Find the PT_LOAD header covering the highest address. This
299 segment will be where bss sections are located, past p_filesz. */
300 old_bss_seg = 0;
301 for (n = old_file_h->e_phnum; --n >= 0; )
302 {
303 ElfW (Phdr) *seg = &OLD_PROGRAM_H (n);
304 if (seg->p_type == PT_LOAD
305 && (old_bss_seg == 0
306 || seg->p_vaddr > old_bss_seg->p_vaddr))
307 old_bss_seg = seg;
308 }
309
310 /* Note that old_bss_addr may be lower than the first bss section
311 address, since the section may need aligning. */
312 old_bss_addr = old_bss_seg->p_vaddr + old_bss_seg->p_filesz;
313 old_bss_offset = old_bss_seg->p_offset + old_bss_seg->p_filesz;
314 old_bss_size = old_bss_seg->p_memsz - old_bss_seg->p_filesz;
315
316 /* Find the last bss style section in the bss segment range. */
317 old_bss_index = -1;
318 for (n = old_file_h->e_shnum; --n > 0; )
319 {
320 ElfW (Shdr) *shdr = &OLD_SECTION_H (n);
321 if (shdr->sh_type == SHT_NOBITS
322 && shdr->sh_addr >= old_bss_addr
323 && shdr->sh_addr + shdr->sh_size <= old_bss_addr + old_bss_size
324 && (old_bss_index == -1
325 || OLD_SECTION_H (old_bss_index).sh_addr < shdr->sh_addr))
326 old_bss_index = n;
327 }
328
329 if (old_bss_index == -1)
330 fatal ("no bss section found");
331
332 new_break = sbrk (0);
333 new_bss_addr = (ElfW (Addr)) new_break;
334 bss_size_growth = new_bss_addr - old_bss_addr;
335 new_data2_size = bss_size_growth;
336 new_data2_size += alignof (ElfW (Shdr)) - 1;
337 new_data2_size -= new_data2_size % alignof (ElfW (Shdr));
338
339 new_data2_offset = old_bss_offset;
340
341 #ifdef UNEXELF_DEBUG
342 fprintf (stderr, "old_bss_index %td\n", old_bss_index);
343 DEBUG_LOG (old_bss_addr);
344 DEBUG_LOG (old_bss_size);
345 DEBUG_LOG (old_bss_offset);
346 DEBUG_LOG (new_bss_addr);
347 DEBUG_LOG (new_data2_size);
348 DEBUG_LOG (new_data2_offset);
349 #endif
350
351 if (new_bss_addr < old_bss_addr + old_bss_size)
352 fatal (".bss shrank when undumping");
353
354 /* Set the output file to the right size. Allocate a buffer to hold
355 the image of the new file. Set pointers to various interesting
356 objects. */
357
358 new_file = emacs_open (new_name, O_RDWR | O_CREAT, 0777);
359 if (new_file < 0)
360 fatal ("Can't creat (%s): %s", new_name, strerror (errno));
361
362 new_file_size = old_file_size + new_data2_size;
363
364 if (ftruncate (new_file, new_file_size))
365 fatal ("Can't ftruncate (%s): %s", new_name, strerror (errno));
366
367 new_base = mmap (NULL, new_file_size, PROT_READ | PROT_WRITE,
368 MAP_ANON | MAP_PRIVATE, mmap_fd, 0);
369 if (new_base == MAP_FAILED)
370 fatal ("Can't allocate buffer for %s: %s", old_name, strerror (errno));
371
372 /* Make our new file, program and section headers as copies of the
373 originals. */
374
375 new_file_h = (ElfW (Ehdr) *) new_base;
376 memcpy (new_file_h, old_file_h, old_file_h->e_ehsize);
377
378 /* Fix up file header. Section header is further away now. */
379
380 if (new_file_h->e_shoff >= old_bss_offset)
381 new_file_h->e_shoff += new_data2_size;
382
383 new_program_h = (ElfW (Phdr) *) ((byte *) new_base + new_file_h->e_phoff);
384 new_section_h = (ElfW (Shdr) *) ((byte *) new_base + new_file_h->e_shoff);
385
386 memcpy (new_program_h, old_program_h,
387 old_file_h->e_phnum * old_file_h->e_phentsize);
388 memcpy (new_section_h, old_section_h,
389 old_file_h->e_shnum * old_file_h->e_shentsize);
390
391 #ifdef UNEXELF_DEBUG
392 DEBUG_LOG (old_file_h->e_shoff);
393 fprintf (stderr, "Old section count %td\n", (ptrdiff_t) old_file_h->e_shnum);
394 DEBUG_LOG (new_file_h->e_shoff);
395 fprintf (stderr, "New section count %td\n", (ptrdiff_t) new_file_h->e_shnum);
396 #endif
397
398 /* Fix up program header. Extend the writable data segment so
399 that the bss area is covered too. */
400
401 new_bss_seg = new_program_h + (old_bss_seg - old_program_h);
402 new_bss_seg->p_filesz = new_bss_addr - new_bss_seg->p_vaddr;
403 new_bss_seg->p_memsz = new_bss_seg->p_filesz;
404
405 /* Copy over what we have in memory now for the bss area. */
406 memcpy (new_base + new_data2_offset, (caddr_t) old_bss_addr,
407 bss_size_growth);
408
409 /* Walk through all section headers, copying data and updating. */
410 for (n = 1; n < old_file_h->e_shnum; n++)
411 {
412 caddr_t src;
413 ElfW (Shdr) *old_shdr = &OLD_SECTION_H (n);
414 ElfW (Shdr) *new_shdr = &NEW_SECTION_H (n);
415
416 if (new_shdr->sh_type == SHT_NOBITS
417 && new_shdr->sh_addr >= old_bss_addr
418 && (new_shdr->sh_addr + new_shdr->sh_size
419 <= old_bss_addr + old_bss_size))
420 {
421 /* This section now has file backing. */
422 new_shdr->sh_type = SHT_PROGBITS;
423
424 /* SHT_NOBITS sections do not need a valid sh_offset, so it
425 might be incorrect. Write the correct value. */
426 new_shdr->sh_offset = (new_shdr->sh_addr - new_bss_seg->p_vaddr
427 + new_bss_seg->p_offset);
428
429 /* If this is was a SHT_NOBITS .plt section, then it is
430 probably a PowerPC PLT. If it is PowerPC64 ELFv1 then
431 glibc ld.so doesn't initialize the toc pointer word. A
432 non-zero toc pointer word can defeat Power7 thread safety
433 during lazy update of a PLT entry. This only matters if
434 emacs becomes multi-threaded. */
435 if (strcmp (old_section_names + new_shdr->sh_name, ".plt") == 0)
436 memset (new_shdr->sh_offset + new_base, 0, new_shdr->sh_size);
437
438 /* Extend the size of the last bss section to cover dumped
439 data. */
440 if (n == old_bss_index)
441 new_shdr->sh_size = new_bss_addr - new_shdr->sh_addr;
442
443 /* We have already copied this section from the current
444 process. */
445 continue;
446 }
447
448 /* Any section that was originally placed after the .bss
449 section should now be offset by NEW_DATA2_SIZE. */
450 if (new_shdr->sh_offset >= old_bss_offset)
451 new_shdr->sh_offset += new_data2_size;
452
453 /* Now, start to copy the content of sections. */
454 if (new_shdr->sh_type == SHT_NULL
455 || new_shdr->sh_type == SHT_NOBITS)
456 continue;
457
458 /* Some sections are copied from the current process instead of
459 the old file. */
460 if (!strcmp (old_section_names + new_shdr->sh_name, ".data")
461 || !strcmp (old_section_names + new_shdr->sh_name, ".sdata")
462 || !strcmp (old_section_names + new_shdr->sh_name, ".lit4")
463 || !strcmp (old_section_names + new_shdr->sh_name, ".lit8")
464 /* The conditional bit below was in Oliva's original code
465 (1999-08-25) and seems to have been dropped by mistake
466 subsequently. It prevents a crash at startup under X in
467 `IRIX64 6.5 6.5.17m', whether compiled on that release or
468 an earlier one. It causes no trouble on the other ELF
469 platforms I could test (Irix 6.5.15m, Solaris 8, Debian
470 Potato x86, Debian Woody SPARC); however, it's reported
471 to cause crashes under some version of GNU/Linux. It's
472 not yet clear what's changed in that Irix version to
473 cause the problem, or why the fix sometimes fails under
474 GNU/Linux. There's probably no good reason to have
475 something Irix-specific here, but this will have to do
476 for now. IRIX6_5 is the most specific macro we have to
477 test. -- fx 2002-10-01
478
479 The issue _looks_ as though it's gone away on 6.5.18m,
480 but maybe it's still lurking, to be triggered by some
481 change in the binary. It appears to concern the dynamic
482 loader, but I never got anywhere with an SGI support call
483 seeking clues. -- fx 2002-11-29. */
484 #ifdef IRIX6_5
485 || !strcmp (old_section_names + new_shdr->sh_name, ".got")
486 #endif
487 || !strcmp (old_section_names + new_shdr->sh_name, ".sdata1")
488 || !strcmp (old_section_names + new_shdr->sh_name, ".data1"))
489 src = (caddr_t) old_shdr->sh_addr;
490 else
491 src = old_base + old_shdr->sh_offset;
492
493 memcpy (new_shdr->sh_offset + new_base, src, new_shdr->sh_size);
494
495 #if (defined __alpha__ && !defined __OpenBSD__) || defined _SYSTYPE_SYSV
496 /* Update Alpha and MIPS COFF debug symbol table. */
497 if (strcmp (old_section_names + new_shdr->sh_name, ".mdebug") == 0
498 && new_shdr->sh_offset - old_shdr->sh_offset != 0
499 #if defined _SYSTYPE_SYSV
500 && new_shdr->sh_type == SHT_MIPS_DEBUG
501 #endif
502 )
503 {
504 ptrdiff_t diff = new_shdr->sh_offset - old_shdr->sh_offset;
505 HDRR *phdr = (HDRR *) (new_shdr->sh_offset + new_base);
506
507 phdr->cbLineOffset += diff;
508 phdr->cbDnOffset += diff;
509 phdr->cbPdOffset += diff;
510 phdr->cbSymOffset += diff;
511 phdr->cbOptOffset += diff;
512 phdr->cbAuxOffset += diff;
513 phdr->cbSsOffset += diff;
514 phdr->cbSsExtOffset += diff;
515 phdr->cbFdOffset += diff;
516 phdr->cbRfdOffset += diff;
517 phdr->cbExtOffset += diff;
518 }
519 #endif /* __alpha__ || _SYSTYPE_SYSV */
520
521 #if __sgi
522 /* Adjust the HDRR offsets in .mdebug and copy the
523 line data if it's in its usual 'hole' in the object.
524 Makes the new file debuggable with dbx.
525 patches up two problems: the absolute file offsets
526 in the HDRR record of .mdebug (see /usr/include/syms.h), and
527 the ld bug that gets the line table in a hole in the
528 elf file rather than in the .mdebug section proper.
529 David Anderson. davea@sgi.com Jan 16,1994. */
530 if (strcmp (old_section_names + new_shdr->sh_name, ".mdebug") == 0
531 && new_shdr->sh_offset - old_shdr->sh_offset != 0)
532 {
533 #define MDEBUGADJUST(__ct,__fileaddr) \
534 if (n_phdrr->__ct > 0) \
535 { \
536 n_phdrr->__fileaddr += movement; \
537 }
538
539 HDRR *o_phdrr = (HDRR *) ((byte *) old_base + old_shdr->sh_offset);
540 HDRR *n_phdrr = (HDRR *) ((byte *) new_base + new_shdr->sh_offset);
541 ptrdiff_t movement = new_shdr->sh_offset - old_shdr->sh_offset;
542
543 MDEBUGADJUST (idnMax, cbDnOffset);
544 MDEBUGADJUST (ipdMax, cbPdOffset);
545 MDEBUGADJUST (isymMax, cbSymOffset);
546 MDEBUGADJUST (ioptMax, cbOptOffset);
547 MDEBUGADJUST (iauxMax, cbAuxOffset);
548 MDEBUGADJUST (issMax, cbSsOffset);
549 MDEBUGADJUST (issExtMax, cbSsExtOffset);
550 MDEBUGADJUST (ifdMax, cbFdOffset);
551 MDEBUGADJUST (crfd, cbRfdOffset);
552 MDEBUGADJUST (iextMax, cbExtOffset);
553 /* The Line Section, being possible off in a hole of the object,
554 requires special handling. */
555 if (n_phdrr->cbLine > 0)
556 {
557 n_phdrr->cbLineOffset += movement;
558
559 if (o_phdrr->cbLineOffset > (old_shdr->sh_offset
560 + old_shdr->sh_size))
561 /* If not covered by section, it hasn't yet been copied. */
562 memcpy (n_phdrr->cbLineOffset + new_base,
563 o_phdrr->cbLineOffset + old_base, n_phdrr->cbLine);
564 }
565 }
566 #endif /* __sgi */
567 }
568
569 /* Update the symbol values of _edata and _end. */
570 for (n = new_file_h->e_shnum; 0 < --n; )
571 {
572 byte *symnames;
573 ElfW (Sym) *symp, *symendp;
574 ElfW (Shdr) *sym_shdr = &NEW_SECTION_H (n);
575
576 if (sym_shdr->sh_type != SHT_DYNSYM
577 && sym_shdr->sh_type != SHT_SYMTAB)
578 continue;
579
580 symnames = ((byte *) new_base
581 + NEW_SECTION_H (sym_shdr->sh_link).sh_offset);
582 symp = (ElfW (Sym) *) (sym_shdr->sh_offset + new_base);
583 symendp = (ElfW (Sym) *) ((byte *) symp + sym_shdr->sh_size);
584
585 for (; symp < symendp; symp ++)
586 {
587 if (strcmp ((char *) (symnames + symp->st_name), "_end") == 0
588 || strcmp ((char *) (symnames + symp->st_name), "end") == 0
589 || strcmp ((char *) (symnames + symp->st_name), "_edata") == 0
590 || strcmp ((char *) (symnames + symp->st_name), "edata") == 0)
591 memcpy (&symp->st_value, &new_bss_addr, sizeof (new_bss_addr));
592
593 /* Strictly speaking, #ifdef below is not necessary. But we
594 keep it to indicate that this kind of change may also be
595 necessary for other unexecs to support GNUstep. */
596 #ifdef NS_IMPL_GNUSTEP
597 /* ObjC runtime modifies the values of some data structures
598 such as classes and selectors in the .data section after
599 loading. As the dump process copies the .data section
600 from the current process, that causes problems when the
601 modified classes are reinitialized in the dumped
602 executable. We copy such data from the old file, not
603 from the current process. */
604 if (strncmp ((char *) (symnames + symp->st_name),
605 "_OBJC_", sizeof ("_OBJC_") - 1) == 0)
606 {
607 ElfW (Shdr) *new_shdr = &NEW_SECTION_H (symp->st_shndx);
608 if (new_shdr->sh_type != SHT_NOBITS)
609 {
610 ElfW (Shdr) *old_shdr = &OLD_SECTION_H (symp->st_shndx);
611 ptrdiff_t reladdr = symp->st_value - new_shdr->sh_addr;
612 ptrdiff_t newoff = reladdr + new_shdr->sh_offset;
613
614 if (old_shdr->sh_type == SHT_NOBITS)
615 memset (new_base + newoff, 0, symp->st_size);
616 else
617 {
618 ptrdiff_t oldoff = reladdr + old_shdr->sh_offset;
619 memcpy (new_base + newoff, old_base + oldoff,
620 symp->st_size);
621 }
622 }
623 }
624 #endif
625 }
626 }
627
628 /* Modify the names of sections we changed from SHT_NOBITS to
629 SHT_PROGBITS. This is really just cosmetic, but some tools that
630 (wrongly) operate on section names rather than types might be
631 confused by a SHT_PROGBITS .bss section. */
632 new_section_names = ((char *) new_base
633 + NEW_SECTION_H (new_file_h->e_shstrndx).sh_offset);
634 for (n = new_file_h->e_shnum; 0 < --n; )
635 {
636 ElfW (Shdr) *old_shdr = &OLD_SECTION_H (n);
637 ElfW (Shdr) *new_shdr = &NEW_SECTION_H (n);
638
639 /* Replace the leading '.' with ','. When .shstrtab is string
640 merged this will rename both .bss and .rela.bss to ,bss and
641 .rela,bss. */
642 if (old_shdr->sh_type == SHT_NOBITS
643 && new_shdr->sh_type == SHT_PROGBITS)
644 *(new_section_names + new_shdr->sh_name) = ',';
645 }
646
647 /* This loop seeks out relocation sections for the data section, so
648 that it can undo relocations performed by the runtime loader. */
649 for (n = new_file_h->e_shnum; 0 < --n; )
650 {
651 ElfW (Shdr) *rel_shdr = &NEW_SECTION_H (n);
652 ElfW (Shdr) *shdr;
653
654 switch (rel_shdr->sh_type)
655 {
656 default:
657 break;
658 case SHT_REL:
659 case SHT_RELA:
660 /* This code handles two different size structs, but there should
661 be no harm in that provided that r_offset is always the first
662 member. */
663 shdr = &NEW_SECTION_H (rel_shdr->sh_info);
664 if (!strcmp (old_section_names + shdr->sh_name, ".data")
665 || !strcmp (old_section_names + shdr->sh_name, ".sdata")
666 || !strcmp (old_section_names + shdr->sh_name, ".lit4")
667 || !strcmp (old_section_names + shdr->sh_name, ".lit8")
668 #ifdef IRIX6_5 /* see above */
669 || !strcmp (old_section_names + shdr->sh_name, ".got")
670 #endif
671 || !strcmp (old_section_names + shdr->sh_name, ".sdata1")
672 || !strcmp (old_section_names + shdr->sh_name, ".data1"))
673 {
674 ElfW (Addr) offset = shdr->sh_addr - shdr->sh_offset;
675 caddr_t reloc = old_base + rel_shdr->sh_offset, end;
676 for (end = reloc + rel_shdr->sh_size;
677 reloc < end;
678 reloc += rel_shdr->sh_entsize)
679 {
680 ElfW (Addr) addr = ((ElfW (Rel) *) reloc)->r_offset - offset;
681 /* Ignore R_*_NONE relocs. */
682 if (((ElfW (Rel) *) reloc)->r_offset == 0)
683 continue;
684 /* Assume reloc applies to a word.
685 ??? This is not always true, eg. TLS module/index
686 pair in .got which occupies two words. */
687 memcpy (new_base + addr, old_base + addr,
688 sizeof (ElfW (Addr)));
689 }
690 }
691 break;
692 }
693 }
694
695 /* Write out new_file, and free the buffers. */
696
697 if (write (new_file, new_base, new_file_size) != new_file_size)
698 fatal ("Didn't write %lu bytes to %s: %s",
699 (unsigned long) new_file_size, new_name, strerror (errno));
700 munmap (old_base, old_file_size);
701 munmap (new_base, new_file_size);
702
703 /* Close the files and make the new file executable. */
704
705 #if MAP_ANON == 0
706 emacs_close (mmap_fd);
707 #endif
708
709 if (emacs_close (old_file) != 0)
710 fatal ("Can't close (%s): %s", old_name, strerror (errno));
711
712 if (emacs_close (new_file) != 0)
713 fatal ("Can't close (%s): %s", new_name, strerror (errno));
714 }