Mercurial > emacs
annotate src/unexnext.c @ 95948:d55ec23f052d
*** empty log message ***
| author | Glenn Morris <rgm@gnu.org> |
|---|---|
| date | Sun, 15 Jun 2008 02:53:17 +0000 |
| parents | 8971ddf55736 |
| children |
| rev | line source |
|---|---|
| 1349 | 1 /* Dump Emacs in macho format. |
|
75227
e90d04cd455a
Update copyright for years from Emacs 21 to present (mainly adding
Glenn Morris <rgm@gnu.org>
parents:
68651
diff
changeset
|
2 Copyright (C) 1990, 1993, 2001, 2002, 2003, 2004, |
| 79759 | 3 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
| 1349 | 4 Written by Bradley Taylor (btaylor@next.com). |
| 5 | |
| 6 This file is part of GNU Emacs. | |
| 7 | |
|
94963
8971ddf55736
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79759
diff
changeset
|
8 GNU Emacs is free software: you can redistribute it and/or modify |
| 1349 | 9 it under the terms of the GNU General Public License as published by |
|
94963
8971ddf55736
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79759
diff
changeset
|
10 the Free Software Foundation, either version 3 of the License, or |
|
8971ddf55736
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79759
diff
changeset
|
11 (at your option) any later version. |
| 1349 | 12 |
| 13 GNU Emacs is distributed in the hope that it will be useful, | |
| 14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 16 GNU General Public License for more details. | |
| 17 | |
| 18 You should have received a copy of the GNU General Public License | |
|
94963
8971ddf55736
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79759
diff
changeset
|
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
| 1349 | 20 |
| 21 | |
| 22 #undef __STRICT_BSD__ | |
| 23 | |
| 24 #include <stdio.h> | |
| 25 #include <stdlib.h> | |
| 26 #include <stdarg.h> | |
|
2660
b70f4760d769
Changes for Emacs 19 from Thorsten Ohl <ohl@chico.harvard.edu>:
Jim Blandy <jimb@redhat.com>
parents:
1349
diff
changeset
|
27 #include <mach/mach.h> |
|
b70f4760d769
Changes for Emacs 19 from Thorsten Ohl <ohl@chico.harvard.edu>:
Jim Blandy <jimb@redhat.com>
parents:
1349
diff
changeset
|
28 #include <mach-o/loader.h> |
|
16853
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
29 #include <mach-o/reloc.h> |
| 1349 | 30 #include <sys/file.h> |
| 31 #include <sys/stat.h> | |
|
19669
e8e724dad99a
Include unistd.h instead of libc.h.
Richard M. Stallman <rms@gnu.org>
parents:
16853
diff
changeset
|
32 #include <unistd.h> |
| 19670 | 33 /* Instead of unistd.h, this used to include libc.h. |
| 34 "Nelson H. F. Beebe" <beebe@math.utah.edu> says that doesn't work | |
| 35 in system version 3.3. */ | |
| 1349 | 36 |
| 37 | |
|
2660
b70f4760d769
Changes for Emacs 19 from Thorsten Ohl <ohl@chico.harvard.edu>:
Jim Blandy <jimb@redhat.com>
parents:
1349
diff
changeset
|
38 int malloc_cookie; |
| 1349 | 39 |
| 40 /* | |
| 41 * Kludge: we don't expect any program data beyond VM_HIGHDATA | |
| 42 * What is really needed is a way to find out from malloc() which | |
| 43 * pages it vm_allocated and write only those out into the data segment. | |
| 44 * | |
| 45 * This kludge may break when we stop using fixed virtual address | |
|
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
46 * shared libraries. Actually, emacs will probably continue working, but be |
| 1349 | 47 * much larger on disk than it needs to be (because non-malloced data will |
| 48 * be in the file). | |
| 49 */ | |
| 50 static const unsigned VM_HIGHDATA = 0x2000000; | |
| 51 | |
| 52 typedef struct region_t { | |
| 53 vm_address_t address; | |
| 54 vm_size_t size; | |
| 55 vm_prot_t protection; | |
| 56 vm_prot_t max_protection; | |
| 57 vm_inherit_t inheritance; | |
| 58 boolean_t shared; | |
| 59 port_t object_name; | |
| 60 vm_offset_t offset; | |
| 61 } region_t; | |
| 62 | |
| 63 | |
| 64 static void | |
| 65 grow( | |
| 66 struct load_command ***the_commands, | |
| 67 unsigned *the_commands_len | |
| 68 ) | |
| 69 { | |
| 70 if (*the_commands == NULL) { | |
| 71 *the_commands_len = 1; | |
| 72 *the_commands = malloc(sizeof(*the_commands)); | |
| 73 } else { | |
| 74 (*the_commands_len)++; | |
|
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
75 *the_commands = realloc(*the_commands, |
| 1349 | 76 (*the_commands_len * |
| 77 sizeof(**the_commands))); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 | |
| 82 static void | |
| 83 save_command( | |
| 84 struct load_command *command, | |
| 85 struct load_command ***the_commands, | |
| 86 unsigned *the_commands_len | |
| 87 ) | |
| 88 { | |
| 89 struct load_command **tmp; | |
| 90 | |
| 91 grow(the_commands, the_commands_len); | |
| 92 tmp = &(*the_commands)[*the_commands_len - 1]; | |
| 93 *tmp = malloc(command->cmdsize); | |
| 94 bcopy(command, *tmp, command->cmdsize); | |
| 95 } | |
| 96 | |
| 97 static void | |
| 98 fatal_unexec(char *format, ...) | |
| 99 { | |
| 100 va_list ap; | |
| 101 | |
| 102 va_start(ap, format); | |
| 103 fprintf(stderr, "unexec: "); | |
| 104 vfprintf(stderr, format, ap); | |
| 105 fprintf(stderr, "\n"); | |
| 106 va_end(ap); | |
| 107 } | |
| 108 | |
| 109 static int | |
| 110 read_macho( | |
| 111 int fd, | |
| 112 struct mach_header *the_header, | |
| 113 struct load_command ***the_commands, | |
| 114 unsigned *the_commands_len | |
| 115 ) | |
| 116 { | |
| 117 struct load_command command; | |
| 118 struct load_command *buf; | |
| 119 int i; | |
| 120 int size; | |
| 121 | |
| 122 if (read(fd, the_header, sizeof(*the_header)) != sizeof(*the_header)) { | |
| 123 fatal_unexec("cannot read macho header"); | |
| 124 return (0); | |
| 125 } | |
| 126 for (i = 0; i < the_header->ncmds; i++) { | |
|
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
127 if (read(fd, &command, sizeof(struct load_command)) != |
| 1349 | 128 sizeof(struct load_command)) { |
| 129 fatal_unexec("cannot read macho load command header"); | |
| 130 return (0); | |
| 131 } | |
| 132 size = command.cmdsize - sizeof(struct load_command); | |
| 133 if (size < 0) { | |
| 134 fatal_unexec("bogus load command size"); | |
| 135 return (0); | |
| 136 } | |
| 137 buf = malloc(command.cmdsize); | |
| 138 buf->cmd = command.cmd; | |
| 139 buf->cmdsize = command.cmdsize; | |
|
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
140 if (read(fd, ((char *)buf + |
|
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
141 sizeof(struct load_command)), |
| 1349 | 142 size) != size) { |
| 143 fatal_unexec("cannot read load command data"); | |
| 144 return (0); | |
| 145 } | |
| 146 save_command(buf, the_commands, the_commands_len); | |
| 147 } | |
| 148 return (1); | |
| 149 } | |
| 150 | |
| 151 static int | |
| 152 filldatagap( | |
| 153 vm_address_t start_address, | |
| 154 vm_size_t *size, | |
| 155 vm_address_t end_address | |
| 156 ) | |
| 157 { | |
| 158 vm_address_t address; | |
| 159 vm_size_t gapsize; | |
| 160 | |
| 161 address = (start_address + *size); | |
| 162 gapsize = end_address - address; | |
| 163 *size += gapsize; | |
| 164 if (vm_allocate(task_self(), &address, gapsize, | |
| 165 FALSE) != KERN_SUCCESS) { | |
| 166 fatal_unexec("cannot vm_allocate"); | |
| 167 return (0); | |
| 168 } | |
| 169 return (1); | |
| 170 } | |
| 171 | |
| 172 static int | |
| 173 get_data_region( | |
| 174 vm_address_t *address, | |
| 175 vm_size_t *size | |
| 176 ) | |
| 177 { | |
| 178 region_t region; | |
| 179 kern_return_t ret; | |
| 180 struct section *sect; | |
| 181 | |
|
4598
cec168152d88
(get_data_region): Add cast to avoid warning.
Richard M. Stallman <rms@gnu.org>
parents:
2660
diff
changeset
|
182 sect = (struct section *) getsectbyname(SEG_DATA, SECT_DATA); |
| 1349 | 183 region.address = 0; |
| 184 *address = 0; | |
| 185 for (;;) { | |
|
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
186 ret = vm_region(task_self(), |
|
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
187 ®ion.address, |
|
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
188 ®ion.size, |
|
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
189 ®ion.protection, |
|
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
190 ®ion.max_protection, |
| 1349 | 191 ®ion.inheritance, |
|
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
192 ®ion.shared, |
|
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
193 ®ion.object_name, |
| 1349 | 194 ®ion.offset); |
| 195 if (ret != KERN_SUCCESS || region.address >= VM_HIGHDATA) { | |
| 196 break; | |
| 197 } | |
| 198 if (*address != 0) { | |
| 199 if (region.address > *address + *size) { | |
|
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
200 if (!filldatagap(*address, size, |
| 1349 | 201 region.address)) { |
| 202 return (0); | |
| 203 } | |
|
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
204 } |
| 1349 | 205 *size += region.size; |
| 206 } else { | |
| 207 if (region.address == sect->addr) { | |
| 208 *address = region.address; | |
| 209 *size = region.size; | |
|
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
210 } |
| 1349 | 211 } |
| 212 region.address += region.size; | |
| 213 } | |
| 214 return (1); | |
| 215 } | |
| 216 | |
| 217 static char * | |
| 218 my_malloc( | |
| 219 vm_size_t size | |
| 220 ) | |
| 221 { | |
| 222 vm_address_t address; | |
| 223 | |
| 224 if (vm_allocate(task_self(), &address, size, TRUE) != KERN_SUCCESS) { | |
| 225 return (NULL); | |
| 226 } | |
| 227 return ((char *)address); | |
| 228 } | |
| 229 | |
| 230 static void | |
| 231 my_free( | |
| 232 char *buf, | |
| 233 vm_size_t size | |
| 234 ) | |
| 235 { | |
| 236 vm_deallocate(task_self(), (vm_address_t)buf, size); | |
| 237 } | |
| 238 | |
| 239 static int | |
| 240 unexec_doit( | |
| 241 int infd, | |
| 242 int outfd | |
| 243 ) | |
| 244 { | |
| 245 int i; | |
| 246 struct load_command **the_commands = NULL; | |
| 247 unsigned the_commands_len; | |
| 248 struct mach_header the_header; | |
|
16853
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
249 int fgrowth = 0; |
| 1349 | 250 int fdatastart; |
| 251 int fdatasize; | |
| 252 int size; | |
| 253 struct stat st; | |
| 254 char *buf; | |
| 255 vm_address_t data_address; | |
| 256 vm_size_t data_size; | |
|
16853
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
257 vm_size_t vmaddr_growth = 0; |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
258 vm_size_t dataseg_vmaddr, dataseg_vmend; |
| 1349 | 259 |
| 260 struct segment_command *segment; | |
| 261 | |
|
16853
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
262 #ifdef NS_TARGET |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
263 unsigned long extreloff = 0; |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
264 unsigned long nextrel = 0; |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
265 struct dysymtab_command *dysymtab; |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
266 struct relocation_info reloc_info; |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
267 #endif |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
268 |
| 1349 | 269 if (!read_macho(infd, &the_header, &the_commands, &the_commands_len)) { |
| 270 return (0); | |
| 271 } | |
| 272 | |
| 273 | |
|
2660
b70f4760d769
Changes for Emacs 19 from Thorsten Ohl <ohl@chico.harvard.edu>:
Jim Blandy <jimb@redhat.com>
parents:
1349
diff
changeset
|
274 malloc_cookie = malloc_freezedry (); |
| 1349 | 275 if (!get_data_region(&data_address, &data_size)) { |
| 276 return (0); | |
| 277 } | |
| 278 | |
| 279 | |
| 280 /* | |
| 281 * DO NOT USE MALLOC IN THIS SECTION | |
| 282 */ | |
| 283 { | |
| 284 /* | |
| 285 * Fix offsets | |
| 286 */ | |
| 287 for (i = 0; i < the_commands_len; i++) { | |
| 288 switch (the_commands[i]->cmd) { | |
| 289 case LC_SEGMENT: | |
| 290 segment = ((struct segment_command *) | |
| 291 the_commands[i]); | |
| 292 if (strcmp(segment->segname, SEG_DATA) == 0) { | |
| 293 fdatastart = segment->fileoff; | |
| 294 fdatasize = segment->filesize; | |
|
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
295 fgrowth = (data_size - |
| 1349 | 296 segment->filesize); |
| 297 segment->vmsize = data_size; | |
| 298 segment->filesize = data_size; | |
|
16853
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
299 dataseg_vmaddr = segment->vmaddr; |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
300 dataseg_vmend = segment->vmaddr + segment->vmsize; |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
301 vmaddr_growth = segment->vmaddr + segment->vmsize; |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
302 } else { |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
303 ((struct segment_command *)the_commands[i])->fileoff += fgrowth; |
| 1349 | 304 } |
|
16853
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
305 |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
306 if( strcmp( segment->segname, SEG_LINKEDIT ) == 0 ) { |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
307 segment->vmaddr = vmaddr_growth; |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
308 } |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
309 |
| 1349 | 310 break; |
| 311 case LC_SYMTAB: | |
| 312 ((struct symtab_command *) | |
| 313 the_commands[i])->symoff += fgrowth; | |
| 314 ((struct symtab_command *) | |
| 315 the_commands[i])->stroff += fgrowth; | |
| 316 break; | |
| 317 case LC_SYMSEG: | |
| 318 ((struct symseg_command *) | |
| 319 the_commands[i])->offset += fgrowth; | |
| 320 break; | |
|
16853
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
321 #ifdef NS_TARGET |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
322 case LC_DYSYMTAB: |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
323 dysymtab = ((struct dysymtab_command *)the_commands[i]); |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
324 extreloff = dysymtab->extreloff; |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
325 nextrel = dysymtab->nextrel; |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
326 dysymtab->indirectsymoff += fgrowth; |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
327 dysymtab->extreloff += fgrowth; |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
328 break; |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
329 #endif |
| 1349 | 330 default: |
| 331 break; | |
| 332 } | |
| 333 } | |
|
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
334 |
| 1349 | 335 /* |
| 336 * Write header | |
| 337 */ | |
|
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
338 if (write(outfd, &the_header, |
| 1349 | 339 sizeof(the_header)) != sizeof(the_header)) { |
| 340 fatal_unexec("cannot write output file"); | |
| 341 return (0); | |
| 342 } | |
|
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
343 |
| 1349 | 344 /* |
| 345 * Write commands | |
| 346 */ | |
| 347 for (i = 0; i < the_commands_len; i++) { | |
|
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
348 if (write(outfd, the_commands[i], |
|
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
349 the_commands[i]->cmdsize) != |
| 1349 | 350 the_commands[i]->cmdsize) { |
| 351 fatal_unexec("cannot write output file"); | |
| 352 return (0); | |
| 353 } | |
| 354 } | |
|
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
355 |
| 1349 | 356 /* |
| 357 * Write original text | |
| 358 */ | |
|
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
359 if (lseek(infd, the_header.sizeofcmds + sizeof(the_header), |
| 1349 | 360 L_SET) < 0) { |
| 361 fatal_unexec("cannot seek input file"); | |
| 362 return (0); | |
| 363 } | |
|
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
364 size = fdatastart - (sizeof(the_header) + |
| 1349 | 365 the_header.sizeofcmds); |
| 366 buf = my_malloc(size); | |
| 367 if (read(infd, buf, size) != size) { | |
| 368 my_free(buf, size); | |
| 369 fatal_unexec("cannot read input file"); | |
| 370 } | |
| 371 if (write(outfd, buf, size) != size) { | |
| 372 my_free(buf, size); | |
| 373 fatal_unexec("cannot write output file"); | |
| 374 return (0); | |
| 375 } | |
| 376 my_free(buf, size); | |
|
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
377 |
|
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
378 |
| 1349 | 379 /* |
| 380 * Write new data | |
| 381 */ | |
|
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
382 if (write(outfd, (char *)data_address, |
| 1349 | 383 data_size) != data_size) { |
| 384 fatal_unexec("cannot write output file"); | |
| 385 return (0); | |
| 386 } | |
|
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
387 |
| 1349 | 388 } |
| 389 | |
| 390 /* | |
| 391 * OKAY TO USE MALLOC NOW | |
| 392 */ | |
| 393 | |
| 394 /* | |
| 395 * Write rest of file | |
| 396 */ | |
| 397 fstat(infd, &st); | |
| 398 if (lseek(infd, fdatasize, L_INCR) < 0) { | |
| 399 fatal_unexec("cannot seek input file"); | |
| 400 return (0); | |
| 401 } | |
| 402 size = st.st_size - lseek(infd, 0, L_INCR); | |
| 403 | |
| 404 buf = malloc(size); | |
| 405 if (read(infd, buf, size) != size) { | |
| 406 free(buf); | |
| 407 fatal_unexec("cannot read input file"); | |
| 408 return (0); | |
| 409 } | |
| 410 if (write(outfd, buf, size) != size) { | |
| 411 free(buf); | |
| 412 fatal_unexec("cannot write output file"); | |
| 413 return (0); | |
| 414 } | |
| 415 free(buf); | |
|
16853
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
416 |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
417 #ifdef NS_TARGET |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
418 /* |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
419 * Fix up relocation entries in the data segment. |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
420 */ |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
421 |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
422 if (lseek(infd, extreloff, L_SET) < 0) { |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
423 fatal_unexec("cannot seek input file"); |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
424 return (0); |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
425 } |
|
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
426 |
|
16853
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
427 for (i = 0; i < nextrel; i++) |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
428 { |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
429 long zeroval = 0; |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
430 |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
431 if (read(infd, &reloc_info, sizeof (reloc_info)) != sizeof (reloc_info)) { |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
432 fatal_unexec("cannot read input file"); |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
433 return (0); |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
434 } |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
435 if (reloc_info.r_address >= dataseg_vmaddr && reloc_info.r_address < dataseg_vmend) |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
436 { |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
437 if (lseek (outfd, fdatastart + reloc_info.r_address - dataseg_vmaddr, L_SET) < 0 ) { |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
438 fatal_unexec("cannot seek input file"); |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
439 return (0); |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
440 } |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
441 switch (reloc_info.r_length) { |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
442 case 0: |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
443 if (write(outfd, &zeroval, 1) != 1) { |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
444 fatal_unexec("cannot write output file"); |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
445 return (0); |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
446 } |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
447 break; |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
448 case 1: |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
449 if (write(outfd, &zeroval, 2) != 2) { |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
450 fatal_unexec("cannot write output file"); |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
451 return (0); |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
452 } |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
453 break; |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
454 case 2: |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
455 if (write(outfd, &zeroval, 4) != 4) { |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
456 fatal_unexec("cannot write output file"); |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
457 return (0); |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
458 } |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
459 break; |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
460 } |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
461 } |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
462 } |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
463 #endif |
|
63f2ad395c81
Include <mach-o/reloc.h>.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
464 |
| 1349 | 465 return (1); |
| 466 } | |
| 467 | |
| 468 void | |
| 469 unexec( | |
| 470 char *outfile, | |
| 471 char *infile | |
| 472 ) | |
| 473 { | |
| 474 int infd; | |
| 475 int outfd; | |
| 476 char tmpbuf[L_tmpnam]; | |
| 477 char *tmpfile; | |
| 478 | |
| 479 infd = open(infile, O_RDONLY, 0); | |
| 480 if (infd < 0) { | |
| 481 fatal_unexec("cannot open input file `%s'", infile); | |
| 482 exit(1); | |
| 483 } | |
|
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
19670
diff
changeset
|
484 |
| 1349 | 485 tmpnam(tmpbuf); |
| 486 tmpfile = rindex(tmpbuf, '/'); | |
| 487 if (tmpfile == NULL) { | |
| 488 tmpfile = tmpbuf; | |
| 489 } else { | |
| 490 tmpfile++; | |
| 491 } | |
| 492 outfd = open(tmpfile, O_WRONLY|O_TRUNC|O_CREAT, 0755); | |
| 493 if (outfd < 0) { | |
| 494 close(infd); | |
| 495 fatal_unexec("cannot open tmp file `%s'", tmpfile); | |
| 496 exit(1); | |
| 497 } | |
| 498 if (!unexec_doit(infd, outfd)) { | |
| 499 close(infd); | |
| 500 close(outfd); | |
| 501 unlink(tmpfile); | |
| 502 exit(1); | |
| 503 } | |
| 504 close(infd); | |
| 505 close(outfd); | |
| 506 if (rename(tmpfile, outfile) < 0) { | |
| 507 unlink(tmpfile); | |
| 508 fatal_unexec("cannot rename `%s' to `%s'", tmpfile, outfile); | |
| 509 exit(1); | |
| 510 } | |
| 511 } | |
| 52401 | 512 |
| 513 /* arch-tag: 9796bdc3-c050-417a-b2f5-4cfd31032634 | |
| 514 (do not change this comment) */ |
