0
|
1 /* Unexec for Siemens machines running Sinix (modified SVR4).
|
|
2 Copyright (C) 1985, 1986, 1987, 1988, 1990, 1992, 1993, 1994, 1995
|
|
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 2, 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., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, 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 /* Synched up with: FSF 19.31. */
|
|
27
|
|
28 /*
|
|
29 * unexec.c - Convert a running program into an a.out file.
|
|
30 *
|
|
31 * Author: Spencer W. Thomas
|
|
32 * Computer Science Dept.
|
|
33 * University of Utah
|
|
34 * Date: Tue Mar 2 1982
|
|
35 * Modified heavily since then.
|
|
36 *
|
|
37 * Synopsis:
|
|
38 * unexec (new_name, a_name, data_start, bss_start, entry_address)
|
|
39 * char *new_name, *a_name;
|
|
40 * unsigned data_start, bss_start, entry_address;
|
|
41 *
|
|
42 * Takes a snapshot of the program and makes an a.out format file in the
|
|
43 * file named by the string argument new_name.
|
|
44 * If a_name is non-NULL, the symbol table will be taken from the given file.
|
|
45 * On some machines, an existing a_name file is required.
|
|
46 *
|
|
47 * The boundaries within the a.out file may be adjusted with the data_start
|
|
48 * and bss_start arguments. Either or both may be given as 0 for defaults.
|
|
49 *
|
|
50 * Data_start gives the boundary between the text segment and the data
|
|
51 * segment of the program. The text segment can contain shared, read-only
|
|
52 * program code and literal data, while the data segment is always unshared
|
|
53 * and unprotected. Data_start gives the lowest unprotected address.
|
|
54 * The value you specify may be rounded down to a suitable boundary
|
|
55 * as required by the machine you are using.
|
|
56 *
|
|
57 * Specifying zero for data_start means the boundary between text and data
|
|
58 * should not be the same as when the program was loaded.
|
|
59 * If NO_REMAP is defined, the argument data_start is ignored and the
|
|
60 * segment boundaries are never changed.
|
|
61 *
|
|
62 * Bss_start indicates how much of the data segment is to be saved in the
|
|
63 * a.out file and restored when the program is executed. It gives the lowest
|
|
64 * unsaved address, and is rounded up to a page boundary. The default when 0
|
|
65 * is given assumes that the entire data segment is to be stored, including
|
|
66 * the previous data and bss as well as any additional storage allocated with
|
|
67 * break (2).
|
|
68 *
|
|
69 * The new file is set up to start at entry_address.
|
|
70 *
|
|
71 * If you make improvements I'd like to get them too.
|
|
72 * harpo!utah-cs!thomas, thomas@Utah-20
|
|
73 *
|
|
74 */
|
|
75
|
|
76 /* Even more heavily modified by james@bigtex.cactus.org of Dell Computer Co.
|
|
77 * ELF support added.
|
|
78 *
|
|
79 * Basic theory: the data space of the running process needs to be
|
|
80 * dumped to the output file. Normally we would just enlarge the size
|
|
81 * of .data, scooting everything down. But we can't do that in ELF,
|
|
82 * because there is often something between the .data space and the
|
|
83 * .bss space.
|
|
84 *
|
|
85 * In the temacs dump below, notice that the Global Offset Table
|
|
86 * (.got) and the Dynamic link data (.dynamic) come between .data1 and
|
|
87 * .bss. It does not work to overlap .data with these fields.
|
|
88 *
|
|
89 * The solution is to create a new .data segment. This segment is
|
|
90 * filled with data from the current process. Since the contents of
|
|
91 * various sections refer to sections by index, the new .data segment
|
|
92 * is made the last in the table to avoid changing any existing index.
|
|
93 */
|
|
94
|
|
95 /* Modified by wtien@urbana.mcd.mot.com of Motorola Inc.
|
|
96 *
|
|
97 * The above mechanism does not work if the unexeced ELF file is being
|
|
98 * re-layout by other applications (such as `strip'). All the applications
|
|
99 * that re-layout the internal of ELF will layout all sections in ascending
|
|
100 * order of their file offsets. After the re-layout, the data2 section will
|
|
101 * still be the LAST section in the section header vector, but its file offset
|
|
102 * is now being pushed far away down, and causes part of it not to be mapped
|
|
103 * in (ie. not covered by the load segment entry in PHDR vector), therefore
|
|
104 * causes the new binary to fail.
|
|
105 *
|
|
106 * The solution is to modify the unexec algorithm to insert the new data2
|
|
107 * section header right before the new bss section header, so their file
|
|
108 * offsets will be in the ascending order. Since some of the section's (all
|
|
109 * sections AFTER the bss section) indexes are now changed, we also need to
|
|
110 * modify some fields to make them point to the right sections. This is done
|
|
111 * by macro PATCH_INDEX. All the fields that need to be patched are:
|
|
112 *
|
|
113 * 1. ELF header e_shstrndx field.
|
|
114 * 2. section header sh_link and sh_info field.
|
|
115 * 3. symbol table entry st_shndx field.
|
|
116 */
|
|
117
|
|
118 /*
|
|
119 * New modifications for Siemens Nixdorf's MIPS-based machines.
|
|
120 * Marco.Walther@mch.sni.de
|
|
121 *
|
|
122 * The problem: Before the bss segment we have a so called sbss segment
|
|
123 * (small bss) and maybe an sdata segment. These segments
|
|
124 * must also be handled correct.
|
|
125 *
|
|
126 * /home1/marco/emacs/emacs-19.22/src
|
|
127 * dump -hv temacs
|
|
128 *
|
|
129 * temacs:
|
|
130 *
|
|
131 * **** SECTION HEADER TABLE ****
|
|
132 * [No] Type Flags Addr Offset Size Name
|
|
133 * Link Info Adralgn Entsize
|
|
134 *
|
|
135 * [1] PBIT -A-- 0x4000f4 0xf4 0x13 .interp
|
|
136 * 0 0 0x1 0
|
|
137 *
|
|
138 * [2] REGI -A-- 0x400108 0x108 0x18 .reginfo
|
|
139 * 0 0 0x4 0x18
|
|
140 *
|
|
141 * [3] DYNM -A-- 0x400120 0x120 0xb8 .dynamic
|
|
142 * 6 0 0x4 0x8
|
|
143 *
|
|
144 * [4] HASH -A-- 0x4001d8 0x1d8 0x8a0 .hash
|
|
145 * 5 0 0x4 0x4
|
|
146 *
|
|
147 * [5] DYNS -A-- 0x400a78 0xa78 0x11f0 .dynsym
|
|
148 * 6 2 0x4 0x10
|
|
149 *
|
|
150 * [6] STRT -A-- 0x401c68 0x1c68 0xbf9 .dynstr
|
|
151 * 0 0 0x1 0
|
|
152 *
|
|
153 * [7] REL -A-- 0x402864 0x2864 0x18 .rel.dyn
|
|
154 * 5 14 0x4 0x8
|
|
155 *
|
|
156 * [8] PBIT -AI- 0x402880 0x2880 0x60 .init
|
|
157 * 0 0 0x10 0x1
|
|
158 *
|
|
159 * [9] PBIT -AI- 0x4028e0 0x28e0 0x1234 .plt
|
|
160 * 0 0 0x4 0x4
|
|
161 *
|
|
162 * [10] PBIT -AI- 0x403b20 0x3b20 0xee400 .text
|
|
163 * 0 0 0x20 0x1
|
|
164 *
|
|
165 * [11] PBIT -AI- 0x4f1f20 0xf1f20 0x60 .fini
|
|
166 * 0 0 0x10 0x1
|
|
167 *
|
|
168 * [12] PBIT -A-- 0x4f1f80 0xf1f80 0xd90 .rdata
|
|
169 * 0 0 0x10 0x1
|
|
170 *
|
|
171 * [13] PBIT -A-- 0x4f2d10 0xf2d10 0x17e0 .rodata
|
|
172 * 0 0 0x10 0x1
|
|
173 *
|
|
174 * [14] PBIT WA-- 0x5344f0 0xf44f0 0x4b3e4 .data <<<<<
|
|
175 * 0 0 0x10 0x1
|
|
176 *
|
|
177 * [15] PBIT WA-G 0x57f8d4 0x13f8d4 0x2a84 .got
|
|
178 * 0 0 0x4 0x4
|
|
179 *
|
|
180 * [16] PBIT WA-G 0x582360 0x142360 0x10 .sdata <<<<<
|
|
181 * 0 0 0x10 0x1
|
|
182 *
|
|
183 * [17] NOBI WA-G 0x582370 0x142370 0xb84 .sbss <<<<<
|
|
184 * 0 0 0x4 0
|
|
185 *
|
|
186 * [18] NOBI WA-- 0x582f00 0x142370 0x27ec0 .bss <<<<<
|
|
187 * 0 0 0x10 0x1
|
|
188 *
|
|
189 * [19] SYMT ---- 0 0x142370 0x10e40 .symtab
|
|
190 * 20 1108 0x4 0x10
|
|
191 *
|
|
192 * [20] STRT ---- 0 0x1531b0 0xed9e .strtab
|
|
193 * 0 0 0x1 0
|
|
194 *
|
|
195 * [21] STRT ---- 0 0x161f4e 0xb5 .shstrtab
|
|
196 * 0 0 0x1 0
|
|
197 *
|
|
198 * [22] PBIT ---- 0 0x162003 0x28e2a .comment
|
|
199 * 0 0 0x1 0x1
|
|
200 *
|
|
201 * [23] PBIT ---- 0 0x18ae2d 0x592 .debug
|
|
202 * 0 0 0x1 0
|
|
203 *
|
|
204 * [24] PBIT ---- 0 0x18b3bf 0x80 .line
|
|
205 * 0 0 0x1 0
|
|
206 *
|
|
207 * [25] MDBG ---- 0 0x18b440 0x60 .mdebug
|
|
208 * 0 0 0x4 0
|
|
209 *
|
|
210 *
|
|
211 * dump -hv emacs
|
|
212 *
|
|
213 * emacs:
|
|
214 *
|
|
215 * **** SECTION HEADER TABLE ****
|
|
216 * [No] Type Flags Addr Offset Size Name
|
|
217 * Link Info Adralgn Entsize
|
|
218 *
|
|
219 * [1] PBIT -A-- 0x4000f4 0xf4 0x13 .interp
|
|
220 * 0 0 0x1 0
|
|
221 *
|
|
222 * [2] REGI -A-- 0x400108 0x108 0x18 .reginfo
|
|
223 * 0 0 0x4 0x18
|
|
224 *
|
|
225 * [3] DYNM -A-- 0x400120 0x120 0xb8 .dynamic
|
|
226 * 6 0 0x4 0x8
|
|
227 *
|
|
228 * [4] HASH -A-- 0x4001d8 0x1d8 0x8a0 .hash
|
|
229 * 5 0 0x4 0x4
|
|
230 *
|
|
231 * [5] DYNS -A-- 0x400a78 0xa78 0x11f0 .dynsym
|
|
232 * 6 2 0x4 0x10
|
|
233 *
|
|
234 * [6] STRT -A-- 0x401c68 0x1c68 0xbf9 .dynstr
|
|
235 * 0 0 0x1 0
|
|
236 *
|
|
237 * [7] REL -A-- 0x402864 0x2864 0x18 .rel.dyn
|
|
238 * 5 14 0x4 0x8
|
|
239 *
|
|
240 * [8] PBIT -AI- 0x402880 0x2880 0x60 .init
|
|
241 * 0 0 0x10 0x1
|
|
242 *
|
|
243 * [9] PBIT -AI- 0x4028e0 0x28e0 0x1234 .plt
|
|
244 * 0 0 0x4 0x4
|
|
245 *
|
|
246 * [10] PBIT -AI- 0x403b20 0x3b20 0xee400 .text
|
|
247 * 0 0 0x20 0x1
|
|
248 *
|
|
249 * [11] PBIT -AI- 0x4f1f20 0xf1f20 0x60 .fini
|
|
250 * 0 0 0x10 0x1
|
|
251 *
|
|
252 * [12] PBIT -A-- 0x4f1f80 0xf1f80 0xd90 .rdata
|
|
253 * 0 0 0x10 0x1
|
|
254 *
|
|
255 * [13] PBIT -A-- 0x4f2d10 0xf2d10 0x17e0 .rodata
|
|
256 * 0 0 0x10 0x1
|
|
257 *
|
|
258 * [14] PBIT WA-- 0x5344f0 0xf44f0 0x4b3e4 .data <<<<<
|
|
259 * 0 0 0x10 0x1
|
|
260 *
|
|
261 * [15] PBIT WA-G 0x57f8d4 0x13f8d4 0x2a84 .got
|
|
262 * 0 0 0x4 0x4
|
|
263 *
|
|
264 * [16] PBIT WA-G 0x582360 0x142360 0xb94 .sdata <<<<<
|
|
265 * 0 0 0x10 0x1
|
|
266 *
|
|
267 * [17] PBIT WA-- 0x582f00 0x142f00 0x94100 .data <<<<<
|
|
268 * 0 0 0x10 0x1
|
|
269 *
|
|
270 * [18] NOBI WA-G 0x617000 0x1d7000 0 .sbss <<<<<
|
|
271 * 0 0 0x4 0
|
|
272 *
|
|
273 * [19] NOBI WA-- 0x617000 0x1d7000 0 .bss <<<<<
|
|
274 * 0 0 0x4 0x1
|
|
275 *
|
|
276 * [20] SYMT ---- 0 0x1d7000 0x10e40 .symtab
|
|
277 * 21 1109 0x4 0x10
|
|
278 *
|
|
279 * [21] STRT ---- 0 0x1e7e40 0xed9e .strtab
|
|
280 * 0 0 0x1 0
|
|
281 *
|
|
282 * [22] STRT ---- 0 0x1f6bde 0xb5 .shstrtab
|
|
283 * 0 0 0x1 0
|
|
284 *
|
|
285 * [23] PBIT ---- 0 0x1f6c93 0x28e2a .comment
|
|
286 * 0 0 0x1 0x1
|
|
287 *
|
|
288 * [24] PBIT ---- 0 0x21fabd 0x592 .debug
|
|
289 * 0 0 0x1 0
|
|
290 *
|
|
291 * [25] PBIT ---- 0 0x22004f 0x80 .line
|
|
292 * 0 0 0x1 0
|
|
293 *
|
|
294 * [26] MDBG ---- 0 0x2200d0 0x60 .mdebug
|
|
295 * 0 0 0x4 0
|
|
296 *
|
|
297 */
|
|
298
|
|
299 #include <sys/types.h>
|
|
300 #include <stdio.h>
|
|
301 #include <sys/stat.h>
|
|
302 #include <memory.h>
|
|
303 #include <string.h>
|
|
304 #include <errno.h>
|
|
305 #include <unistd.h>
|
|
306 #include <fcntl.h>
|
|
307 #include <elf.h>
|
|
308 #include <sys/mman.h>
|
|
309
|
|
310 #ifndef emacs
|
|
311 #define fatal(a, b, c) fprintf(stderr, a, b, c), exit(1)
|
|
312 #else
|
|
313 extern void fatal(char *, ...);
|
|
314 #endif
|
|
315
|
|
316 /* Get the address of a particular section or program header entry,
|
|
317 * accounting for the size of the entries.
|
|
318 */
|
|
319
|
|
320 #define OLD_SECTION_H(n) \
|
|
321 (*(Elf32_Shdr *) ((byte *) old_section_h + old_file_h->e_shentsize * (n)))
|
|
322 #define NEW_SECTION_H(n) \
|
|
323 (*(Elf32_Shdr *) ((byte *) new_section_h + new_file_h->e_shentsize * (n)))
|
|
324 #define OLD_PROGRAM_H(n) \
|
|
325 (*(Elf32_Phdr *) ((byte *) old_program_h + old_file_h->e_phentsize * (n)))
|
|
326 #define NEW_PROGRAM_H(n) \
|
|
327 (*(Elf32_Phdr *) ((byte *) new_program_h + new_file_h->e_phentsize * (n)))
|
|
328
|
|
329 #define PATCH_INDEX(n) \
|
|
330 do { \
|
|
331 if ((n) >= old_sbss_index) \
|
|
332 (n) += 1 + (old_sdata_index ? 0 : 1); } while (0)
|
|
333
|
|
334 typedef unsigned char byte;
|
|
335
|
|
336 /* Round X up to a multiple of Y. */
|
|
337
|
|
338 int
|
|
339 round_up (x, y)
|
|
340 int x, y;
|
|
341 {
|
|
342 int rem = x % y;
|
|
343 if (rem == 0)
|
|
344 return x;
|
|
345 return x - rem + y;
|
|
346 }
|
|
347
|
|
348 /* ****************************************************************
|
|
349 * unexec
|
|
350 *
|
|
351 * driving logic.
|
|
352 *
|
|
353 * In ELF, this works by replacing the old .bss section with a new
|
|
354 * .data section, and inserting an empty .bss immediately afterwards.
|
|
355 *
|
|
356 */
|
|
357 void
|
|
358 unexec (new_name, old_name, data_start, bss_start, entry_address)
|
|
359 char *new_name, *old_name;
|
|
360 unsigned data_start, bss_start, entry_address;
|
|
361 {
|
|
362 extern unsigned int bss_end;
|
|
363 int new_file, old_file, new_file_size;
|
|
364
|
|
365 /* Pointers to the base of the image of the two files. */
|
|
366 caddr_t old_base, new_base;
|
|
367
|
|
368 /* Pointers to the file, program and section headers for the old and new
|
|
369 * files.
|
|
370 */
|
|
371 Elf32_Ehdr *old_file_h, *new_file_h;
|
|
372 Elf32_Phdr *old_program_h, *new_program_h;
|
|
373 Elf32_Shdr *old_section_h, *new_section_h;
|
|
374
|
|
375 /* Point to the section name table in the old file */
|
|
376 char *old_section_names;
|
|
377
|
|
378 Elf32_Addr old_bss_addr, new_bss_addr;
|
|
379 Elf32_Addr old_sbss_addr;
|
|
380 Elf32_Word old_bss_size, new_data2_size;
|
|
381 Elf32_Word old_sbss_size, new_data3_size;
|
|
382 Elf32_Off new_data2_offset;
|
|
383 Elf32_Off new_data3_offset;
|
|
384 Elf32_Addr new_data2_addr;
|
|
385 Elf32_Addr new_data3_addr;
|
|
386
|
|
387 Elf32_Word old_sdata_size, new_sdata_size;
|
|
388 int old_sdata_index = 0;
|
|
389
|
|
390 int n, nn, old_data_index, new_data2_align;
|
|
391 int old_bss_index;
|
|
392 int old_sbss_index;
|
|
393 int old_bss_padding;
|
|
394 struct stat stat_buf;
|
|
395
|
|
396 /* Open the old file & map it into the address space. */
|
|
397
|
|
398 old_file = open (old_name, O_RDONLY);
|
|
399
|
|
400 if (old_file < 0)
|
|
401 fatal ("Can't open %s for reading: errno %d\n", old_name, errno);
|
|
402
|
|
403 if (fstat (old_file, &stat_buf) == -1)
|
|
404 fatal ("Can't fstat(%s): errno %d\n", old_name, errno);
|
|
405
|
|
406 old_base = mmap (0, stat_buf.st_size, PROT_READ, MAP_SHARED, old_file, 0);
|
|
407
|
|
408 if (old_base == (caddr_t) -1)
|
|
409 fatal ("Can't mmap(%s): errno %d\n", old_name, errno);
|
|
410
|
|
411 #ifdef DEBUG
|
|
412 fprintf (stderr, "mmap(%s, %x) -> %x\n", old_name, stat_buf.st_size,
|
|
413 old_base);
|
|
414 #endif
|
|
415
|
|
416 /* Get pointers to headers & section names */
|
|
417
|
|
418 old_file_h = (Elf32_Ehdr *) old_base;
|
|
419 old_program_h = (Elf32_Phdr *) ((byte *) old_base + old_file_h->e_phoff);
|
|
420 old_section_h = (Elf32_Shdr *) ((byte *) old_base + old_file_h->e_shoff);
|
|
421 old_section_names = (char *) old_base
|
|
422 + OLD_SECTION_H(old_file_h->e_shstrndx).sh_offset;
|
|
423
|
|
424 /* Find the old .sbss section.
|
|
425 */
|
|
426
|
|
427 for (old_sbss_index = 1; old_sbss_index < old_file_h->e_shnum;
|
|
428 old_sbss_index++)
|
|
429 {
|
|
430 #ifdef DEBUG
|
|
431 fprintf (stderr, "Looking for .sbss - found %s\n",
|
|
432 old_section_names + OLD_SECTION_H(old_sbss_index).sh_name);
|
|
433 #endif
|
|
434 if (!strcmp (old_section_names + OLD_SECTION_H(old_sbss_index).sh_name,
|
|
435 ".sbss"))
|
|
436 break;
|
|
437 }
|
|
438 if (old_sbss_index == old_file_h->e_shnum)
|
|
439 fatal ("Can't find .sbss in %s.\n", old_name, 0);
|
|
440
|
|
441 if (!strcmp(old_section_names + OLD_SECTION_H(old_sbss_index - 1).sh_name,
|
|
442 ".sdata"))
|
|
443 {
|
|
444 old_sdata_index = old_sbss_index - 1;
|
|
445 }
|
|
446
|
|
447
|
|
448 /* Find the old .bss section.
|
|
449 */
|
|
450
|
|
451 for (old_bss_index = 1; old_bss_index < old_file_h->e_shnum; old_bss_index++)
|
|
452 {
|
|
453 #ifdef DEBUG
|
|
454 fprintf (stderr, "Looking for .bss - found %s\n",
|
|
455 old_section_names + OLD_SECTION_H(old_bss_index).sh_name);
|
|
456 #endif
|
|
457 if (!strcmp (old_section_names + OLD_SECTION_H(old_bss_index).sh_name,
|
|
458 ".bss"))
|
|
459 break;
|
|
460 }
|
|
461 if (old_bss_index == old_file_h->e_shnum)
|
|
462 fatal ("Can't find .bss in %s.\n", old_name, 0);
|
|
463
|
|
464 if (old_sbss_index != (old_bss_index - 1))
|
|
465 fatal (".sbss should come immediately before .bss in %s.\n", old_name, 0);
|
|
466
|
|
467 /* Figure out parameters of the new data3 and data2 sections.
|
|
468 * Change the sbss and bss sections.
|
|
469 */
|
|
470
|
|
471 old_bss_addr = OLD_SECTION_H(old_bss_index).sh_addr;
|
|
472 old_bss_size = OLD_SECTION_H(old_bss_index).sh_size;
|
|
473
|
|
474 old_sbss_addr = OLD_SECTION_H(old_sbss_index).sh_addr;
|
|
475 old_sbss_size = OLD_SECTION_H(old_sbss_index).sh_size;
|
|
476
|
|
477 if (old_sdata_index)
|
|
478 {
|
|
479 old_sdata_size = OLD_SECTION_H(old_sdata_index).sh_size;
|
|
480 }
|
|
481
|
|
482 #if defined(emacs) || !defined(DEBUG)
|
|
483 bss_end = (unsigned int) sbrk (0);
|
|
484 new_bss_addr = (Elf32_Addr) bss_end;
|
|
485 #else
|
|
486 new_bss_addr = old_bss_addr + old_bss_size + 0x1234;
|
|
487 #endif
|
|
488 if (old_sdata_index)
|
|
489 {
|
|
490 new_sdata_size = OLD_SECTION_H(old_sbss_index).sh_offset -
|
|
491 OLD_SECTION_H(old_sdata_index).sh_offset + old_sbss_size;
|
|
492 }
|
|
493
|
|
494 new_data3_addr = old_sbss_addr;
|
|
495 new_data3_size = old_sbss_size;
|
|
496 new_data3_offset = OLD_SECTION_H(old_sbss_index).sh_offset;
|
|
497
|
|
498 new_data2_addr = old_bss_addr;
|
|
499 new_data2_size = new_bss_addr - old_bss_addr;
|
|
500 new_data2_align = (new_data3_offset + old_sbss_size) %
|
|
501 OLD_SECTION_H(old_bss_index).sh_addralign;
|
|
502 new_data2_align = new_data2_align ?
|
|
503 OLD_SECTION_H(old_bss_index).sh_addralign - new_data2_align :
|
|
504 0;
|
|
505 new_data2_offset = new_data3_offset + old_sbss_size + new_data2_align;
|
|
506
|
|
507 old_bss_padding = OLD_SECTION_H(old_bss_index).sh_offset -
|
|
508 OLD_SECTION_H(old_sbss_index).sh_offset;
|
|
509 #ifdef DEBUG
|
|
510 fprintf (stderr, "old_bss_index %d\n", old_bss_index);
|
|
511 fprintf (stderr, "old_bss_addr %x\n", old_bss_addr);
|
|
512 fprintf (stderr, "old_bss_size %x\n", old_bss_size);
|
|
513 fprintf (stderr, "new_bss_addr %x\n", new_bss_addr);
|
|
514 fprintf (stderr, "new_data2_addr %x\n", new_data2_addr);
|
|
515 fprintf (stderr, "new_data2_size %x\n", new_data2_size);
|
|
516 fprintf (stderr, "new_data2_offset %x\n", new_data2_offset);
|
|
517 fprintf (stderr, "old_sbss_index %d\n", old_sbss_index);
|
|
518 fprintf (stderr, "old_sbss_addr %x\n", old_sbss_addr);
|
|
519 fprintf (stderr, "old_sbss_size %x\n", old_sbss_size);
|
|
520 if (old_sdata_index)
|
|
521 {
|
|
522 fprintf (stderr, "old_sdata_size %x\n", old_sdata_size);
|
|
523 fprintf (stderr, "new_sdata_size %x\n", new_sdata_size);
|
|
524 }
|
|
525 else
|
|
526 {
|
|
527 fprintf (stderr, "new_data3_addr %x\n", new_data3_addr);
|
|
528 fprintf (stderr, "new_data3_size %x\n", new_data3_size);
|
|
529 fprintf (stderr, "new_data3_offset %x\n", new_data3_offset);
|
|
530 }
|
|
531 #endif
|
|
532
|
|
533 if ((unsigned) new_bss_addr < (unsigned) old_bss_addr + old_bss_size)
|
|
534 fatal (".bss shrank when undumping???\n", 0, 0);
|
|
535
|
|
536 /* Set the output file to the right size and mmap(2) it. Set
|
|
537 * pointers to various interesting objects. stat_buf still has
|
|
538 * old_file data.
|
|
539 */
|
|
540
|
|
541 new_file = open (new_name, O_RDWR | O_CREAT, 0666);
|
|
542 if (new_file < 0)
|
|
543 fatal ("Can't creat(%s): errno %d\n", new_name, errno);
|
|
544
|
|
545 new_file_size = stat_buf.st_size +
|
|
546 ((1 + (old_sdata_index ? 0 : 1)) * old_file_h->e_shentsize) +
|
|
547 new_data2_size + new_data3_size + new_data2_align;
|
|
548
|
|
549 if (ftruncate (new_file, new_file_size))
|
|
550 fatal ("Can't ftruncate(%s): errno %d\n", new_name, errno);
|
|
551
|
|
552 new_base = mmap (0, new_file_size, PROT_READ | PROT_WRITE, MAP_SHARED,
|
|
553 new_file, 0);
|
|
554
|
|
555 if (new_base == (caddr_t) -1)
|
|
556 fatal ("Can't mmap(%s): errno %d\n", new_name, errno);
|
|
557
|
|
558 new_file_h = (Elf32_Ehdr *) new_base;
|
|
559 new_program_h = (Elf32_Phdr *) ((byte *) new_base + old_file_h->e_phoff);
|
|
560 new_section_h = (Elf32_Shdr *) ((byte *) new_base +
|
|
561 old_file_h->e_shoff +
|
|
562 new_data2_size +
|
|
563 new_data2_align +
|
|
564 new_data3_size);
|
|
565
|
|
566 /* Make our new file, program and section headers as copies of the
|
|
567 * originals.
|
|
568 */
|
|
569
|
|
570 memcpy (new_file_h, old_file_h, old_file_h->e_ehsize);
|
|
571 memcpy (new_program_h, old_program_h,
|
|
572 old_file_h->e_phnum * old_file_h->e_phentsize);
|
|
573
|
|
574 /* Modify the e_shstrndx if necessary. */
|
|
575 PATCH_INDEX (new_file_h->e_shstrndx);
|
|
576
|
|
577 /* Fix up file header. We'll add one section. Section header is
|
|
578 * further away now.
|
|
579 */
|
|
580
|
|
581 new_file_h->e_shoff += new_data2_size + new_data2_align + new_data3_size;
|
|
582 new_file_h->e_shnum += 1 + (old_sdata_index ? 0 : 1);
|
|
583
|
|
584 #ifdef DEBUG
|
|
585 fprintf (stderr, "Old section offset %x\n", old_file_h->e_shoff);
|
|
586 fprintf (stderr, "Old section count %d\n", old_file_h->e_shnum);
|
|
587 fprintf (stderr, "New section offset %x\n", new_file_h->e_shoff);
|
|
588 fprintf (stderr, "New section count %d\n", new_file_h->e_shnum);
|
|
589 #endif
|
|
590
|
|
591 /* Fix up a new program header. Extend the writable data segment so
|
|
592 * that the bss area is covered too. Find that segment by looking
|
|
593 * for a segment that ends just before the .bss area. Make sure
|
|
594 * that no segments are above the new .data2. Put a loop at the end
|
|
595 * to adjust the offset and address of any segment that is above
|
|
596 * data2, just in case we decide to allow this later.
|
|
597 */
|
|
598
|
|
599 for (n = new_file_h->e_phnum - 1; n >= 0; n--)
|
|
600 {
|
|
601 /* Compute maximum of all requirements for alignment of section. */
|
|
602 int alignment = (NEW_PROGRAM_H (n)).p_align;
|
|
603 if ((OLD_SECTION_H (old_bss_index)).sh_addralign > alignment)
|
|
604 alignment = OLD_SECTION_H (old_bss_index).sh_addralign;
|
|
605
|
|
606 if ((OLD_SECTION_H (old_sbss_index)).sh_addralign > alignment)
|
|
607 alignment = OLD_SECTION_H (old_sbss_index).sh_addralign;
|
|
608
|
|
609 /* Supposedly this condition is okay for the SGI. */
|
|
610 #if 0
|
|
611 if (NEW_PROGRAM_H(n).p_vaddr + NEW_PROGRAM_H(n).p_filesz > old_bss_addr)
|
|
612 fatal ("Program segment above .bss in %s\n", old_name, 0);
|
|
613 #endif
|
|
614
|
|
615 if (NEW_PROGRAM_H(n).p_type == PT_LOAD
|
|
616 && (round_up ((NEW_PROGRAM_H (n)).p_vaddr
|
|
617 + (NEW_PROGRAM_H (n)).p_filesz,
|
|
618 alignment)
|
|
619 == round_up (old_bss_addr, alignment)))
|
|
620 break;
|
|
621 }
|
|
622 if (n < 0)
|
|
623 fatal ("Couldn't find segment next to .bss in %s\n", old_name, 0);
|
|
624
|
|
625 NEW_PROGRAM_H(n).p_filesz += new_data2_size + new_data2_align +
|
|
626 new_data3_size;
|
|
627 NEW_PROGRAM_H(n).p_memsz = NEW_PROGRAM_H(n).p_filesz;
|
|
628
|
|
629 #if 1 /* Maybe allow section after data2 - does this ever happen? */
|
|
630 for (n = new_file_h->e_phnum - 1; n >= 0; n--)
|
|
631 {
|
|
632 if (NEW_PROGRAM_H(n).p_vaddr
|
|
633 && NEW_PROGRAM_H(n).p_vaddr >= new_data3_addr)
|
|
634 NEW_PROGRAM_H(n).p_vaddr += new_data2_size - old_bss_size +
|
|
635 new_data3_size - old_sbss_size;
|
|
636
|
|
637 if (NEW_PROGRAM_H(n).p_offset >= new_data3_offset)
|
|
638 NEW_PROGRAM_H(n).p_offset += new_data2_size + new_data2_align +
|
|
639 new_data3_size;
|
|
640 }
|
|
641 #endif
|
|
642
|
|
643 /* Fix up section headers based on new .data2 section. Any section
|
|
644 * whose offset or virtual address is after the new .data2 section
|
|
645 * gets its value adjusted. .bss size becomes zero and new address
|
|
646 * is set. data2 section header gets added by copying the existing
|
|
647 * .data header and modifying the offset, address and size.
|
|
648 */
|
|
649 for (old_data_index = 1; old_data_index < old_file_h->e_shnum;
|
|
650 old_data_index++)
|
|
651 if (!strcmp (old_section_names + OLD_SECTION_H(old_data_index).sh_name,
|
|
652 ".data"))
|
|
653 break;
|
|
654 if (old_data_index == old_file_h->e_shnum)
|
|
655 fatal ("Can't find .data in %s.\n", old_name, 0);
|
|
656
|
|
657 /* Walk through all section headers, insert the new data2 section right
|
|
658 before the new bss section. */
|
|
659 for (n = 1, nn = 1; n < old_file_h->e_shnum; n++, nn++)
|
|
660 {
|
|
661 caddr_t src;
|
|
662
|
|
663 if (n == old_sbss_index)
|
|
664
|
|
665 /* If it is sbss section, insert the new data3 section before it. */
|
|
666 {
|
|
667 /* Steal the data section header for this data3 section. */
|
|
668 if (!old_sdata_index)
|
|
669 {
|
|
670 memcpy (&NEW_SECTION_H(nn), &OLD_SECTION_H(old_data_index),
|
|
671 new_file_h->e_shentsize);
|
|
672
|
|
673 NEW_SECTION_H(nn).sh_addr = new_data3_addr;
|
|
674 NEW_SECTION_H(nn).sh_offset = new_data3_offset;
|
|
675 NEW_SECTION_H(nn).sh_size = new_data3_size;
|
|
676 NEW_SECTION_H(nn).sh_flags = OLD_SECTION_H(n).sh_flags;
|
|
677 /* Use the sbss section's alignment. This will assure that the
|
|
678 new data3 section always be placed in the same spot as the old
|
|
679 sbss section by any other application. */
|
|
680 NEW_SECTION_H(nn).sh_addralign = OLD_SECTION_H(n).sh_addralign;
|
|
681
|
|
682 /* Now copy over what we have in the memory now. */
|
|
683 memcpy (NEW_SECTION_H(nn).sh_offset + new_base,
|
|
684 (caddr_t) OLD_SECTION_H(n).sh_addr,
|
|
685 new_data3_size);
|
|
686 /* the new .data2 section should also come before the
|
|
687 * new .sbss section */
|
|
688 nn += 2;
|
|
689 }
|
|
690 else
|
|
691 {
|
|
692 /* We always have a .sdata section: append the contents of the
|
|
693 * old .sbss section.
|
|
694 */
|
|
695 memcpy (new_data3_offset + new_base,
|
|
696 (caddr_t) OLD_SECTION_H(n).sh_addr,
|
|
697 new_data3_size);
|
|
698 nn ++;
|
|
699 }
|
|
700 }
|
|
701 else if (n == old_bss_index)
|
|
702
|
|
703 /* If it is bss section, insert the new data2 section before it. */
|
|
704 {
|
|
705 Elf32_Word tmp_align;
|
|
706 Elf32_Addr tmp_addr;
|
|
707
|
|
708 tmp_align = OLD_SECTION_H(n).sh_addralign;
|
|
709 tmp_addr = OLD_SECTION_H(n).sh_addr;
|
|
710
|
|
711 nn -= 2;
|
|
712 /* Steal the data section header for this data2 section. */
|
|
713 memcpy (&NEW_SECTION_H(nn), &OLD_SECTION_H(old_data_index),
|
|
714 new_file_h->e_shentsize);
|
|
715
|
|
716 NEW_SECTION_H(nn).sh_addr = new_data2_addr;
|
|
717 NEW_SECTION_H(nn).sh_offset = new_data2_offset;
|
|
718 NEW_SECTION_H(nn).sh_size = new_data2_size;
|
|
719 /* Use the bss section's alignment. This will assure that the
|
|
720 new data2 section always be placed in the same spot as the old
|
|
721 bss section by any other application. */
|
|
722 NEW_SECTION_H(nn).sh_addralign = tmp_align;
|
|
723
|
|
724 /* Now copy over what we have in the memory now. */
|
|
725 memcpy (NEW_SECTION_H(nn).sh_offset + new_base,
|
|
726 (caddr_t) tmp_addr, new_data2_size);
|
|
727 nn += 2;
|
|
728 }
|
|
729
|
|
730 memcpy (&NEW_SECTION_H(nn), &OLD_SECTION_H(n),
|
|
731 old_file_h->e_shentsize);
|
|
732
|
|
733 if (old_sdata_index && n == old_sdata_index)
|
|
734 /* The old .sdata section has now a new size */
|
|
735 NEW_SECTION_H(nn).sh_size = new_sdata_size;
|
|
736
|
|
737 /* The new bss section's size is zero, and its file offset and virtual
|
|
738 address should be off by NEW_DATA2_SIZE. */
|
|
739 if (n == old_sbss_index)
|
|
740 {
|
|
741 /* NN should be `old_sbss_index + 2' at this point. */
|
|
742 NEW_SECTION_H(nn).sh_offset += new_data2_size + new_data2_align +
|
|
743 new_data3_size;
|
|
744 NEW_SECTION_H(nn).sh_addr += new_data2_size + new_data2_align +
|
|
745 new_data3_size;
|
|
746 /* Let the new bss section address alignment be the same as the
|
|
747 section address alignment followed the old bss section, so
|
|
748 this section will be placed in exactly the same place. */
|
|
749 NEW_SECTION_H(nn).sh_addralign =
|
|
750 OLD_SECTION_H(nn + (old_sdata_index ? 1 : 0)).sh_addralign;
|
|
751 NEW_SECTION_H(nn).sh_size = 0;
|
|
752 }
|
|
753 else if (n == old_bss_index)
|
|
754 {
|
|
755 /* NN should be `old_bss_index + 2' at this point. */
|
|
756 NEW_SECTION_H(nn).sh_offset += new_data2_size + new_data2_align +
|
|
757 new_data3_size - old_bss_padding;
|
|
758 NEW_SECTION_H(nn).sh_addr += new_data2_size;
|
|
759 /* Let the new bss section address alignment be the same as the
|
|
760 section address alignment followed the old bss section, so
|
|
761 this section will be placed in exactly the same place. */
|
|
762 NEW_SECTION_H(nn).sh_addralign =
|
|
763 OLD_SECTION_H((nn - (old_sdata_index ? 0 : 1))).sh_addralign;
|
|
764 NEW_SECTION_H(nn).sh_size = 0;
|
|
765 }
|
|
766 /* Any section that was original placed AFTER the bss section should now
|
|
767 be off by NEW_DATA2_SIZE. */
|
|
768 else if (NEW_SECTION_H(nn).sh_offset >= new_data3_offset)
|
|
769 NEW_SECTION_H(nn).sh_offset += new_data2_size +
|
|
770 new_data2_align +
|
|
771 new_data3_size -
|
|
772 old_bss_padding;
|
|
773
|
|
774 /* If any section hdr refers to the section after the new .data
|
|
775 section, make it refer to next one because we have inserted
|
|
776 a new section in between. */
|
|
777
|
|
778 PATCH_INDEX(NEW_SECTION_H(nn).sh_link);
|
|
779 PATCH_INDEX(NEW_SECTION_H(nn).sh_info);
|
|
780
|
|
781 /* Now, start to copy the content of sections. */
|
|
782 if (NEW_SECTION_H(nn).sh_type == SHT_NULL
|
|
783 || NEW_SECTION_H(nn).sh_type == SHT_NOBITS)
|
|
784 continue;
|
|
785
|
|
786 /* Write out the sections. .data, .data1 and .sdata get copied from
|
|
787 * the current process instead of the old file.
|
|
788 */
|
|
789 if (!strcmp (old_section_names + OLD_SECTION_H(n).sh_name, ".data") ||
|
|
790 !strcmp (old_section_names + OLD_SECTION_H(n).sh_name, ".data1") ||
|
|
791 (old_sdata_index && (n == old_sdata_index)))
|
|
792 src = (caddr_t) OLD_SECTION_H(n).sh_addr;
|
|
793 else
|
|
794 src = old_base + OLD_SECTION_H(n).sh_offset;
|
|
795
|
|
796 memcpy (NEW_SECTION_H(nn).sh_offset + new_base, src,
|
|
797 ((n == old_sdata_index) ?
|
|
798 old_sdata_size :
|
|
799 NEW_SECTION_H(nn).sh_size));
|
|
800
|
|
801 /* If it is the symbol table, its st_shndx field needs to be patched. */
|
|
802 if (NEW_SECTION_H(nn).sh_type == SHT_SYMTAB
|
|
803 || NEW_SECTION_H(nn).sh_type == SHT_DYNSYM)
|
|
804 {
|
|
805 Elf32_Shdr *spt = &NEW_SECTION_H(nn);
|
|
806 unsigned int num = spt->sh_size / spt->sh_entsize;
|
|
807 Elf32_Sym * sym = (Elf32_Sym *) (NEW_SECTION_H(nn).sh_offset +
|
|
808 new_base);
|
|
809 for (; num--; sym++)
|
|
810 {
|
|
811 if ((sym->st_shndx == SHN_UNDEF)
|
|
812 || (sym->st_shndx == SHN_ABS)
|
|
813 || (sym->st_shndx == SHN_COMMON))
|
|
814 continue;
|
|
815
|
|
816 PATCH_INDEX(sym->st_shndx);
|
|
817 }
|
|
818 }
|
|
819 }
|
|
820
|
|
821 /* Close the files and make the new file executable */
|
|
822
|
|
823 if (close (old_file))
|
|
824 fatal ("Can't close(%s): errno %d\n", old_name, errno);
|
|
825
|
|
826 if (close (new_file))
|
|
827 fatal ("Can't close(%s): errno %d\n", new_name, errno);
|
|
828
|
|
829 if (stat (new_name, &stat_buf) == -1)
|
|
830 fatal ("Can't stat(%s): errno %d\n", new_name, errno);
|
|
831
|
|
832 n = umask (777);
|
|
833 umask (n);
|
|
834 stat_buf.st_mode |= 0111 & ~n;
|
|
835 if (chmod (new_name, stat_buf.st_mode) == -1)
|
|
836 fatal ("Can't chmod(%s): errno %d\n", new_name, errno);
|
|
837 }
|