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