(locate-dominating-file): Accept non-existing argument.
[emacs.git] / src / unexnext.c
blob3ed54a59db8294d05bbe9670cadb75ff899343c3
1 /* Dump Emacs in macho format.
2 Copyright (C) 1990, 1993, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4 Written by Bradley Taylor (btaylor@next.com).
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 #undef __STRICT_BSD__
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <mach/mach.h>
28 #include <mach-o/loader.h>
29 #include <mach-o/reloc.h>
30 #include <sys/file.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
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. */
38 int malloc_cookie;
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.
45 * This kludge may break when we stop using fixed virtual address
46 * shared libraries. Actually, emacs will probably continue working, but be
47 * much larger on disk than it needs to be (because non-malloced data will
48 * be in the file).
50 static const unsigned VM_HIGHDATA = 0x2000000;
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;
64 static void
65 grow(
66 struct load_command ***the_commands,
67 unsigned *the_commands_len
70 if (*the_commands == NULL) {
71 *the_commands_len = 1;
72 *the_commands = malloc(sizeof(*the_commands));
73 } else {
74 (*the_commands_len)++;
75 *the_commands = realloc(*the_commands,
76 (*the_commands_len *
77 sizeof(**the_commands)));
82 static void
83 save_command(
84 struct load_command *command,
85 struct load_command ***the_commands,
86 unsigned *the_commands_len
89 struct load_command **tmp;
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);
97 static void
98 fatal_unexec(char *format, ...)
100 va_list ap;
102 va_start(ap, format);
103 fprintf(stderr, "unexec: ");
104 vfprintf(stderr, format, ap);
105 fprintf(stderr, "\n");
106 va_end(ap);
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
117 struct load_command command;
118 struct load_command *buf;
119 int i;
120 int size;
122 if (read(fd, the_header, sizeof(*the_header)) != sizeof(*the_header)) {
123 fatal_unexec("cannot read macho header");
124 return (0);
126 for (i = 0; i < the_header->ncmds; i++) {
127 if (read(fd, &command, sizeof(struct load_command)) !=
128 sizeof(struct load_command)) {
129 fatal_unexec("cannot read macho load command header");
130 return (0);
132 size = command.cmdsize - sizeof(struct load_command);
133 if (size < 0) {
134 fatal_unexec("bogus load command size");
135 return (0);
137 buf = malloc(command.cmdsize);
138 buf->cmd = command.cmd;
139 buf->cmdsize = command.cmdsize;
140 if (read(fd, ((char *)buf +
141 sizeof(struct load_command)),
142 size) != size) {
143 fatal_unexec("cannot read load command data");
144 return (0);
146 save_command(buf, the_commands, the_commands_len);
148 return (1);
151 static int
152 filldatagap(
153 vm_address_t start_address,
154 vm_size_t *size,
155 vm_address_t end_address
158 vm_address_t address;
159 vm_size_t gapsize;
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);
169 return (1);
172 static int
173 get_data_region(
174 vm_address_t *address,
175 vm_size_t *size
178 region_t region;
179 kern_return_t ret;
180 struct section *sect;
182 sect = (struct section *) getsectbyname(SEG_DATA, SECT_DATA);
183 region.address = 0;
184 *address = 0;
185 for (;;) {
186 ret = vm_region(task_self(),
187 &region.address,
188 &region.size,
189 &region.protection,
190 &region.max_protection,
191 &region.inheritance,
192 &region.shared,
193 &region.object_name,
194 &region.offset);
195 if (ret != KERN_SUCCESS || region.address >= VM_HIGHDATA) {
196 break;
198 if (*address != 0) {
199 if (region.address > *address + *size) {
200 if (!filldatagap(*address, size,
201 region.address)) {
202 return (0);
205 *size += region.size;
206 } else {
207 if (region.address == sect->addr) {
208 *address = region.address;
209 *size = region.size;
212 region.address += region.size;
214 return (1);
217 static char *
218 my_malloc(
219 vm_size_t size
222 vm_address_t address;
224 if (vm_allocate(task_self(), &address, size, TRUE) != KERN_SUCCESS) {
225 return (NULL);
227 return ((char *)address);
230 static void
231 my_free(
232 char *buf,
233 vm_size_t size
236 vm_deallocate(task_self(), (vm_address_t)buf, size);
239 static int
240 unexec_doit(
241 int infd,
242 int outfd
245 int i;
246 struct load_command **the_commands = NULL;
247 unsigned the_commands_len;
248 struct mach_header the_header;
249 int fgrowth = 0;
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;
257 vm_size_t vmaddr_growth = 0;
258 vm_size_t dataseg_vmaddr, dataseg_vmend;
260 struct segment_command *segment;
262 #ifdef NS_TARGET
263 unsigned long extreloff = 0;
264 unsigned long nextrel = 0;
265 struct dysymtab_command *dysymtab;
266 struct relocation_info reloc_info;
267 #endif
269 if (!read_macho(infd, &the_header, &the_commands, &the_commands_len)) {
270 return (0);
274 malloc_cookie = malloc_freezedry ();
275 if (!get_data_region(&data_address, &data_size)) {
276 return (0);
281 * DO NOT USE MALLOC IN THIS SECTION
285 * Fix offsets
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;
295 fgrowth = (data_size -
296 segment->filesize);
297 segment->vmsize = data_size;
298 segment->filesize = data_size;
299 dataseg_vmaddr = segment->vmaddr;
300 dataseg_vmend = segment->vmaddr + segment->vmsize;
301 vmaddr_growth = segment->vmaddr + segment->vmsize;
302 } else {
303 ((struct segment_command *)the_commands[i])->fileoff += fgrowth;
306 if( strcmp( segment->segname, SEG_LINKEDIT ) == 0 ) {
307 segment->vmaddr = vmaddr_growth;
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;
321 #ifdef NS_TARGET
322 case LC_DYSYMTAB:
323 dysymtab = ((struct dysymtab_command *)the_commands[i]);
324 extreloff = dysymtab->extreloff;
325 nextrel = dysymtab->nextrel;
326 dysymtab->indirectsymoff += fgrowth;
327 dysymtab->extreloff += fgrowth;
328 break;
329 #endif
330 default:
331 break;
336 * Write header
338 if (write(outfd, &the_header,
339 sizeof(the_header)) != sizeof(the_header)) {
340 fatal_unexec("cannot write output file");
341 return (0);
345 * Write commands
347 for (i = 0; i < the_commands_len; i++) {
348 if (write(outfd, the_commands[i],
349 the_commands[i]->cmdsize) !=
350 the_commands[i]->cmdsize) {
351 fatal_unexec("cannot write output file");
352 return (0);
357 * Write original text
359 if (lseek(infd, the_header.sizeofcmds + sizeof(the_header),
360 L_SET) < 0) {
361 fatal_unexec("cannot seek input file");
362 return (0);
364 size = fdatastart - (sizeof(the_header) +
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");
371 if (write(outfd, buf, size) != size) {
372 my_free(buf, size);
373 fatal_unexec("cannot write output file");
374 return (0);
376 my_free(buf, size);
380 * Write new data
382 if (write(outfd, (char *)data_address,
383 data_size) != data_size) {
384 fatal_unexec("cannot write output file");
385 return (0);
391 * OKAY TO USE MALLOC NOW
395 * Write rest of file
397 fstat(infd, &st);
398 if (lseek(infd, fdatasize, L_INCR) < 0) {
399 fatal_unexec("cannot seek input file");
400 return (0);
402 size = st.st_size - lseek(infd, 0, L_INCR);
404 buf = malloc(size);
405 if (read(infd, buf, size) != size) {
406 free(buf);
407 fatal_unexec("cannot read input file");
408 return (0);
410 if (write(outfd, buf, size) != size) {
411 free(buf);
412 fatal_unexec("cannot write output file");
413 return (0);
415 free(buf);
417 #ifdef NS_TARGET
419 * Fix up relocation entries in the data segment.
422 if (lseek(infd, extreloff, L_SET) < 0) {
423 fatal_unexec("cannot seek input file");
424 return (0);
427 for (i = 0; i < nextrel; i++)
429 long zeroval = 0;
431 if (read(infd, &reloc_info, sizeof (reloc_info)) != sizeof (reloc_info)) {
432 fatal_unexec("cannot read input file");
433 return (0);
435 if (reloc_info.r_address >= dataseg_vmaddr && reloc_info.r_address < dataseg_vmend)
437 if (lseek (outfd, fdatastart + reloc_info.r_address - dataseg_vmaddr, L_SET) < 0 ) {
438 fatal_unexec("cannot seek input file");
439 return (0);
441 switch (reloc_info.r_length) {
442 case 0:
443 if (write(outfd, &zeroval, 1) != 1) {
444 fatal_unexec("cannot write output file");
445 return (0);
447 break;
448 case 1:
449 if (write(outfd, &zeroval, 2) != 2) {
450 fatal_unexec("cannot write output file");
451 return (0);
453 break;
454 case 2:
455 if (write(outfd, &zeroval, 4) != 4) {
456 fatal_unexec("cannot write output file");
457 return (0);
459 break;
463 #endif
465 return (1);
468 void
469 unexec(
470 char *outfile,
471 char *infile
474 int infd;
475 int outfd;
476 char tmpbuf[L_tmpnam];
477 char *tmpfile;
479 infd = open(infile, O_RDONLY, 0);
480 if (infd < 0) {
481 fatal_unexec("cannot open input file `%s'", infile);
482 exit(1);
485 tmpnam(tmpbuf);
486 tmpfile = rindex(tmpbuf, '/');
487 if (tmpfile == NULL) {
488 tmpfile = tmpbuf;
489 } else {
490 tmpfile++;
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);
498 if (!unexec_doit(infd, outfd)) {
499 close(infd);
500 close(outfd);
501 unlink(tmpfile);
502 exit(1);
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);
513 /* arch-tag: 9796bdc3-c050-417a-b2f5-4cfd31032634
514 (do not change this comment) */