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