(syms_of_ntproc) <w32-get-true-file-attributes>: Doc fix.
[bpt/emacs.git] / src / unexelf.c
1 /* Copyright (C) 1985, 1986, 1987, 1988, 1990, 1992, 1999, 2000, 2001,
2 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
21
22 In other words, you are welcome to use, share and improve this program.
23 You are forbidden to forbid anyone else to use, share and improve
24 what you give them. Help stamp out software-hoarding! */
25
26
27 /*
28 * unexec.c - Convert a running program into an a.out file.
29 *
30 * Author: Spencer W. Thomas
31 * Computer Science Dept.
32 * University of Utah
33 * Date: Tue Mar 2 1982
34 * Modified heavily since then.
35 *
36 * Synopsis:
37 * unexec (new_name, old_name, data_start, bss_start, entry_address)
38 * char *new_name, *old_name;
39 * unsigned data_start, bss_start, entry_address;
40 *
41 * Takes a snapshot of the program and makes an a.out format file in the
42 * file named by the string argument new_name.
43 * If old_name is non-NULL, the symbol table will be taken from the given file.
44 * On some machines, an existing old_name file is required.
45 *
46 * The boundaries within the a.out file may be adjusted with the data_start
47 * and bss_start arguments. Either or both may be given as 0 for defaults.
48 *
49 * Data_start gives the boundary between the text segment and the data
50 * segment of the program. The text segment can contain shared, read-only
51 * program code and literal data, while the data segment is always unshared
52 * and unprotected. Data_start gives the lowest unprotected address.
53 * The value you specify may be rounded down to a suitable boundary
54 * as required by the machine you are using.
55 *
56 * Bss_start indicates how much of the data segment is to be saved in the
57 * a.out file and restored when the program is executed. It gives the lowest
58 * unsaved address, and is rounded up to a page boundary. The default when 0
59 * is given assumes that the entire data segment is to be stored, including
60 * the previous data and bss as well as any additional storage allocated with
61 * break (2).
62 *
63 * The new file is set up to start at entry_address.
64 *
65 */
66
67 /* Even more heavily modified by james@bigtex.cactus.org of Dell Computer Co.
68 * ELF support added.
69 *
70 * Basic theory: the data space of the running process needs to be
71 * dumped to the output file. Normally we would just enlarge the size
72 * of .data, scooting everything down. But we can't do that in ELF,
73 * because there is often something between the .data space and the
74 * .bss space.
75 *
76 * In the temacs dump below, notice that the Global Offset Table
77 * (.got) and the Dynamic link data (.dynamic) come between .data1 and
78 * .bss. It does not work to overlap .data with these fields.
79 *
80 * The solution is to create a new .data segment. This segment is
81 * filled with data from the current process. Since the contents of
82 * various sections refer to sections by index, the new .data segment
83 * is made the last in the table to avoid changing any existing index.
84
85 * This is an example of how the section headers are changed. "Addr"
86 * is a process virtual address. "Offset" is a file offset.
87
88 raid:/nfs/raid/src/dist-18.56/src> dump -h temacs
89
90 temacs:
91
92 **** SECTION HEADER TABLE ****
93 [No] Type Flags Addr Offset Size Name
94 Link Info Adralgn Entsize
95
96 [1] 1 2 0x80480d4 0xd4 0x13 .interp
97 0 0 0x1 0
98
99 [2] 5 2 0x80480e8 0xe8 0x388 .hash
100 3 0 0x4 0x4
101
102 [3] 11 2 0x8048470 0x470 0x7f0 .dynsym
103 4 1 0x4 0x10
104
105 [4] 3 2 0x8048c60 0xc60 0x3ad .dynstr
106 0 0 0x1 0
107
108 [5] 9 2 0x8049010 0x1010 0x338 .rel.plt
109 3 7 0x4 0x8
110
111 [6] 1 6 0x8049348 0x1348 0x3 .init
112 0 0 0x4 0
113
114 [7] 1 6 0x804934c 0x134c 0x680 .plt
115 0 0 0x4 0x4
116
117 [8] 1 6 0x80499cc 0x19cc 0x3c56f .text
118 0 0 0x4 0
119
120 [9] 1 6 0x8085f3c 0x3df3c 0x3 .fini
121 0 0 0x4 0
122
123 [10] 1 2 0x8085f40 0x3df40 0x69c .rodata
124 0 0 0x4 0
125
126 [11] 1 2 0x80865dc 0x3e5dc 0xd51 .rodata1
127 0 0 0x4 0
128
129 [12] 1 3 0x8088330 0x3f330 0x20afc .data
130 0 0 0x4 0
131
132 [13] 1 3 0x80a8e2c 0x5fe2c 0x89d .data1
133 0 0 0x4 0
134
135 [14] 1 3 0x80a96cc 0x606cc 0x1a8 .got
136 0 0 0x4 0x4
137
138 [15] 6 3 0x80a9874 0x60874 0x80 .dynamic
139 4 0 0x4 0x8
140
141 [16] 8 3 0x80a98f4 0x608f4 0x449c .bss
142 0 0 0x4 0
143
144 [17] 2 0 0 0x608f4 0x9b90 .symtab
145 18 371 0x4 0x10
146
147 [18] 3 0 0 0x6a484 0x8526 .strtab
148 0 0 0x1 0
149
150 [19] 3 0 0 0x729aa 0x93 .shstrtab
151 0 0 0x1 0
152
153 [20] 1 0 0 0x72a3d 0x68b7 .comment
154 0 0 0x1 0
155
156 raid:/nfs/raid/src/dist-18.56/src> dump -h xemacs
157
158 xemacs:
159
160 **** SECTION HEADER TABLE ****
161 [No] Type Flags Addr Offset Size Name
162 Link Info Adralgn Entsize
163
164 [1] 1 2 0x80480d4 0xd4 0x13 .interp
165 0 0 0x1 0
166
167 [2] 5 2 0x80480e8 0xe8 0x388 .hash
168 3 0 0x4 0x4
169
170 [3] 11 2 0x8048470 0x470 0x7f0 .dynsym
171 4 1 0x4 0x10
172
173 [4] 3 2 0x8048c60 0xc60 0x3ad .dynstr
174 0 0 0x1 0
175
176 [5] 9 2 0x8049010 0x1010 0x338 .rel.plt
177 3 7 0x4 0x8
178
179 [6] 1 6 0x8049348 0x1348 0x3 .init
180 0 0 0x4 0
181
182 [7] 1 6 0x804934c 0x134c 0x680 .plt
183 0 0 0x4 0x4
184
185 [8] 1 6 0x80499cc 0x19cc 0x3c56f .text
186 0 0 0x4 0
187
188 [9] 1 6 0x8085f3c 0x3df3c 0x3 .fini
189 0 0 0x4 0
190
191 [10] 1 2 0x8085f40 0x3df40 0x69c .rodata
192 0 0 0x4 0
193
194 [11] 1 2 0x80865dc 0x3e5dc 0xd51 .rodata1
195 0 0 0x4 0
196
197 [12] 1 3 0x8088330 0x3f330 0x20afc .data
198 0 0 0x4 0
199
200 [13] 1 3 0x80a8e2c 0x5fe2c 0x89d .data1
201 0 0 0x4 0
202
203 [14] 1 3 0x80a96cc 0x606cc 0x1a8 .got
204 0 0 0x4 0x4
205
206 [15] 6 3 0x80a9874 0x60874 0x80 .dynamic
207 4 0 0x4 0x8
208
209 [16] 8 3 0x80c6800 0x7d800 0 .bss
210 0 0 0x4 0
211
212 [17] 2 0 0 0x7d800 0x9b90 .symtab
213 18 371 0x4 0x10
214
215 [18] 3 0 0 0x87390 0x8526 .strtab
216 0 0 0x1 0
217
218 [19] 3 0 0 0x8f8b6 0x93 .shstrtab
219 0 0 0x1 0
220
221 [20] 1 0 0 0x8f949 0x68b7 .comment
222 0 0 0x1 0
223
224 [21] 1 3 0x80a98f4 0x608f4 0x1cf0c .data
225 0 0 0x4 0
226
227 * This is an example of how the file header is changed. "Shoff" is
228 * the section header offset within the file. Since that table is
229 * after the new .data section, it is moved. "Shnum" is the number of
230 * sections, which we increment.
231 *
232 * "Phoff" is the file offset to the program header. "Phentsize" and
233 * "Shentsz" are the program and section header entries sizes respectively.
234 * These can be larger than the apparent struct sizes.
235
236 raid:/nfs/raid/src/dist-18.56/src> dump -f temacs
237
238 temacs:
239
240 **** ELF HEADER ****
241 Class Data Type Machine Version
242 Entry Phoff Shoff Flags Ehsize
243 Phentsize Phnum Shentsz Shnum Shstrndx
244
245 1 1 2 3 1
246 0x80499cc 0x34 0x792f4 0 0x34
247 0x20 5 0x28 21 19
248
249 raid:/nfs/raid/src/dist-18.56/src> dump -f xemacs
250
251 xemacs:
252
253 **** ELF HEADER ****
254 Class Data Type Machine Version
255 Entry Phoff Shoff Flags Ehsize
256 Phentsize Phnum Shentsz Shnum Shstrndx
257
258 1 1 2 3 1
259 0x80499cc 0x34 0x96200 0 0x34
260 0x20 5 0x28 22 19
261
262 * These are the program headers. "Offset" is the file offset to the
263 * segment. "Vaddr" is the memory load address. "Filesz" is the
264 * segment size as it appears in the file, and "Memsz" is the size in
265 * memory. Below, the third segment is the code and the fourth is the
266 * data: the difference between Filesz and Memsz is .bss
267
268 raid:/nfs/raid/src/dist-18.56/src> dump -o temacs
269
270 temacs:
271 ***** PROGRAM EXECUTION HEADER *****
272 Type Offset Vaddr Paddr
273 Filesz Memsz Flags Align
274
275 6 0x34 0x8048034 0
276 0xa0 0xa0 5 0
277
278 3 0xd4 0 0
279 0x13 0 4 0
280
281 1 0x34 0x8048034 0
282 0x3f2f9 0x3f2f9 5 0x1000
283
284 1 0x3f330 0x8088330 0
285 0x215c4 0x25a60 7 0x1000
286
287 2 0x60874 0x80a9874 0
288 0x80 0 7 0
289
290 raid:/nfs/raid/src/dist-18.56/src> dump -o xemacs
291
292 xemacs:
293 ***** PROGRAM EXECUTION HEADER *****
294 Type Offset Vaddr Paddr
295 Filesz Memsz Flags Align
296
297 6 0x34 0x8048034 0
298 0xa0 0xa0 5 0
299
300 3 0xd4 0 0
301 0x13 0 4 0
302
303 1 0x34 0x8048034 0
304 0x3f2f9 0x3f2f9 5 0x1000
305
306 1 0x3f330 0x8088330 0
307 0x3e4d0 0x3e4d0 7 0x1000
308
309 2 0x60874 0x80a9874 0
310 0x80 0 7 0
311
312
313 */
314 \f
315 /* Modified by wtien@urbana.mcd.mot.com of Motorola Inc.
316 *
317 * The above mechanism does not work if the unexeced ELF file is being
318 * re-layout by other applications (such as `strip'). All the applications
319 * that re-layout the internal of ELF will layout all sections in ascending
320 * order of their file offsets. After the re-layout, the data2 section will
321 * still be the LAST section in the section header vector, but its file offset
322 * is now being pushed far away down, and causes part of it not to be mapped
323 * in (ie. not covered by the load segment entry in PHDR vector), therefore
324 * causes the new binary to fail.
325 *
326 * The solution is to modify the unexec algorithm to insert the new data2
327 * section header right before the new bss section header, so their file
328 * offsets will be in the ascending order. Since some of the section's (all
329 * sections AFTER the bss section) indexes are now changed, we also need to
330 * modify some fields to make them point to the right sections. This is done
331 * by macro PATCH_INDEX. All the fields that need to be patched are:
332 *
333 * 1. ELF header e_shstrndx field.
334 * 2. section header sh_link and sh_info field.
335 * 3. symbol table entry st_shndx field.
336 *
337 * The above example now should look like:
338
339 **** SECTION HEADER TABLE ****
340 [No] Type Flags Addr Offset Size Name
341 Link Info Adralgn Entsize
342
343 [1] 1 2 0x80480d4 0xd4 0x13 .interp
344 0 0 0x1 0
345
346 [2] 5 2 0x80480e8 0xe8 0x388 .hash
347 3 0 0x4 0x4
348
349 [3] 11 2 0x8048470 0x470 0x7f0 .dynsym
350 4 1 0x4 0x10
351
352 [4] 3 2 0x8048c60 0xc60 0x3ad .dynstr
353 0 0 0x1 0
354
355 [5] 9 2 0x8049010 0x1010 0x338 .rel.plt
356 3 7 0x4 0x8
357
358 [6] 1 6 0x8049348 0x1348 0x3 .init
359 0 0 0x4 0
360
361 [7] 1 6 0x804934c 0x134c 0x680 .plt
362 0 0 0x4 0x4
363
364 [8] 1 6 0x80499cc 0x19cc 0x3c56f .text
365 0 0 0x4 0
366
367 [9] 1 6 0x8085f3c 0x3df3c 0x3 .fini
368 0 0 0x4 0
369
370 [10] 1 2 0x8085f40 0x3df40 0x69c .rodata
371 0 0 0x4 0
372
373 [11] 1 2 0x80865dc 0x3e5dc 0xd51 .rodata1
374 0 0 0x4 0
375
376 [12] 1 3 0x8088330 0x3f330 0x20afc .data
377 0 0 0x4 0
378
379 [13] 1 3 0x80a8e2c 0x5fe2c 0x89d .data1
380 0 0 0x4 0
381
382 [14] 1 3 0x80a96cc 0x606cc 0x1a8 .got
383 0 0 0x4 0x4
384
385 [15] 6 3 0x80a9874 0x60874 0x80 .dynamic
386 4 0 0x4 0x8
387
388 [16] 1 3 0x80a98f4 0x608f4 0x1cf0c .data
389 0 0 0x4 0
390
391 [17] 8 3 0x80c6800 0x7d800 0 .bss
392 0 0 0x4 0
393
394 [18] 2 0 0 0x7d800 0x9b90 .symtab
395 19 371 0x4 0x10
396
397 [19] 3 0 0 0x87390 0x8526 .strtab
398 0 0 0x1 0
399
400 [20] 3 0 0 0x8f8b6 0x93 .shstrtab
401 0 0 0x1 0
402
403 [21] 1 0 0 0x8f949 0x68b7 .comment
404 0 0 0x1 0
405
406 */
407 \f
408 /* We do not use mmap because that fails with NFS.
409 Instead we read the whole file, modify it, and write it out. */
410
411 #ifndef emacs
412 #define fatal(a, b, c) fprintf (stderr, a, b, c), exit (1)
413 #include <string.h>
414 #else
415 #include <config.h>
416 extern void fatal (const char *msgid, ...);
417 #endif
418
419 #include <sys/types.h>
420 #include <stdio.h>
421 #include <sys/stat.h>
422 #include <memory.h>
423 #include <errno.h>
424 #include <unistd.h>
425 #include <fcntl.h>
426 #if !defined (__NetBSD__) && !defined (__OpenBSD__)
427 #include <elf.h>
428 #endif
429 #include <sys/mman.h>
430 #if defined (__sony_news) && defined (_SYSTYPE_SYSV)
431 #include <sys/elf_mips.h>
432 #include <sym.h>
433 #endif /* __sony_news && _SYSTYPE_SYSV */
434 #if __sgi
435 #include <syms.h> /* for HDRR declaration */
436 #endif /* __sgi */
437 #ifdef BROKEN_NOCOMBRELOC
438 #include <assert.h>
439 #endif
440
441 #ifndef MAP_ANON
442 #ifdef MAP_ANONYMOUS
443 #define MAP_ANON MAP_ANONYMOUS
444 #else
445 #define MAP_ANON 0
446 #endif
447 #endif
448
449 #ifndef MAP_FAILED
450 #define MAP_FAILED ((void *) -1)
451 #endif
452
453 #if defined (__alpha__) && !defined (__NetBSD__) && !defined (__OpenBSD__)
454 /* Declare COFF debugging symbol table. This used to be in
455 /usr/include/sym.h, but this file is no longer included in Red Hat
456 5.0 and presumably in any other glibc 2.x based distribution. */
457 typedef struct {
458 short magic;
459 short vstamp;
460 int ilineMax;
461 int idnMax;
462 int ipdMax;
463 int isymMax;
464 int ioptMax;
465 int iauxMax;
466 int issMax;
467 int issExtMax;
468 int ifdMax;
469 int crfd;
470 int iextMax;
471 long cbLine;
472 long cbLineOffset;
473 long cbDnOffset;
474 long cbPdOffset;
475 long cbSymOffset;
476 long cbOptOffset;
477 long cbAuxOffset;
478 long cbSsOffset;
479 long cbSsExtOffset;
480 long cbFdOffset;
481 long cbRfdOffset;
482 long cbExtOffset;
483 } HDRR, *pHDRR;
484 #define cbHDRR sizeof(HDRR)
485 #define hdrNil ((pHDRR)0)
486 #endif
487
488 #ifdef __NetBSD__
489 /*
490 * NetBSD does not have normal-looking user-land ELF support.
491 */
492 # if defined __alpha__ || defined __sparc_v9__
493 # define ELFSIZE 64
494 # else
495 # define ELFSIZE 32
496 # endif
497 # include <sys/exec_elf.h>
498
499 # ifndef PT_LOAD
500 # define PT_LOAD Elf_pt_load
501 # if 0 /* was in pkgsrc patches for 20.7 */
502 # define SHT_PROGBITS Elf_sht_progbits
503 # endif
504 # define SHT_SYMTAB Elf_sht_symtab
505 # define SHT_DYNSYM Elf_sht_dynsym
506 # define SHT_NULL Elf_sht_null
507 # define SHT_NOBITS Elf_sht_nobits
508 # define SHT_REL Elf_sht_rel
509 # define SHT_RELA Elf_sht_rela
510
511 # define SHN_UNDEF Elf_eshn_undefined
512 # define SHN_ABS Elf_eshn_absolute
513 # define SHN_COMMON Elf_eshn_common
514 # endif /* !PT_LOAD */
515
516 # ifdef __alpha__
517 # include <sys/exec_ecoff.h>
518 # define HDRR struct ecoff_symhdr
519 # define pHDRR HDRR *
520 # endif /* __alpha__ */
521
522 #ifdef __mips__ /* was in pkgsrc patches for 20.7 */
523 # define SHT_MIPS_DEBUG DT_MIPS_FLAGS
524 # define HDRR struct Elf_Shdr
525 #endif /* __mips__ */
526 #endif /* __NetBSD__ */
527
528 #ifdef __OpenBSD__
529 # include <sys/exec_elf.h>
530 #endif
531
532 #if __GNU_LIBRARY__ - 0 >= 6
533 # include <link.h> /* get ElfW etc */
534 #endif
535
536 #ifndef ElfW
537 # ifdef __STDC__
538 # define ElfBitsW(bits, type) Elf##bits##_##type
539 # else
540 # define ElfBitsW(bits, type) Elf/**/bits/**/_/**/type
541 # endif
542 # ifdef _LP64
543 # define ELFSIZE 64
544 # else
545 # define ELFSIZE 32
546 # endif
547 /* This macro expands `bits' before invoking ElfBitsW. */
548 # define ElfExpandBitsW(bits, type) ElfBitsW (bits, type)
549 # define ElfW(type) ElfExpandBitsW (ELFSIZE, type)
550 #endif
551
552 #ifndef ELF_BSS_SECTION_NAME
553 #define ELF_BSS_SECTION_NAME ".bss"
554 #endif
555
556 /* Get the address of a particular section or program header entry,
557 * accounting for the size of the entries.
558 */
559 /*
560 On PPC Reference Platform running Solaris 2.5.1
561 the plt section is also of type NOBI like the bss section.
562 (not really stored) and therefore sections after the bss
563 section start at the plt offset. The plt section is always
564 the one just before the bss section.
565 Thus, we modify the test from
566 if (NEW_SECTION_H (nn).sh_offset >= new_data2_offset)
567 to
568 if (NEW_SECTION_H (nn).sh_offset >=
569 OLD_SECTION_H (old_bss_index-1).sh_offset)
570 This is just a hack. We should put the new data section
571 before the .plt section.
572 And we should not have this routine at all but use
573 the libelf library to read the old file and create the new
574 file.
575 The changed code is minimal and depends on prep set in m/prep.h
576 Erik Deumens
577 Quantum Theory Project
578 University of Florida
579 deumens@qtp.ufl.edu
580 Apr 23, 1996
581 */
582
583 #define OLD_SECTION_H(n) \
584 (*(ElfW(Shdr) *) ((byte *) old_section_h + old_file_h->e_shentsize * (n)))
585 #define NEW_SECTION_H(n) \
586 (*(ElfW(Shdr) *) ((byte *) new_section_h + new_file_h->e_shentsize * (n)))
587 #define OLD_PROGRAM_H(n) \
588 (*(ElfW(Phdr) *) ((byte *) old_program_h + old_file_h->e_phentsize * (n)))
589 #define NEW_PROGRAM_H(n) \
590 (*(ElfW(Phdr) *) ((byte *) new_program_h + new_file_h->e_phentsize * (n)))
591
592 #define PATCH_INDEX(n) \
593 do { \
594 if ((int) (n) >= old_bss_index) \
595 (n)++; } while (0)
596 typedef unsigned char byte;
597
598 /* Round X up to a multiple of Y. */
599
600 static ElfW(Addr)
601 round_up (x, y)
602 ElfW(Addr) x, y;
603 {
604 int rem = x % y;
605 if (rem == 0)
606 return x;
607 return x - rem + y;
608 }
609
610 /* Return the index of the section named NAME.
611 SECTION_NAMES, FILE_NAME and FILE_H give information
612 about the file we are looking in.
613
614 If we don't find the section NAME, that is a fatal error
615 if NOERROR is 0; we return -1 if NOERROR is nonzero. */
616
617 static int
618 find_section (name, section_names, file_name, old_file_h, old_section_h, noerror)
619 char *name;
620 char *section_names;
621 char *file_name;
622 ElfW(Ehdr) *old_file_h;
623 ElfW(Shdr) *old_section_h;
624 int noerror;
625 {
626 int idx;
627
628 for (idx = 1; idx < old_file_h->e_shnum; idx++)
629 {
630 #ifdef DEBUG
631 fprintf (stderr, "Looking for %s - found %s\n", name,
632 section_names + OLD_SECTION_H (idx).sh_name);
633 #endif
634 if (!strcmp (section_names + OLD_SECTION_H (idx).sh_name,
635 name))
636 break;
637 }
638 if (idx == old_file_h->e_shnum)
639 {
640 if (noerror)
641 return -1;
642 else
643 fatal ("Can't find %s in %s.\n", name, file_name);
644 }
645
646 return idx;
647 }
648
649 /* ****************************************************************
650 * unexec
651 *
652 * driving logic.
653 *
654 * In ELF, this works by replacing the old .bss section with a new
655 * .data section, and inserting an empty .bss immediately afterwards.
656 *
657 */
658 void
659 unexec (new_name, old_name, data_start, bss_start, entry_address)
660 char *new_name, *old_name;
661 unsigned data_start, bss_start, entry_address;
662 {
663 int new_file, old_file, new_file_size;
664
665 /* Pointers to the base of the image of the two files. */
666 caddr_t old_base, new_base;
667
668 #if MAP_ANON == 0
669 int mmap_fd;
670 #else
671 # define mmap_fd -1
672 #endif
673
674 /* Pointers to the file, program and section headers for the old and
675 new files. */
676 ElfW(Ehdr) *old_file_h, *new_file_h;
677 ElfW(Phdr) *old_program_h, *new_program_h;
678 ElfW(Shdr) *old_section_h, *new_section_h;
679
680 /* Point to the section name table in the old file. */
681 char *old_section_names;
682
683 ElfW(Addr) old_bss_addr, new_bss_addr;
684 ElfW(Word) old_bss_size, new_data2_size;
685 ElfW(Off) new_data2_offset;
686 ElfW(Addr) new_data2_addr;
687
688 int n, nn;
689 int old_bss_index, old_sbss_index, old_plt_index;
690 int old_data_index, new_data2_index;
691 int old_mdebug_index;
692 struct stat stat_buf;
693 int old_file_size;
694 #ifdef BROKEN_NOCOMBRELOC
695 int unreloc_sections[10], n_unreloc_sections;
696 #endif
697
698 /* Open the old file, allocate a buffer of the right size, and read
699 in the file contents. */
700
701 old_file = open (old_name, O_RDONLY);
702
703 if (old_file < 0)
704 fatal ("Can't open %s for reading: errno %d\n", old_name, errno);
705
706 if (fstat (old_file, &stat_buf) == -1)
707 fatal ("Can't fstat (%s): errno %d\n", old_name, errno);
708
709 #if MAP_ANON == 0
710 mmap_fd = open ("/dev/zero", O_RDONLY);
711 if (mmap_fd < 0)
712 fatal ("Can't open /dev/zero for reading: errno %d\n", errno, 0);
713 #endif
714
715 /* We cannot use malloc here because that may use sbrk. If it does,
716 we'd dump our temporary buffers with Emacs, and we'd have to be
717 extra careful to use the correct value of sbrk(0) after
718 allocating all buffers in the code below, which we aren't. */
719 old_file_size = stat_buf.st_size;
720 old_base = mmap (NULL, old_file_size, PROT_READ | PROT_WRITE,
721 MAP_ANON | MAP_PRIVATE, mmap_fd, 0);
722 if (old_base == MAP_FAILED)
723 fatal ("Can't allocate buffer for %s\n", old_name, 0);
724
725 if (read (old_file, old_base, stat_buf.st_size) != stat_buf.st_size)
726 fatal ("Didn't read all of %s: errno %d\n", old_name, errno);
727
728 /* Get pointers to headers & section names */
729
730 old_file_h = (ElfW(Ehdr) *) old_base;
731 old_program_h = (ElfW(Phdr) *) ((byte *) old_base + old_file_h->e_phoff);
732 old_section_h = (ElfW(Shdr) *) ((byte *) old_base + old_file_h->e_shoff);
733 old_section_names = (char *) old_base
734 + OLD_SECTION_H (old_file_h->e_shstrndx).sh_offset;
735
736 /* Find the mdebug section, if any. */
737
738 old_mdebug_index = find_section (".mdebug", old_section_names,
739 old_name, old_file_h, old_section_h, 1);
740
741 /* Find the old .bss section. Figure out parameters of the new
742 data2 and bss sections. */
743
744 old_bss_index = find_section (".bss", old_section_names,
745 old_name, old_file_h, old_section_h, 0);
746
747 old_sbss_index = find_section (".sbss", old_section_names,
748 old_name, old_file_h, old_section_h, 1);
749 if (old_sbss_index != -1)
750 if (OLD_SECTION_H (old_sbss_index).sh_type != SHT_NOBITS)
751 old_sbss_index = -1;
752
753 /* PowerPC64 has .plt in the BSS section. */
754 old_plt_index = find_section (".plt", old_section_names,
755 old_name, old_file_h, old_section_h, 1);
756 if (old_plt_index != -1)
757 if (OLD_SECTION_H (old_plt_index).sh_type != SHT_NOBITS)
758 old_plt_index = -1;
759
760 if (old_sbss_index == -1 && old_plt_index == -1)
761 {
762 old_bss_addr = OLD_SECTION_H (old_bss_index).sh_addr;
763 old_bss_size = OLD_SECTION_H (old_bss_index).sh_size;
764 new_data2_index = old_bss_index;
765 }
766 else if (old_plt_index != -1
767 && (old_sbss_index == -1
768 || (OLD_SECTION_H (old_sbss_index).sh_addr
769 > OLD_SECTION_H (old_plt_index).sh_addr)))
770 {
771 old_bss_addr = OLD_SECTION_H (old_plt_index).sh_addr;
772 old_bss_size = OLD_SECTION_H (old_bss_index).sh_size
773 + OLD_SECTION_H (old_plt_index).sh_size;
774 if (old_sbss_index != -1)
775 old_bss_size += OLD_SECTION_H (old_sbss_index).sh_size;
776 new_data2_index = old_plt_index;
777 }
778 else
779 {
780 old_bss_addr = OLD_SECTION_H (old_sbss_index).sh_addr;
781 old_bss_size = OLD_SECTION_H (old_bss_index).sh_size
782 + OLD_SECTION_H (old_sbss_index).sh_size;
783 new_data2_index = old_sbss_index;
784 }
785
786 /* Find the old .data section. Figure out parameters of
787 the new data2 and bss sections. */
788
789 old_data_index = find_section (".data", old_section_names,
790 old_name, old_file_h, old_section_h, 0);
791
792 #if defined (emacs) || !defined (DEBUG)
793 new_bss_addr = (ElfW(Addr)) sbrk (0);
794 #else
795 new_bss_addr = old_bss_addr + old_bss_size + 0x1234;
796 #endif
797 new_data2_addr = old_bss_addr;
798 new_data2_size = new_bss_addr - old_bss_addr;
799 new_data2_offset = OLD_SECTION_H (old_data_index).sh_offset +
800 (new_data2_addr - OLD_SECTION_H (old_data_index).sh_addr);
801
802 #ifdef DEBUG
803 fprintf (stderr, "old_bss_index %d\n", old_bss_index);
804 fprintf (stderr, "old_bss_addr %x\n", old_bss_addr);
805 fprintf (stderr, "old_bss_size %x\n", old_bss_size);
806 fprintf (stderr, "new_bss_addr %x\n", new_bss_addr);
807 fprintf (stderr, "new_data2_addr %x\n", new_data2_addr);
808 fprintf (stderr, "new_data2_size %x\n", new_data2_size);
809 fprintf (stderr, "new_data2_offset %x\n", new_data2_offset);
810 #endif
811
812 if ((unsigned) new_bss_addr < (unsigned) old_bss_addr + old_bss_size)
813 fatal (".bss shrank when undumping???\n", 0, 0);
814
815 /* Set the output file to the right size. Allocate a buffer to hold
816 the image of the new file. Set pointers to various interesting
817 objects. stat_buf still has old_file data. */
818
819 new_file = open (new_name, O_RDWR | O_CREAT, 0666);
820 if (new_file < 0)
821 fatal ("Can't creat (%s): errno %d\n", new_name, errno);
822
823 new_file_size = stat_buf.st_size + old_file_h->e_shentsize + new_data2_size;
824
825 if (ftruncate (new_file, new_file_size))
826 fatal ("Can't ftruncate (%s): errno %d\n", new_name, errno);
827
828 new_base = mmap (NULL, new_file_size, PROT_READ | PROT_WRITE,
829 MAP_ANON | MAP_PRIVATE, mmap_fd, 0);
830 if (new_base == MAP_FAILED)
831 fatal ("Can't allocate buffer for %s\n", old_name, 0);
832
833 new_file_h = (ElfW(Ehdr) *) new_base;
834 new_program_h = (ElfW(Phdr) *) ((byte *) new_base + old_file_h->e_phoff);
835 new_section_h = (ElfW(Shdr) *)
836 ((byte *) new_base + old_file_h->e_shoff + new_data2_size);
837
838 /* Make our new file, program and section headers as copies of the
839 originals. */
840
841 memcpy (new_file_h, old_file_h, old_file_h->e_ehsize);
842 memcpy (new_program_h, old_program_h,
843 old_file_h->e_phnum * old_file_h->e_phentsize);
844
845 /* Modify the e_shstrndx if necessary. */
846 PATCH_INDEX (new_file_h->e_shstrndx);
847
848 /* Fix up file header. We'll add one section. Section header is
849 further away now. */
850
851 new_file_h->e_shoff += new_data2_size;
852 new_file_h->e_shnum += 1;
853
854 #ifdef DEBUG
855 fprintf (stderr, "Old section offset %x\n", old_file_h->e_shoff);
856 fprintf (stderr, "Old section count %d\n", old_file_h->e_shnum);
857 fprintf (stderr, "New section offset %x\n", new_file_h->e_shoff);
858 fprintf (stderr, "New section count %d\n", new_file_h->e_shnum);
859 #endif
860
861 /* Fix up a new program header. Extend the writable data segment so
862 that the bss area is covered too. Find that segment by looking
863 for a segment that ends just before the .bss area. Make sure
864 that no segments are above the new .data2. Put a loop at the end
865 to adjust the offset and address of any segment that is above
866 data2, just in case we decide to allow this later. */
867
868 for (n = new_file_h->e_phnum - 1; n >= 0; n--)
869 {
870 /* Compute maximum of all requirements for alignment of section. */
871 ElfW(Word) alignment = (NEW_PROGRAM_H (n)).p_align;
872 if ((OLD_SECTION_H (old_bss_index)).sh_addralign > alignment)
873 alignment = OLD_SECTION_H (old_bss_index).sh_addralign;
874
875 #ifdef __sgi
876 /* According to r02kar@x4u2.desy.de (Karsten Kuenne)
877 and oliva@gnu.org (Alexandre Oliva), on IRIX 5.2, we
878 always get "Program segment above .bss" when dumping
879 when the executable doesn't have an sbss section. */
880 if (old_sbss_index != -1)
881 #endif /* __sgi */
882 if (NEW_PROGRAM_H (n).p_vaddr + NEW_PROGRAM_H (n).p_filesz
883 > (old_sbss_index == -1
884 ? old_bss_addr
885 : round_up (old_bss_addr, alignment)))
886 fatal ("Program segment above .bss in %s\n", old_name, 0);
887
888 if (NEW_PROGRAM_H (n).p_type == PT_LOAD
889 && (round_up ((NEW_PROGRAM_H (n)).p_vaddr
890 + (NEW_PROGRAM_H (n)).p_filesz,
891 alignment)
892 == round_up (old_bss_addr, alignment)))
893 break;
894 }
895 if (n < 0)
896 fatal ("Couldn't find segment next to .bss in %s\n", old_name, 0);
897
898 /* Make sure that the size includes any padding before the old .bss
899 section. */
900 NEW_PROGRAM_H (n).p_filesz = new_bss_addr - NEW_PROGRAM_H (n).p_vaddr;
901 NEW_PROGRAM_H (n).p_memsz = NEW_PROGRAM_H (n).p_filesz;
902
903 #if 0 /* Maybe allow section after data2 - does this ever happen? */
904 for (n = new_file_h->e_phnum - 1; n >= 0; n--)
905 {
906 if (NEW_PROGRAM_H (n).p_vaddr
907 && NEW_PROGRAM_H (n).p_vaddr >= new_data2_addr)
908 NEW_PROGRAM_H (n).p_vaddr += new_data2_size - old_bss_size;
909
910 if (NEW_PROGRAM_H (n).p_offset >= new_data2_offset)
911 NEW_PROGRAM_H (n).p_offset += new_data2_size;
912 }
913 #endif
914
915 /* Fix up section headers based on new .data2 section. Any section
916 whose offset or virtual address is after the new .data2 section
917 gets its value adjusted. .bss size becomes zero and new address
918 is set. data2 section header gets added by copying the existing
919 .data header and modifying the offset, address and size. */
920 for (old_data_index = 1; old_data_index < (int) old_file_h->e_shnum;
921 old_data_index++)
922 if (!strcmp (old_section_names + OLD_SECTION_H (old_data_index).sh_name,
923 ".data"))
924 break;
925 if (old_data_index == old_file_h->e_shnum)
926 fatal ("Can't find .data in %s.\n", old_name, 0);
927
928 /* Walk through all section headers, insert the new data2 section right
929 before the new bss section. */
930 for (n = 1, nn = 1; n < (int) old_file_h->e_shnum; n++, nn++)
931 {
932 caddr_t src;
933 /* If it is (s)bss section, insert the new data2 section before it. */
934 /* new_data2_index is the index of either old_sbss or old_bss, that was
935 chosen as a section for new_data2. */
936 if (n == new_data2_index)
937 {
938 /* Steal the data section header for this data2 section. */
939 memcpy (&NEW_SECTION_H (nn), &OLD_SECTION_H (old_data_index),
940 new_file_h->e_shentsize);
941
942 NEW_SECTION_H (nn).sh_addr = new_data2_addr;
943 NEW_SECTION_H (nn).sh_offset = new_data2_offset;
944 NEW_SECTION_H (nn).sh_size = new_data2_size;
945 /* Use the bss section's alignment. This will assure that the
946 new data2 section always be placed in the same spot as the old
947 bss section by any other application. */
948 NEW_SECTION_H (nn).sh_addralign = OLD_SECTION_H (n).sh_addralign;
949
950 /* Now copy over what we have in the memory now. */
951 memcpy (NEW_SECTION_H (nn).sh_offset + new_base,
952 (caddr_t) OLD_SECTION_H (n).sh_addr,
953 new_data2_size);
954 nn++;
955 }
956
957 memcpy (&NEW_SECTION_H (nn), &OLD_SECTION_H (n),
958 old_file_h->e_shentsize);
959
960 if (n == old_bss_index
961 /* The new bss and sbss section's size is zero, and its file offset
962 and virtual address should be off by NEW_DATA2_SIZE. */
963 || n == old_sbss_index || n == old_plt_index
964 )
965 {
966 /* NN should be `old_s?bss_index + 1' at this point. */
967 NEW_SECTION_H (nn).sh_offset =
968 NEW_SECTION_H (new_data2_index).sh_offset + new_data2_size;
969 NEW_SECTION_H (nn).sh_addr =
970 NEW_SECTION_H (new_data2_index).sh_addr + new_data2_size;
971 /* Let the new bss section address alignment be the same as the
972 section address alignment followed the old bss section, so
973 this section will be placed in exactly the same place. */
974 NEW_SECTION_H (nn).sh_addralign = OLD_SECTION_H (nn).sh_addralign;
975 NEW_SECTION_H (nn).sh_size = 0;
976 }
977 else
978 {
979 /* Any section that was originally placed after the .bss
980 section should now be off by NEW_DATA2_SIZE. If a
981 section overlaps the .bss section, consider it to be
982 placed after the .bss section. Overlap can occur if the
983 section just before .bss has less-strict alignment; this
984 was observed between .symtab and .bss on Solaris 2.5.1
985 (sparc) with GCC snapshot 960602. */
986 #ifdef SOLARIS_POWERPC
987 /* On PPC Reference Platform running Solaris 2.5.1
988 the plt section is also of type NOBI like the bss section.
989 (not really stored) and therefore sections after the bss
990 section start at the plt offset. The plt section is always
991 the one just before the bss section.
992 It would be better to put the new data section before
993 the .plt section, or use libelf instead.
994 Erik Deumens, deumens@qtp.ufl.edu. */
995 if (NEW_SECTION_H (nn).sh_offset
996 >= OLD_SECTION_H (old_bss_index-1).sh_offset)
997 NEW_SECTION_H (nn).sh_offset += new_data2_size;
998 #else
999 if (NEW_SECTION_H (nn).sh_offset + NEW_SECTION_H (nn).sh_size
1000 > new_data2_offset)
1001 NEW_SECTION_H (nn).sh_offset += new_data2_size;
1002 #endif
1003 /* Any section that was originally placed after the section
1004 header table should now be off by the size of one section
1005 header table entry. */
1006 if (NEW_SECTION_H (nn).sh_offset > new_file_h->e_shoff)
1007 NEW_SECTION_H (nn).sh_offset += new_file_h->e_shentsize;
1008 }
1009
1010 /* If any section hdr refers to the section after the new .data
1011 section, make it refer to next one because we have inserted
1012 a new section in between. */
1013
1014 PATCH_INDEX (NEW_SECTION_H (nn).sh_link);
1015 /* For symbol tables, info is a symbol table index,
1016 so don't change it. */
1017 if (NEW_SECTION_H (nn).sh_type != SHT_SYMTAB
1018 && NEW_SECTION_H (nn).sh_type != SHT_DYNSYM)
1019 PATCH_INDEX (NEW_SECTION_H (nn).sh_info);
1020
1021 if (old_sbss_index != -1)
1022 if (!strcmp (old_section_names + NEW_SECTION_H (nn).sh_name, ".sbss"))
1023 {
1024 NEW_SECTION_H (nn).sh_offset =
1025 round_up (NEW_SECTION_H (nn).sh_offset,
1026 NEW_SECTION_H (nn).sh_addralign);
1027 NEW_SECTION_H (nn).sh_type = SHT_PROGBITS;
1028 }
1029
1030 /* Now, start to copy the content of sections. */
1031 if (NEW_SECTION_H (nn).sh_type == SHT_NULL
1032 || NEW_SECTION_H (nn).sh_type == SHT_NOBITS)
1033 continue;
1034
1035 /* Write out the sections. .data and .data1 (and data2, called
1036 ".data" in the strings table) get copied from the current process
1037 instead of the old file. */
1038 if (!strcmp (old_section_names + NEW_SECTION_H (n).sh_name, ".data")
1039 || !strcmp ((old_section_names + NEW_SECTION_H (n).sh_name),
1040 ".sdata")
1041 || !strcmp ((old_section_names + NEW_SECTION_H (n).sh_name),
1042 ".lit4")
1043 || !strcmp ((old_section_names + NEW_SECTION_H (n).sh_name),
1044 ".lit8")
1045 /* The conditional bit below was in Oliva's original code
1046 (1999-08-25) and seems to have been dropped by mistake
1047 subsequently. It prevents a crash at startup under X in
1048 `IRIX64 6.5 6.5.17m', whether compiled on that relase or
1049 an earlier one. It causes no trouble on the other ELF
1050 platforms I could test (Irix 6.5.15m, Solaris 8, Debian
1051 Potato x86, Debian Woody SPARC); however, it's reported
1052 to cause crashes under some version of GNU/Linux. It's
1053 not yet clear what's changed in that Irix version to
1054 cause the problem, or why the fix sometimes fails under
1055 GNU/Linux. There's probably no good reason to have
1056 something Irix-specific here, but this will have to do
1057 for now. IRIX6_5 is the most specific macro we have to
1058 test. -- fx 2002-10-01
1059
1060 The issue _looks_ as though it's gone away on 6.5.18m,
1061 but maybe it's still lurking, to be triggered by some
1062 change in the binary. It appears to concern the dynamic
1063 loader, but I never got anywhere with an SGI support call
1064 seeking clues. -- fx 2002-11-29. */
1065 #ifdef IRIX6_5
1066 || !strcmp ((old_section_names + NEW_SECTION_H (n).sh_name),
1067 ".got")
1068 #endif
1069 || !strcmp ((old_section_names + NEW_SECTION_H (n).sh_name),
1070 ".sdata1")
1071 || !strcmp ((old_section_names + NEW_SECTION_H (n).sh_name),
1072 ".data1")
1073 || !strcmp ((old_section_names + NEW_SECTION_H (n).sh_name),
1074 ".sbss"))
1075 src = (caddr_t) OLD_SECTION_H (n).sh_addr;
1076 else
1077 src = old_base + OLD_SECTION_H (n).sh_offset;
1078
1079 memcpy (NEW_SECTION_H (nn).sh_offset + new_base, src,
1080 NEW_SECTION_H (nn).sh_size);
1081
1082 #ifdef __alpha__
1083 /* Update Alpha COFF symbol table: */
1084 if (strcmp (old_section_names + OLD_SECTION_H (n).sh_name, ".mdebug")
1085 == 0)
1086 {
1087 pHDRR symhdr = (pHDRR) (NEW_SECTION_H (nn).sh_offset + new_base);
1088
1089 symhdr->cbLineOffset += new_data2_size;
1090 symhdr->cbDnOffset += new_data2_size;
1091 symhdr->cbPdOffset += new_data2_size;
1092 symhdr->cbSymOffset += new_data2_size;
1093 symhdr->cbOptOffset += new_data2_size;
1094 symhdr->cbAuxOffset += new_data2_size;
1095 symhdr->cbSsOffset += new_data2_size;
1096 symhdr->cbSsExtOffset += new_data2_size;
1097 symhdr->cbFdOffset += new_data2_size;
1098 symhdr->cbRfdOffset += new_data2_size;
1099 symhdr->cbExtOffset += new_data2_size;
1100 }
1101 #endif /* __alpha__ */
1102
1103 #if defined (__sony_news) && defined (_SYSTYPE_SYSV)
1104 if (NEW_SECTION_H (nn).sh_type == SHT_MIPS_DEBUG
1105 && old_mdebug_index != -1)
1106 {
1107 int diff = NEW_SECTION_H(nn).sh_offset
1108 - OLD_SECTION_H(old_mdebug_index).sh_offset;
1109 HDRR *phdr = (HDRR *)(NEW_SECTION_H (nn).sh_offset + new_base);
1110
1111 if (diff)
1112 {
1113 phdr->cbLineOffset += diff;
1114 phdr->cbDnOffset += diff;
1115 phdr->cbPdOffset += diff;
1116 phdr->cbSymOffset += diff;
1117 phdr->cbOptOffset += diff;
1118 phdr->cbAuxOffset += diff;
1119 phdr->cbSsOffset += diff;
1120 phdr->cbSsExtOffset += diff;
1121 phdr->cbFdOffset += diff;
1122 phdr->cbRfdOffset += diff;
1123 phdr->cbExtOffset += diff;
1124 }
1125 }
1126 #endif /* __sony_news && _SYSTYPE_SYSV */
1127
1128 #if __sgi
1129 /* Adjust the HDRR offsets in .mdebug and copy the
1130 line data if it's in its usual 'hole' in the object.
1131 Makes the new file debuggable with dbx.
1132 patches up two problems: the absolute file offsets
1133 in the HDRR record of .mdebug (see /usr/include/syms.h), and
1134 the ld bug that gets the line table in a hole in the
1135 elf file rather than in the .mdebug section proper.
1136 David Anderson. davea@sgi.com Jan 16,1994. */
1137 if (n == old_mdebug_index)
1138 {
1139 #define MDEBUGADJUST(__ct,__fileaddr) \
1140 if (n_phdrr->__ct > 0) \
1141 { \
1142 n_phdrr->__fileaddr += movement; \
1143 }
1144
1145 HDRR * o_phdrr = (HDRR *)((byte *)old_base + OLD_SECTION_H (n).sh_offset);
1146 HDRR * n_phdrr = (HDRR *)((byte *)new_base + NEW_SECTION_H (nn).sh_offset);
1147 unsigned movement = new_data2_size;
1148
1149 MDEBUGADJUST (idnMax, cbDnOffset);
1150 MDEBUGADJUST (ipdMax, cbPdOffset);
1151 MDEBUGADJUST (isymMax, cbSymOffset);
1152 MDEBUGADJUST (ioptMax, cbOptOffset);
1153 MDEBUGADJUST (iauxMax, cbAuxOffset);
1154 MDEBUGADJUST (issMax, cbSsOffset);
1155 MDEBUGADJUST (issExtMax, cbSsExtOffset);
1156 MDEBUGADJUST (ifdMax, cbFdOffset);
1157 MDEBUGADJUST (crfd, cbRfdOffset);
1158 MDEBUGADJUST (iextMax, cbExtOffset);
1159 /* The Line Section, being possible off in a hole of the object,
1160 requires special handling. */
1161 if (n_phdrr->cbLine > 0)
1162 {
1163 if (o_phdrr->cbLineOffset > (OLD_SECTION_H (n).sh_offset
1164 + OLD_SECTION_H (n).sh_size))
1165 {
1166 /* line data is in a hole in elf. do special copy and adjust
1167 for this ld mistake.
1168 */
1169 n_phdrr->cbLineOffset += movement;
1170
1171 memcpy (n_phdrr->cbLineOffset + new_base,
1172 o_phdrr->cbLineOffset + old_base, n_phdrr->cbLine);
1173 }
1174 else
1175 {
1176 /* somehow line data is in .mdebug as it is supposed to be. */
1177 MDEBUGADJUST (cbLine, cbLineOffset);
1178 }
1179 }
1180 }
1181 #endif /* __sgi */
1182
1183 /* If it is the symbol table, its st_shndx field needs to be patched. */
1184 if (NEW_SECTION_H (nn).sh_type == SHT_SYMTAB
1185 || NEW_SECTION_H (nn).sh_type == SHT_DYNSYM)
1186 {
1187 ElfW(Shdr) *spt = &NEW_SECTION_H (nn);
1188 unsigned int num = spt->sh_size / spt->sh_entsize;
1189 ElfW(Sym) * sym = (ElfW(Sym) *) (NEW_SECTION_H (nn).sh_offset +
1190 new_base);
1191 for (; num--; sym++)
1192 {
1193 if ((sym->st_shndx == SHN_UNDEF)
1194 || (sym->st_shndx == SHN_ABS)
1195 || (sym->st_shndx == SHN_COMMON))
1196 continue;
1197
1198 PATCH_INDEX (sym->st_shndx);
1199 }
1200 }
1201 }
1202
1203 /* Update the symbol values of _edata and _end. */
1204 for (n = new_file_h->e_shnum - 1; n; n--)
1205 {
1206 byte *symnames;
1207 ElfW(Sym) *symp, *symendp;
1208
1209 if (NEW_SECTION_H (n).sh_type != SHT_DYNSYM
1210 && NEW_SECTION_H (n).sh_type != SHT_SYMTAB)
1211 continue;
1212
1213 symnames = ((byte *) new_base
1214 + NEW_SECTION_H (NEW_SECTION_H (n).sh_link).sh_offset);
1215 symp = (ElfW(Sym) *) (NEW_SECTION_H (n).sh_offset + new_base);
1216 symendp = (ElfW(Sym) *) ((byte *)symp + NEW_SECTION_H (n).sh_size);
1217
1218 for (; symp < symendp; symp ++)
1219 if (strcmp ((char *) (symnames + symp->st_name), "_end") == 0
1220 || strcmp ((char *) (symnames + symp->st_name), "end") == 0
1221 || strcmp ((char *) (symnames + symp->st_name), "_edata") == 0
1222 || strcmp ((char *) (symnames + symp->st_name), "edata") == 0)
1223 memcpy (&symp->st_value, &new_bss_addr, sizeof (new_bss_addr));
1224 }
1225
1226 /* This loop seeks out relocation sections for the data section, so
1227 that it can undo relocations performed by the runtime linker. */
1228 #ifndef BROKEN_NOCOMBRELOC
1229 for (n = new_file_h->e_shnum - 1; n; n--)
1230 {
1231 ElfW(Shdr) section = NEW_SECTION_H (n);
1232
1233 /* Cause a compilation error if anyone uses n instead of nn below. */
1234 struct {int a;} n;
1235 (void)n.a; /* Prevent `unused variable' warnings. */
1236
1237 switch (section.sh_type)
1238 {
1239 default:
1240 break;
1241 case SHT_REL:
1242 case SHT_RELA:
1243 /* This code handles two different size structs, but there should
1244 be no harm in that provided that r_offset is always the first
1245 member. */
1246 nn = section.sh_info;
1247 if (!strcmp (old_section_names + NEW_SECTION_H (nn).sh_name, ".data")
1248 || !strcmp ((old_section_names + NEW_SECTION_H (nn).sh_name),
1249 ".sdata")
1250 || !strcmp ((old_section_names + NEW_SECTION_H (nn).sh_name),
1251 ".lit4")
1252 || !strcmp ((old_section_names + NEW_SECTION_H (nn).sh_name),
1253 ".lit8")
1254 #ifdef IRIX6_5 /* see above */
1255 || !strcmp ((old_section_names + NEW_SECTION_H (nn).sh_name),
1256 ".got")
1257 #endif
1258 || !strcmp ((old_section_names + NEW_SECTION_H (nn).sh_name),
1259 ".sdata1")
1260 || !strcmp ((old_section_names + NEW_SECTION_H (nn).sh_name),
1261 ".data1"))
1262 {
1263 ElfW(Addr) offset = (NEW_SECTION_H (nn).sh_addr
1264 - NEW_SECTION_H (nn).sh_offset);
1265 caddr_t reloc = old_base + section.sh_offset, end;
1266 for (end = reloc + section.sh_size; reloc < end;
1267 reloc += section.sh_entsize)
1268 {
1269 ElfW(Addr) addr = ((ElfW(Rel) *) reloc)->r_offset - offset;
1270 #ifdef __alpha__
1271 /* The Alpha ELF binutils currently have a bug that
1272 sometimes results in relocs that contain all
1273 zeroes. Work around this for now... */
1274 if (((ElfW(Rel) *) reloc)->r_offset == 0)
1275 continue;
1276 #endif
1277 memcpy (new_base + addr, old_base + addr, sizeof(ElfW(Addr)));
1278 }
1279 }
1280 break;
1281 }
1282 }
1283 #else /* BROKEN_NOCOMBRELOC */
1284 for (n = 1, n_unreloc_sections = 0; n < new_file_h->e_shnum; n++)
1285 if (!strcmp (old_section_names + NEW_SECTION_H (n).sh_name, ".data")
1286 || !strcmp (old_section_names + NEW_SECTION_H (n).sh_name, ".sdata")
1287 || !strcmp (old_section_names + NEW_SECTION_H (n).sh_name, ".lit4")
1288 || !strcmp (old_section_names + NEW_SECTION_H (n).sh_name, ".lit8")
1289 #ifdef IRIX6_5 /* see above */
1290 || !strcmp (old_section_names + NEW_SECTION_H (n).sh_name, ".got")
1291 #endif
1292 || !strcmp (old_section_names + NEW_SECTION_H (n).sh_name, ".sdata1")
1293 || !strcmp (old_section_names + NEW_SECTION_H (n).sh_name, ".data1"))
1294 {
1295 assert (n_unreloc_sections
1296 < (sizeof (unreloc_sections) / sizeof (unreloc_sections[0])));
1297 unreloc_sections[n_unreloc_sections++] = n;
1298 #ifdef DEBUG
1299 fprintf (stderr, "section %d: %s\n", n,
1300 old_section_names + NEW_SECTION_H (n).sh_name);
1301 #endif
1302 }
1303
1304 for (n = new_file_h->e_shnum - 1; n; n--)
1305 {
1306 ElfW(Shdr) section = NEW_SECTION_H (n);
1307 caddr_t reloc, end;
1308 ElfW(Addr) addr, offset;
1309 int target;
1310
1311 switch (section.sh_type)
1312 {
1313 default:
1314 break;
1315 case SHT_REL:
1316 case SHT_RELA:
1317 /* This code handles two different size structs, but there should
1318 be no harm in that provided that r_offset is always the first
1319 member. */
1320 for (reloc = old_base + section.sh_offset,
1321 end = reloc + section.sh_size;
1322 reloc < end;
1323 reloc += section.sh_entsize)
1324 {
1325 addr = ((ElfW(Rel) *) reloc)->r_offset;
1326 #ifdef __alpha__
1327 /* The Alpha ELF binutils currently have a bug that
1328 sometimes results in relocs that contain all
1329 zeroes. Work around this for now... */
1330 if (addr == 0)
1331 continue;
1332 #endif
1333 for (nn = 0; nn < n_unreloc_sections; nn++)
1334 {
1335 target = unreloc_sections[nn];
1336 if (NEW_SECTION_H (target).sh_addr <= addr
1337 && addr < (NEW_SECTION_H (target).sh_addr +
1338 NEW_SECTION_H (target).sh_size))
1339 {
1340 offset = (NEW_SECTION_H (target).sh_addr -
1341 NEW_SECTION_H (target).sh_offset);
1342 memcpy (new_base + addr - offset,
1343 old_base + addr - offset,
1344 sizeof (ElfW(Addr)));
1345 #ifdef DEBUG
1346 fprintf (stderr, "unrelocate: [%08lx] <= %08lx\n",
1347 (long) addr,
1348 (long) *((long *) (new_base + addr - offset)));
1349 #endif
1350 break;
1351 }
1352 }
1353 }
1354 break;
1355 }
1356 }
1357 #endif /* BROKEN_NOCOMBRELOC */
1358
1359 /* Write out new_file, and free the buffers. */
1360
1361 if (write (new_file, new_base, new_file_size) != new_file_size)
1362 #ifndef emacs
1363 fatal ("Didn't write %d bytes: errno %d\n",
1364 new_file_size, errno);
1365 #else
1366 fatal ("Didn't write %d bytes to %s: errno %d\n",
1367 new_file_size, new_base, errno);
1368 #endif
1369 munmap (old_base, old_file_size);
1370 munmap (new_base, new_file_size);
1371
1372 /* Close the files and make the new file executable. */
1373
1374 #if MAP_ANON == 0
1375 close (mmap_fd);
1376 #endif
1377
1378 if (close (old_file))
1379 fatal ("Can't close (%s): errno %d\n", old_name, errno);
1380
1381 if (close (new_file))
1382 fatal ("Can't close (%s): errno %d\n", new_name, errno);
1383
1384 if (stat (new_name, &stat_buf) == -1)
1385 fatal ("Can't stat (%s): errno %d\n", new_name, errno);
1386
1387 n = umask (777);
1388 umask (n);
1389 stat_buf.st_mode |= 0111 & ~n;
1390 if (chmod (new_name, stat_buf.st_mode) == -1)
1391 fatal ("Can't chmod (%s): errno %d\n", new_name, errno);
1392 }
1393
1394 /* arch-tag: e02e1512-95e2-4ef0-bba7-b6bce658f1e3
1395 (do not change this comment) */